![](/img/trans.png)
[英]python script doesn't write a word in a text file after launching through another python script
[英]Python: Script won't find word in text file
我正在尝试从文本文件中找到特定的单词,但是我的脚本似乎无法将单词与文本文件中一行所写的内容相匹配,即使我知道它可以匹配。 我已经注意到有空格,但是既然我说的entry in line
,那行不行吗?
我也尝试过:
if str(entry) in line:,
if str(entry) in str(line): and
if entry in str(line):
但它们似乎都不起作用
我看不到我要去哪里错了。 任何帮助,将不胜感激。
这是我的代码
with open(address+'file_containing_data_I_want.txt') as f:
for entry in System_data:
print "Entry:"
print entry
for line in f:
print "Start of line"
print line
print"End of line"
if entry in line:
print "Found entry in line" #This never gets printed
使用打印语句(仅针对第一个条目),我看到:
Entry:
Manufacturer
Start of line
??
End of line
Start of line
End of line
Start of line
Manufacturer=manufacturer_data
End of line
Start of line
Model=model_data
End of line
Start of line
End of line
Start of line
End of line
文本文件如下所示(注意:我无法更改文本文件,因为这是我将收到的方式, '
表示空白行):
'
'
Manufacturer=manufacturer_data
Model=model_data
'
'
'
更新:将我的脚本更改为:
with open(address+'file_containing_data_I_want.txt') as f:
for line in f:
print "Start of line %s" % line
print"End of line"
for entry in System_data:
print "Entry: %s" % entry
if entry in line.strip():
print "Found entry in line"
结果被打印出来(仍然没有“在行中找到条目”):
Entry: Manufacturer
Entry: Model
Start of line:
End of line
Entry: Manufacturer
Entry: Model
Start of line: Manufacturer=manufacturer_data
End of line
Entry: Manufacturer
Entry: Model
Start of line: Model=model_data
Entry: Manufacturer
Entry: Model
Start of line:
End of line
Entry: Manufacturer
Entry: Model
Start of line:
End of line
将我的代码更改为此:
for line in f:
print "Start of line: %s" % line.strip("\r\n")
print "End of line"
for entry in System_data:
print "Entry: %s" % entry.strip()
if entry.strip() in line.strip("\r\n"):
print "FOUND!!!!!!!!!!!!!"
给我这个:
Start of line: ??
End of line
Entry: Manufacturer
Entry: Model
Start of line:
End of line
Entry: Manufacturer
Entry: Model
Start of line: Manufacturer=manufacturer_data
End of line
Entry: Manufacturer
Entry: Model
Start of line: Model=model_data
End of line
您在第一个循环之后读取到文件末尾。 相反,请交换循环,以便在文件的每一行都检查System_data
每个entry
:
for line in f:
print "Start of line %s" % line
print "End of line"
for entry in System_data:
print "Entry: %s" % entry
if entry.strip() in line.strip("\r\n"):
print "Found entry in line" #This now gets printed
或者您可以通过for line in f
之前调用f.seek(0)
在当前代码中更正此行为
您应该从文件的条目和行中删除所有空格/换行符。 因此,请在所有内容前加上前缀
entry = entry.strip()
并更改
if entry in line:
至
if entry in line.strip():
编辑:还有,摩西·科莱多耶(Moses Koledoye)说的
好的,所以看来问题在于该字符串实际上是十六进制形式。 但是当我使用print repr(line)
时,它只以十六进制形式出现: '\\x00m\\x00a\\x00n\\x00u\\x00f\\x00a\\x00c\\x00t\\x00u\\x00r\\x00e\\x00r\\x00_\\x00d\\x00a\\x00t\\x00a\\x00'
所以我将代码更改为以下内容:
with open(address+'file_containing_data_I_want.txt') as f:
for line in f:
for entry in System_data:
line=line.strip()
line = re.sub(r'[^\w=]', '', line)
if entry in line:
print "Found entry in line"
现在, if entry in line:
输入以下内容,此脚本将进入循环if entry in line:
并显示"Found entry in line"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.