-2

I exported an Excel sheet in TXT format; upon reading it in Python, I am not able to find strings insde it.

I tried several methods, none works: each one works only if I look for one character long string! Why?!?

fname="elenco.txt"

counter=0
for line in open(fname):
    counter = counter + 1
    if counter>5:
        break
    if "AB" in line:
        print "funziona"
    print line
    
with open(fname, 'r') as inF:
    for line in inF:
        if 'T' in line:
            print "OK"
    
with open(fname) as f:
    content=f.readlines()
counter=0
for rec in content:
    test=rec[:len(rec)-5]
    print test, ">>>",test.find('TORINO')
    counter = counter + 1
    if counter>5:
        break
    #if rec.find("ABBIA")>0:
    #   print "Trovato: " + rec
print "Fine."
4

3 回答 3

1

这对我来说很好:

fileList = open("filename", "r").readlines()

for line in fileList:
 if "string" in line:
  print line
于 2013-06-20T08:33:10.783 回答
0

这很奇怪:我发现错误不在源代码中,而是在文本文件中!虽然它有 .txt 后缀,而且它看起来像 PSPad 中的标准文本文件,但它不是:从 DOS 运行脚本我发现文件中的每个字母之间都有“空格”......所以我用十六进制编辑器检查了它,我发现它是一个unicode文件!我不知道为什么会这样,我通过将 Excel .xls 文件保存为 .txt 格式获得了该文件。

于 2013-06-21T13:47:09.640 回答
0

猜猜它是一个案例问题。尝试if "AB" in line.upper()代替if "AB" in line.

于 2013-06-20T09:09:25.100 回答