Unicodedecodeerror:' Charmap'编解码器可以在位置2501中解码字节0x81:字符映射到< undefined>

发布于 2025-01-28 04:57:49 字数 1475 浏览 3 评论 0原文

我一直在寻找解决方案,但还没有找到任何有效的方法。我尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文