我最近开始学习Python,直到现在,一切似乎都相当直观。
我有一个文本文件,上面有几行数据。我遍历每一行,将其分成单词,我现在想要遍历给定行上的每个单词以检查它是否以给定字符串开头,然后如果是,则将单词更改为其他内容。
到目前为止,我有:
with open('test_inputfile.txt','r') as f:
for line in f:
words = line.split('","')
for word in words:
if word.startswith('spam'):
# change given word
但这并不起作用,因为我似乎无法访问.startswith()
的{{1}}功能。
我确信它一定很容易,因为到目前为止其他一切都非常简单!
感谢。
答案 0 :(得分:0)
你可以试试这个:
Append
现在,f存储包含每行中所有单词的列表。
答案 1 :(得分:0)
如果您使用的是CSV数据,这可能会很有用。如果是这种情况,请将您的拆分更改为line.split(',')
。否则见下文。
使用startswith
函数时,无需实际拆分行,因为您只对行开头的内容感兴趣。有关startswith
功能
with open('test_inputfile.txt', 'r') as f:
for line in f:
if line.startswith('spam', 0, 4):
# take action
这有效地检查“垃圾邮件”一词是否位于0到4位置
一切顺利:)
答案 2 :(得分:0)
您可能忘记删除每行中的初始/最终双引号。但我强烈建议使用csv
模块来处理csv数据:
import csv
with open('test_inputfile.txt','r') as f:
reader = csv.reader(f, delimiter=',', quotechar='"')
# both params are the default values anyway
for row in reader:
for word in row:
if word.startswith('spam'):
# do stuff
答案 3 :(得分:0)
你有一个这样的文件:
"toast","eggs","bacon"
"orangejuice","spamandtoast","bagels"
阅读文件:
with open("test_inputfile.txt", "r") as fs:
for lines in fs:
line = lines.split(",")
for word in line:
word = word.replace('"','') # removes the quotes
if word.startswith("spam"):
print word
您还可以在开头创建一个空列表wordlist = []
,并在列表中添加每个单词。
wordlist.append(word)
更好地使用csv
模块。