3

假设我有一个文件名(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 做同样的事情?如何将每列的值存储在变量中,然后在存储和操作它们的值时逐行读取文件?

4

4 回答 4

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.splithttp://docs.python.org/2/library/stdtypes.html# str.striphttp: //docs.python.org/2/library/stdtypes.html#bltin-file-objects 。

于 2013-08-23T21:33:50.840 回答
2
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)

现在,每个cols 都有四列中每一列的所有条目

于 2013-08-23T21:34:11.317 回答
2

Subhasis Das 的答案是关于文件打开、拆分等。但是您想要拥有值变量。这也很容易。代替

fields = line.strip().split()

a,b,c,d = line.strip().split()

但是,对于多于或少于四列的行,这将引发异常。

于 2013-08-23T21:38:45.070 回答
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
于 2013-08-23T21:35:58.610 回答