**:
eg:[python]view plain
copy
#! /usr/bin/python
import
sys
import
string
import
os
base_dir="/home/qinjianwang/mtr_dir/2012_09_12/tw"
as_uniq_info=base_dir + "/as_uniq_info"
get_line_num="wc -l "
+ as_uniq_info +
" | awk ''"
###get the lines of "as_uniq_info"
line_num = os.popen(get_line_num).read().strip('\n'
) global
vexnum
[python]view plain
copy
vexnum = string.atoi(line_num)
python 字串連線
[python]view plain
copy
base_dir=
"/home/qinjianwang/mtr_dir/2012_09_12/tw"
as_uniq_info=base_dir + "/as_uniq_info"
有:as_uniq_info="/home/qinjianwang/mtr_dir/2012_09_12/tw/as_uniq_info"
需要注意以下這句:
[python]view plain
copy
get_line_num=
"wc -l "
+ as_uniq_info +
" | awk ''"
###get the lines of "as_uniq_info"
注意1:
拼接shell命令時,需要引用python中的變數as_uniq_info,as_uniq_info單獨拿出來,再拼接其它命令,不要寫成下面這樣:
[python]view plain
copy
get_line_num=
"wc -l as_uniq_info | awk ''"
這樣寫會造成shell無法正確替換python中的變數as_uniq_info。
注意2:拼接shell命令時,要注意引號中命令間的留空,如以下
[python]view plain
copy
"wc -l "
和 " | awk ''"
引號"wc -l 「中的 -l 後面就需要留空,要注意。
python 呼叫 shell
get_line_num="wc -l as_uniq_info | awk ''" ###get the lines of "as_uniq_info"
line_num =os.popen(get_line_num).read().strip('\n')
get_line_num 作為shell執行的命令,會取得檔案as_uniq_info檔案的行數
os.popen(get_line_num):執行shell
get_line_num = os.popen(get_line_num).read():呼叫read,賦值給get_line_num,此時get_line_num中含有乙個換行符("\n")。
os.popen(get_line_num).read().strip('\n'):去掉換行符
shell 引用python變數
[python]view plain
copy
line_num = os.popen(get_line_num)
引用了python中的get_line_num變數。
python引用shell中值
[python]view plain
copy
line_num = os.popen(get_line_num).read()
執行完shell後,通過read()函式,賦值給python中的變數line_num
python 去掉換行符"\n"
[python]view plain
copy
line_num = os.popen(get_line_num).read().strip(
'\n'
)
通過strip()函式,去掉換行符"\n"
python按行讀取檔案 去掉換行符"\n"
[python]view plain
copy
forline
infile.readlines():
line=line.strip('\n'
)
python 字元轉實型
[python]view plain
copy
b="123.678"
>>> string.atof(b)
123.678
python 字元轉整型
[python]view plain
copy
vexnum = string.at
python按行讀取檔案
讀 1 2 3 4 file open sample.txt forline in file pass do something file.close 去除換行符 1 2 3 forline in file.readlines line line.strip n pass do something ...
按行讀取檔案
const string strurlfilename testurl.txt ifstream fin strurlfilename.c str fstream binary if fin fin.eof string serverurl getline fin,serverurl info lo...
python 按行讀取並判斷按行寫入檔案
f open description opinion.json w encoding utf 8 forline inopen test1set raw search.test1.json encoding utf 8 if question type description fact or opi...