简体   繁体   English

有没有办法在 Pandas python 中解决这个问题——“UnicodeEncodeError: 'charmap' codec can't encode characters in position”?

[英]Is there a way to solve this issue in pandas python - " UnicodeEncodeError: 'charmap' codec can't encode characters in position "?

I have been trying to open an excel file (xlsx format and csv format) using python pandas and I am facing utf-8 encoding errors.我一直在尝试使用 python pandas 打开一个 excel 文件(xlsx 格式和 csv 格式),但我面临 utf-8 编码错误。 I have also tried the encoding codes but could not solve the issue.我也尝试过编码代码,但无法解决问题。

Kindly support me to understand and solve the issue请支持我理解并解决问题

this is the code :这是代码:

import pandas as pd
excel_file = 'Task1/Data_task1.xlsx'
data =  pd.read_excel(excel_file, encoding='utf-8', errors = 'ignore')
print(data)

Error :错误 :

File "c:\Users\nivas\Desktop\Srinivas\Internship\Dealroomo\Task1\task1.py", line 4, in <module>
    print(data)
  File "C:\Users\nivas\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 3140-3145: character maps to <undefined>

From my experience, Excel Text and Python do not play well together, and the many times the encoding just never works;根据我的经验,Excel Text 和 Python 不能很好地协同工作,而且很多时候编码根本不起作用; do not know why or how.不知道为什么或如何。

2 Possible solutions: 2 可能的解决方案:

  1. Convert the file to CSV (.txt/.csv format) and see if you can encode it manually inside Excel.将文件转换为 CSV(.txt/.csv 格式),看看是否可以在 Excel 中手动对其进行编码。

  2. Run the program on Linux Ubuntu using LibreOffice instead of Excel.使用 LibreOffice 而不是 Excel 在 Linux Ubuntu 上运行该程序。 Again, you will need to convert to .csv.同样,您需要转换为 .csv。 However, LibreOffice seems to handle the encoding MUCH better than Excel.但是,LibreOffice 似乎比 Excel 更好地处理编码。 For whatever reason, Excel can refuse to convert and get rid of all the funky characters that raise Unicode Error in Python.无论出于何种原因,Excel 都可以拒绝转换并删除所有在 Python 中引发 Unicode 错误的时髦字符。

Best of luck祝你好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python,UnicodeEncodeError:“ charmap”编解码器无法在位置编码字符 - Python, UnicodeEncodeError: 'charmap' codec can't encode characters in position UnicodeEncodeError: 'charmap' 编解码器无法编码字符 - UnicodeEncodeError: 'charmap' codec can't encode characters Python Dataframe 到 CSV - UnicodeEncodeError: 'charmap' 编解码器无法编码字符 - Python Dataframe to CSV - UnicodeEncodeError: 'charmap' codec can't encode characters Python-UnicodeEncodeError:“ charmap”编解码器无法对位置85-89中的字符进行编码:字符映射到<undefined> - Python - UnicodeEncodeError: 'charmap' codec can't encode characters in position 85-89: character maps to <undefined> CSV模块触发“ UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符” - “UnicodeEncodeError: 'charmap' codec can't encode characters” triggered by CSV module UnicodeEncodeError: 'charmap' 编解码器无法编码字符/写入 txt 文件 - UnicodeEncodeError: 'charmap' codec can't encode characters/ writing in txt file UnicodeEncodeError: &#39;charmap&#39; 编解码器无法对位置 1082-1​​084 中的字符进行编码:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode characters in position 1082-1084: character maps to <undefined> UnicodeEncodeError:“charmap”编解码器无法对 108308-108313 中的字符 position 进行编码 - UnicodeEncodeError: 'charmap' codec can't encode characters position in 108308-108313 获取:UnicodeEncodeError:&#39;charmap&#39; 编解码器无法对位置 0-1 中的字符进行编码:字符映射到<undefined> - Getting:UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined> UnicodeEncodeError: &#39;charmap&#39; 编解码器无法对位置 131-132 中的字符进行编码:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode characters in position 131-132: character maps to <undefined>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM