代码如下:
print ("Enter first the source file and then destination file with respective format")
t=input() #Enter source file
t1=input() #Enter destination file
print ("Enter the keyword which has to be copied into another file")
find=input() #Enter the keyword which has to be copied
f=open(t,encoding="utf-8") #Open the source file
f1=open(t1,"a+",encoding="utf-8") #Open the destination file in append and read mode
Copy_Lines=False
for line in f.readlines():
if find in line:
Copy_Lines=True
if Copy_Lines:
f1.write(line)
f1.close()
f.close()
print("Task completed")
但是我犯了这个错误,我只是没能找出问题所在
Traceback (most recent call last):
File "C:\Users\sanket_d\Desktop\python files\COPY_ONE.py", line 13, in <module>
for line in f.readlines():
File "C:\Python34\lib\codecs.py", line 313, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte
请提前帮助和感谢
正如Martijn Pieters评论的那样,您的文件不是
utf-8
编码的,我已经重写了您的代码,使其更符合pep-8样式指南,更具可读性:变量名使用小写字母,用下划线
_
分隔单词。你知道吗在赋值之间留一个空格,比如
copy_lines = False
而不是copy_lines=False
,#
和注释之间留一个空格,比如# mycomment
而不是#mycomment
。你知道吗使用
with
打开文件,因为它会自动关闭文件。你知道吗相关问题 更多 >
编程相关推荐