Unicodedecodeerror:' Charmap'编解码器可以在位置2501中解码字节0x81:字符映射到< undefined>
我一直在寻找解决方案,但还没有找到任何有效的方法。我尝试在filepath的前面添加R。我尝试将编码更改为UTF-8。似乎没有任何作用。
我在Mac上写了此脚本,它运行良好,并且正在尝试在Windows上运行它,但它不起作用。
fin = open("C:\\Users\\filepath\\indivsWG22.txt", "rt", encoding = 'ISO-8859-1')
fout = open("C:\\Users\\filepath\\Downloads\\out.csv", "w")
#for each line in the input file
for line in fin:
#read replace the string and write to output file
#This is where I deal with all the pipes and commas
fout.write(line.replace(',', '^').replace('|~|',',').replace('|~',',').replace('~|',','))
#close input and output files
fin.close()
fout.close()
这是我遇到的完整错误:
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-33-22326598bb16> in <module>
8 fout = open("C:\\Users\\filepath\\out.csv", "w")
9 #for each line in the input file
---> 10 for line in fin:
11 #read replace the string and write to output file
12 #This is where I deal with all the pipes and commas
~\Anaconda3\lib\encodings\cp1252.py in decode(self, input, final)
21 class IncrementalDecoder(codecs.IncrementalDecoder):
22 def decode(self, input, final=False):
---> 23 return codecs.charmap_decode(input,self.errors,decoding_table)[0]
24
25 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2501: character maps to <undefined>
I've been looking for solutions to this and haven't found any that worked. I've tried adding r in front of the filepath. I've tried changing the encoding to utf-8. Nothing seems to work.
I wrote this script on a Mac and it worked fine and I'm trying to run it on Windows but it's not working.
fin = open("C:\\Users\\filepath\\indivsWG22.txt", "rt", encoding = 'ISO-8859-1')
fout = open("C:\\Users\\filepath\\Downloads\\out.csv", "w")
#for each line in the input file
for line in fin:
#read replace the string and write to output file
#This is where I deal with all the pipes and commas
fout.write(line.replace(',', '^').replace('|~|',',').replace('|~',',').replace('~|',','))
#close input and output files
fin.close()
fout.close()
This is the full error I'm getting:
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-33-22326598bb16> in <module>
8 fout = open("C:\\Users\\filepath\\out.csv", "w")
9 #for each line in the input file
---> 10 for line in fin:
11 #read replace the string and write to output file
12 #This is where I deal with all the pipes and commas
~\Anaconda3\lib\encodings\cp1252.py in decode(self, input, final)
21 class IncrementalDecoder(codecs.IncrementalDecoder):
22 def decode(self, input, final=False):
---> 23 return codecs.charmap_decode(input,self.errors,decoding_table)[0]
24
25 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2501: character maps to <undefined>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论