簡體   English   中英

我在使用 PyCharm 上的代碼時遇到問題。 我正在嘗試讀取 csv 文件,但出現 unicode 錯誤,它無法讀取位置中的特定字節

[英]I'm having trouble with code on PyCharm. I'm trying to read a csv file but I'm getting a unicode error where it can't read specific bytes in positions

我的代碼如下所示:我使用 PyCharm 作為我的 IDE,我使用的 csv 文件來自 MS Excess。 我已將 csv 編碼為 UTF-8。 我正在嘗試使用 pandas 讀取文件。 當我調用 df.info() 時,我希望能夠區分對象和整數這也是我沒有將其更改為“latin-1”或“ISO ...”的原因

import pandas as pd  
import numpy as np  
import matplotlib.pyplot as plt  
plt.style.use('fivethirtyeight')  
cols = ['sentiment','id','date','query_string','user','text']  
df = pd.read_csv("trainingandtestdata\\training.1600000.processed.noemoticon.csv", header=None, 
names=cols, encoding='utf-8')#low_memory=False dtype='unicode' encoding='latin1'  
df.head()  
df.info()  
df.sentiment.value_counts()

我的錯誤如下所示:
如何修復 position xxxx 到 xxxx 中無法解碼的字節?

"C:\Users\dashg\PycharmProjects\Twitter Sentiment\venv\Scripts\python.exe" 
"C:/Users/dashg/PycharmProjects/Twitter Sentiment/Reviewer.py"   
Traceback (most recent call last):   
  File "C:/Users/dashg/PycharmProjects/Twitter Sentiment/Reviewer.py", line 6, in <module>  
    df = pd.read_csv("trainingandtestdata\\training.1600000.processed.noemoticon.csv", header=None,  
names=cols, encoding='utf-8')#low_memory=False dtype='unicode' encoding='latin1'  
  File "C:\Users\dashg\PycharmProjects\Twitter Sentiment\venv\lib\site- 
packages\pandas\io\parsers.py",       line 676, in parser_f      
    return _read(filepath_or_buffer, kwds)    
  File "C:\Users\dashg\PycharmProjects\Twitter Sentiment\venv\lib\site- 
packages\pandas\io\parsers.py",       line 454, in _read   
    data = parser.read(nrows)   
  File "C:\Users\dashg\PycharmProjects\Twitter Sentiment\venv\lib\site- 
packages\pandas\io\parsers.py",  
line 1133, in read  
    ret = self._engine.read(nrows)  
  File "C:\Users\dashg\PycharmProjects\Twitter Sentiment\venv\lib\site- 
packages\pandas\io\parsers.py",   line 2037, in read  
    data = self._reader.read(nrows)  
  File "pandas\_libs\parsers.pyx", line 860, in pandas._libs.parsers.TextReader.read  
  File "pandas\_libs\parsers.pyx", line 875, in pandas._libs.parsers.TextReader._read_low_memory  
  File "pandas\_libs\parsers.pyx", line 929, in pandas._libs.parsers.TextReader._read_rows  
  File "pandas\_libs\parsers.pyx", line 916, in pandas._libs.parsers.TextReader._tokenize_rows  
  File "pandas\_libs\parsers.pyx", line 2063, in pandas._libs.parsers.raise_parser_error      
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 51845-51846: invalid continuation 
byte

Process finished with exit code 1

當您在read_csv方法中使用encoding='utf-8'時,您的文件沒有utf-8編碼格式。 使用其他編碼方法來幫助您解決問題,例如'latin''ISO-8859-1' 我向您推薦鏈接以尋求幫助。

最壞的情況,如果這些都不起作用,您可以在'rb'模式下讀取文件( open(file, 'rb') )並通過使用 csv 分隔符拆分每一行數據來自己解析它!

我遇到了同樣的問題,但就我而言,解決方案非常簡單。 My ide is PyCharm 2020.1 and the.csv have the iso-8859-1 encoding, I've tried everything without luck, so I decided to check my ide config. 我去了:

  1. 文件
  2. 設置
  3. 左欄:編輯
  4. 在編輯器中:文件編碼然后我添加 my.csv 文件和右側的按鈕:+,最后更改 ide 的配置。 將其全部更改為 iso,因為默認情況下位於 utf-8 並使用確切的字符來處理文件,在我的情況下是:?。 希望這個作品

最好將 csv 保存到 xlsx 中並讀取為

pd.read_excel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM