假设我有一个文件名(test.txt),其中包含以下数据:
AA11 BB11 CC11 DD11
AA22 BB22 CC22 DD22
AA33 BB44 CC44 DD33
在bash(shell脚本)中我可以执行以下操作:
cat test.txt | while read a b c d
do
echo "this is the first column $a "
echo "this is the second column $b "
echo "this is the third column $c "
echo "this is the fifth column $d "
done
我怎么能用python做同样的事情?如何在变量中存储每列的值,然后在存储和操作它们的值时逐行读取文件?
答案 0 :(得分:7)
file = open('test.txt')
for line in file:
fields = line.strip().split()
print fields[0], fields[1], fields[2], fields[3]
Python非常简单:)
更具体地说,split()
将字符串的内容拆分为由某个分隔符分隔的字段(默认情况下为任何空白字符,例如空格,制表符等),并返回包含拆分字段的数组。 strip()
从行的开头和结尾删除所有空白字符。 python中的文件是iterable
对象,当被关键字in
迭代时,会逐个给出文件中的行。有关这些的更多信息,您可以查看http://docs.python.org/2/library/stdtypes.html#str.split,http://docs.python.org/2/library/stdtypes.html#str.strip,http://docs.python.org/2/library/stdtypes.html#bltin-file-objects。
答案 1 :(得分:1)
with open('test.txt') as infile:
lines = [line.strip().split() for line in infile] # bad for large files
col1, col2, col3, col4 = itertools.izip(*lines)
现在,每个col
都包含四列
答案 2 :(得分:1)
使用csv
模块:
import csv
nth = {
1: "first",
2: "second",
3: "third",
4: "fourth"
}
with open('test.txt', 'r') as f:
reader = csv.reader(f, delimiter=" ")
for row in reader:
for index, item in enumerate(row):
print "this is the %s column %s" % (nth[index + 1], item)
而且,不使用csv
:
nth = {
1: "first",
2: "second",
3: "third",
4: "fourth"
}
with open('test.txt', 'r') as f:
for row in f:
for index, item in enumerate(row.split()):
print "this is the %s column %s" % (nth[index + 1], item.strip())
打印:
this is the first column AA11
this is the second column BB11
this is the third column CC11
this is the fourth column DD11
this is the first column AA22
this is the second column BB22
this is the third column CC22
this is the fourth column DD22
this is the first column AA33
this is the second column BB44
this is the third column CC44
this is the fourth column DD33
答案 3 :(得分:1)
关于文件打开,分割等,Subhasis Das的答案很好。 但是你想拥有值变量。这也很简单。而不是
fields = line.strip().split()
写
a,b,c,d = line.strip().split()
对于具有多于或少于四列的行,这将抛出异常。