第一次海报,请和我一起...
#!/usr/bin/python
import sys, os
count = 0
filename = "data2.txt"
file = open(filename, "r")
s1 = input("What do you want to search for? ")
search = s1.lower()
words = search.split()
def restart_program():
"""Restarts the current program.
Note: this function does not return. Any cleanup action (like
saving data) must be done before calling this function."""
python = sys.executable
os.execl(python, python, * sys.argv)
def countlines(): #count length of file
with open(filename, 'r') as f:
count = 0
for line in f:
count += 1
return count
total = countlines()
totp1 = total + 1
with open(filename) as f:
currpos = 0
for line in f:
currpos += 1
line2 = line.lower()
if currpos < totp1:
if len(search.split()) == 2:
if line2.find(words[0]) != -1 and line2.find(words[1]) != -1:
print(line)
if len(search.split()) == 1:
if line2.find(search) != -1:
print(line)
#restart_program()
if search == "quit":
print('Exiting')
break
if search == "exit":
print('Exiting')
break
if currpos == total:
restart_program()
当我在Repl.it上运行此代码时,它可以毫无问题地运行,并且可以正常运行。但是,当我在笔记本电脑(Arch)上运行它时,出现此错误:
Traceback (most recent call last):
File "./search", line 23, in <module>
total = countlines()
File "./search", line 20, in countlines
for line in f:
File "/usr/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 359: invalid start byte
任何帮助表示赞赏。如果错过任何必需的详细信息,请询问。谢谢!