【发布时间】:2018-06-29 17:06:23
【问题描述】:
给定文本文件
sample.txt
2012-01-01 09:00 San Diego Men's Clothing 214.05 Amex
2012-01-01 09:00 San Diego Women's Clothing 153.57 Visa
2012-01-01 09:00 Omaha Music 66.08 Cash
我希望能够只阅读第三列的文本。这段代码
for line in open("sample.txt"):
city=line.split()[2]
print(city)
可以在一定程度上阅读第三列:
San
San
Omaha
但我想要的是:
San Diego
San Diego
Omaha
我该怎么做?
【问题讨论】:
-
您需要指定拆分字符串的子字符串。类似:
split("\t")。解释了here