使用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