Python 2.7 Python脚本打印行两次

Python 2.7 Python脚本打印行两次,python-2.7,utf-8,Python 2.7,Utf 8,这是我为工作而写的一个小“哈克蒂”脚本,它将找到的所有行打印了两次,为什么 #!/usr/bin/python ins = open("FileName") for line in ins: s = list(line.decode("utf-8")) for character in s: if ord( character ) > 10000: print repr(line) ins.close() 你需要打破你的内环 fo

这是我为工作而写的一个小“哈克蒂”脚本,它将找到的所有行打印了两次,为什么

#!/usr/bin/python

ins = open("FileName")

for line in ins:
    s  = list(line.decode("utf-8"))
    for character in s:
       if ord( character ) > 10000:
          print repr(line)

ins.close()

你需要打破你的内环

for line in ins:
    s  = list(line.decode("utf-8"))
    for character in s:
       if ord( character ) > 10000:
          print repr(line)
          # Found it in the line, move onto the next line
          break
否则,如果一行中有多个匹配项,您将多次打印一行

您应该使用with语句作为文件句柄

with open("FileName", "r") as ins:
    <do stuff with ins>
打开(“文件名”,“r”)作为ins:

此外,您不必将line.decode()返回的字符串强制转换为列表;调用它的迭代器是列表构造函数无论如何都要做的事情。还是摆脱中间人吧。

因为在第一次
打印后,您不会
中断
for
循环的
?我猜这些行中有两个字符
ord
超过10000。甚至是“hacketier”:
import re;打印关于findall(u“^.[\u03e8-\uffff].*”,ins.read()
:)(dec 10000==hex 3e8)