0%

Open Read Write File

open syntax: 

open( file, mode, encoding="utf-8")

mode 字串 說明
r Read only 讀取模式 ( 預設 )
r+ Read and write
w Write only 寫入模式,檔案若存在,會清空內容再寫入; 若檔案不存在,則建立新檔開啟寫入
w+ write+read
a Append only 附加模式,若檔案存在,則寫入內容會附加至檔 案尾端
a+ Append and Read (‘a+’)

Read

讀取方法 描述
read( ) 一次讀取檔案所有的內容,回傳為字串
readline( ) 只讀取一行內容
readlines( ) 將所有檔案內容,每行讀入回傳為串列
1
2
3
fileObj = open("路徑檔名" , "r", encoding="utf-8") 
content = fileObj.read( )
fileObj.close( )

Write

1
2
3
fileObj = open("路徑檔名" , "w", encoding="utf-8")
fileObj.write(輸出資料)
fileObj.close( )

Example

Ex1. read file and print

Read more »

Interview Code Test

I am sharing some Code Test during my Interview, and also some practice Code example.
今天分享我一些Live Coding 面試問題,還有一些刷題的問題。

Amazon Live Code

Q1. read file into datastucture and search like logfile

If we have a log.txt, as below, you need to and search specfic string, like cat.

  • log.txt
    1
    2
    3
    1 cat
    2 lion
    3 apple

Answer 1: list

1
2
3
4
5
6
7
8
with open("log.txt", "r") as textfile:
lines = textfile.readlines()
for i in lines:
sli = i.split(' ')
#print(sli)
if "cat" in sli:
print(sli[0], sli[-1])

Answer2: using dictionary

1
2
3
4
5
6
7
8
9
10
11
12
d={}
with open("log.txt", "r") as textfile:
#lines = textfile.readlines()
#skip \n
lines = textfile.read().splitlines()
#print(lines)
for i in lines:
#sli = i.split(' ')
(key,val)=i.split(" ")
d[int(key)]=val
# print(key,"",val)
print(d)
Read more »

Advance JS

構造函數constructor Function

1
2
3
4
5
6
7
8
9
10
11
12
13
function createCard(initName){
this.name=initName

}

const a1 = new createCard("Ma")
const a2 = new createCard("Ma2")
const a3 = new createCard("Ma3")
const a4 = new createCard("Ma4")
const a5 = new createCard("Ma5")
console.log(a1)
console.log(a2)
console.log(a3)

output

createCard { name: ‘Ma’ }
createCard { name: ‘Ma2’ }
createCard { name: ‘Ma3’ }

array function =>

In ES6 there is a new function called array function, which you can write shorter function.

Before ES6

function hello(){}

after ES6

let hello = function (){}
shorthand
let hello = () =>{}

function without passing paramter

Before ES6 (normal function)

ex: function hello(){}

Read more »

今天我想分享如何用linux shell script 語法。
我們需要加這個在script裡面,然後檔案改成.sh

1
#! /bin/bash
  • How to execute script

    chmod 777 myscript.sh
    ./myscript.sh

1. variable

define variable

1
2
3
text1=Hello
name=CC
echo $text1 $name

output:

Hello CC

read and echo variable

read variable

read a

read vairable without skip new line:

read -p “Your Options: “ option

Read more »

alias

Method 1: put your alias in .alias_pofile

Recommend use this file, will not destory .bashrc file.

1
2
cd ~
touch .alias_pofile

Method 2: edit .bashrc

This method need to reboot to take effect

change directory

change folder or directory

cd : change directory to home ~ directory
cd folder: change to specific directory

change upper directory

Read more »

1. Print

Ex1 Print Basic Interger and String

1
2
3
str_name= "hello"
num_value= 999
print(f"str value is {str_name}, and number is {num_value}")

output:

str value is hello, and number is 999

%-formatting-OLD

Example 1 Print String with % Operator

1
2
3
4
5
6
7
text = 'world'
print("hello " + text)
print('hello %s' % text)

name = "Test"
num = 100
print("Hello, %s. My num is %s." % (name, num))

output:

hello world
hello world
Hello, Test. You are 100.

3. str.format()- NEW

str.format() 是對 %-formatting 的改進,可以用函式呼叫語法,可以通過 format() 方法對被轉換為字串的物件

Read more »

JS基本須知道

How to run JS

  • Browser- inspect

    • Method1:

      Go to inspect > console

    • Method 2:

      inspect> sources> snipperts> create js file

  • NodeJS environment

    XXX.js//CREATE JS file

    node XXX.js //run .js file using node

adding comment

// single line comment
/**/: multiply line comment

顯示 print

  • console.log() 顯示在console
  • alert() 跳出顯示
  • document.write("\<h1>hello <br\/>")

JS  基本語法- Part 1

1. declare variable 宣告變數

  • let (新)可以取代var

    ex:

    let myName= "chenchih"
    console.log(myName)

  • var
  • const

    must be inital and not able to change
    不被修改變數。如果不想被人修改

    1
    2
    3
    const test= "heloo"
    test="world"
    console.log(test) //error
Read more »

今天我想寫關於 Markdown相關指令。想信大家都會常用MD或是markdown。

字體效果

斜體字 ==> *文字*
粗體字 ==> **文字**
斜粗體 ==> ***文字***
刪除線strikethrough ==> ~~文字~~
斜體2 ==> _文字_
斜粗2 ==> __文字__
正常^上標^ ==> 文字^string1^
正常下標 ==> 文字~string1~
++底線++ ==> ++文字++
==螢光標記== ==> ==文字==

color顏色

  • color ==> <font color="red">text </font>
  • Highlighted text => <mark style="background-color: #FFFF00">Highlighted text</mark>

    change mark or span/div

Font Size 大小

  • HTML ==> <font size=5> **HTML**</font>

Horizontal Rules


1
2
3
---
***
___
Read more »

前言

今天天想分享如何在terminal或終端機變成更有效率或是更美化。我們需要用到zshohmysh

本篇文章會安裝以下套件:

  • ubuntu 安裝和設定
    • 基本套件: zsh git wget curl
    • ohymysh套件
    • shell 設定
    • Oh My Zsh 安裝,主題設定
    • 安裝Powerlevel10k 主題,不然會怪怪

更新 ubuntu 套件

$ sudo apt-get update

安裝 zsh

檢查我們目前支援的shells

目前支援的shells
$cat /etc/shells

zsh-after

Read more »

在這篇教學,我想要分享HEXO簡易版本。我在之前有寫ㄧ篇是關於Hexo完整篇,文章太長。如果你不想看那一篇可以看這一篇。在這一篇,我只會分享一些基本指令。

安裝 git

可以到官網下載git:,或是你是Linux/MAC,可以輸入命令方式來安裝如下:

  • Linux (Ubuntu, Debian):

    $ sudo apt-get install git-core

  • Linux (Fedora, Red Hat, CentOS)

    sudo yum install git-core

  • mac 安裝:

    brew install git

安裝 Hexo

先安裝 node.js

可以到官網下載Node.js,或是你是Linux/MAC,可以輸入命令方式來安裝如下:

  • MAC:

    $ brew update
    $ brew install node

  • Linux(ubuntu)

    $ sudo apt-get install -y nodejs

再安裝 Hexo

$ npm install -g hexo-cli # -g 全域安裝

Read more »