python按行读取文件 去掉换行符"\n"
2012-09-12 14:14
981 查看
eg:
python 字符串连接
有:as_uniq_info="/home/qinjianwang/mtr_dir/2012_09_12/tw/as_uniq_info"
需要注意以下这句:
注意2:拼接shell命令时,要注意引号中命令间的留空,如以下
python 调用 shell
get_line_num="wc -l as_uniq_info | awk '{print $1}'" ###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中的get_line_num变量。
python引用shell中值
执行完shell后,通过read()函数,赋值给python中的变量line_num
python 去掉换行符"\n"
通过strip()函数,去掉换行符"\n"
python按行读取文件 去掉换行符"\n"
Python 字符转实型
Python 字符转整型
#! /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 '{print $1}'" ###get the lines of "as_uniq_info" line_num = os.popen(get_line_num).read().strip('\n') global VEXNUM
VEXNUM = string.atoi(line_num)
python 字符串连接
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"
需要注意以下这句:
get_line_num="wc -l " + as_uniq_info + " | awk '{print $1}'" ###get the lines of "as_uniq_info"注意1:拼接shell命令时,需要引用python中的变量as_uniq_info,as_uniq_info单独拿出来,再拼接其它命令,不要写成下面这样:
get_line_num="wc -l as_uniq_info | awk '{print $1}'"这样写会造成shell无法正确替换python中的变量as_uniq_info。
注意2:拼接shell命令时,要注意引号中命令间的留空,如以下
"wc -l " 和 " | awk '{print $1}'"引号"wc -l “中的 -l 后面就需要留空,要注意。
python 调用 shell
get_line_num="wc -l as_uniq_info | awk '{print $1}'" ###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变量
line_num = os.popen(get_line_num)
引用了python中的get_line_num变量。
python引用shell中值
line_num = os.popen(get_line_num).read()
执行完shell后,通过read()函数,赋值给python中的变量line_num
python 去掉换行符"\n"
line_num = os.popen(get_line_num).read().strip('\n')
通过strip()函数,去掉换行符"\n"
python按行读取文件 去掉换行符"\n"
for line in file.readlines(): line=line.strip('\n')
Python 字符转实型
b="123.678" >>> string.atof(b) 123.678
Python 字符转整型
VEXNUM = string.atoi(line_num)
相关文章推荐
- python按行读取文件 去掉换行符"\n"
- python按行读取文件,如何去掉换行符"\n"
- python按行读取文件,如何去掉换行符"\n"
- PHP按行读取文件 去掉换行符"\n"
- python按行读取文件 去掉换行符"\n" (查眼力,比较文档的不同)
- python按行读取文件,去掉每行的换行符"\n"
- python按行读取文件,如何去掉换行符"\n"
- python按行读取文件,如何去掉换行符"\n"
- python按行读取文件,如何去掉换行符"\n"
- python按行读取文件,如何去掉换行符"\n"
- python按行读取文件 如何去掉换行符 "\n"
- 参数化时按行读取txt文件,如何去掉换行符"\n"
- 【Python】按行读取文件、IOError: [Errno 22] invalid mode ('a+') or filename,处理文件的换行符
- python按行读取文件,去掉每行的换行符\n的实例
- PHP按行读取文件 去掉换行符"\n"
- python按行读取文件,去掉换行符"\n"
- Python 按行读取文件并去掉换行
- python 文件读取,去掉换行符
- 看面试题看到一个按行读取文件,写了个按行读取将读取到的文件按","分割,将分割的数组封装为一个类,也就是可以写到数据库了
- [Python]用Python编写程序修复"恶邮差"变种U病毒感染文件。