0%

Create empty Excel File

1
2
3
4
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.Workbook()
wb.save(fn)

Create Sheet工作表的

Create Empty Sheet 新增工作

1
2
3
4
5
6
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.Workbook()
wb.create_sheet("Sheet1", 0)
wb.create_sheet("Sheet2", 1)
wb.save(fn)

Read Excel Sheet 讀取excel檔案每個工作表的名稱

1
2
3
4
5
6
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.load_workbook(fn)
print(wb.sheetnames)
print(wb.active)
print(wb.active.title)

output:

1
2
3
['Sheet1', 'Sheet2', 'Sheet']
<Worksheet "Sheet1">
Sheet1

edit Name of worksheet 修改工作表名稱

Read more »

list 列印

這裡會包含以下內內容:

  • 基本讀 list 搭配 for
  • *list (快速讀 list 不需要用到 for loop)
  • N 筆後換
  • 轉換 list 到字串(搭配 join)
  • enumerate 方法
  • Method:
    • split
    • slice
    • copy
    • zip
    • 轉換 list 理的 tuple 到 list

常用的方式印 list 內容

list : list = ['A','B','C','D','E','F']

我們可以用以下方法:

- for loop

1
2
3
4
list= [1,2,3]
for i in list:
print(i, end=" ") #
print()

output:

1 2 3

- *list: 不需要 for loop ,可以 unpack list

Read more »

今天分享如何搜尋字串裡面的 substring 或如果有檔案(log)想找相關字來做分析。

split() 方式

我的 string 是:

st="[20221013.162853.788442][info]:[>>> DL- ingress traffic: 0.010799(Mbps), egress traffic: 0.087016(Mbps), ReTx: 0.000220(Mbps)]"

我想抓出時間TPUT的直,如:
[20221013.162853.788442]0.010799(Mbps) 同時要把 [](Mbps)移除,可以用下面方式:

  • Step1: 宣告我的 string
1
st="[20221013.162853.788442][info]:[>>> DL- ingress traffic: 0.010799(Mbps), egress traffic: 0.087016(Mbps), ReTx: 0.000220(Mbps)]"
  • Step2 抓取時間直
1
2
dateStr = st.split('[', 1)[1].split(']')[0]
print(dateStr) #20221013.162853.788442

output:

20221013.162853.788442

Read more »

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 »