python导入csv报错_Python Pandas read_csv报错
為實(shí)現(xiàn)文本去重(將前面采集的數(shù)據(jù)進(jìn)行兩兩對比刪除重復(fù)),寫了以下代碼。
#-*- coding: utf-8 -*-
import pandas as pd
inputfile = 'e:/data/H_KJ300F-JAC2101W.txt' #評論文件
outputfile = 'e:/data/H_KJ300F-JAC2101W_process_1.txt' #評論處理后保存路徑
data = pd.read_csv(inputfile, encoding = 'utf-8', header = None)
l1 = len(data)
data = pd.DataFrame(data[0].unique())
l2 = len(data)
data.to_csv(outputfile, index = False, header = False, encoding = 'utf-8')
print(u'刪除了%s條評論。' %(l1 - l2))
報(bào)錯(cuò):
Traceback (most recent call last): File "", line 1, in return _read(filepath_or_buffer, kwds) File "D:\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 401, in _read data = parser.read() File "D:\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 939, in read ret = self._engine.read(nrows) File "D:\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1508, in read data = self._reader.read(nrows) File "pandas\parser.pyx", line 848, in pandas.parser.TextReader.read (pandas\parser.c:10415) File "pandas\parser.pyx", line 870, in pandas.parser.TextReader._read_low_memory (pandas\parser.c:10691) File "pandas\parser.pyx", line 924, in pandas.parser.TextReader._read_rows (pandas\parser.c:11437) File "pandas\parser.pyx", line 911, in pandas.parser.TextReader._tokenize_rows (pandas\parser.c:11308) File "pandas\parser.pyx", line 2024, in pandas.parser.raise_parser_error (pandas\parser.c:27037)pandas.io.common.CParserError: Error tokenizing data. C error: Expected 1 fields in line 360, saw 2>>> data =pd.read_csv(inputfile,encoding ='utf-8',header = None) data = self._reader.read(nrows) File "pandas\parser.pyx", line 848, in pandas.parser.TextReader.read (pandas\parser.c:10415)>>> File "pandas\parser.pyx", line 911, in pandas.parser.TextReader._tokenize_rows (pandas\parser.c:11308) File "pandas\parser.pyx", line 2024, in pandas.parser.raise_parser_error (pandas\parser.c:27037)pandas.io.common.CParserError: Error tokenizing data. C error: Expected 1 fields in line 361, saw 2 File "pandas\parser.pyx", line 870, in pandas.parser.TextReader._read_low_memory (pandas\parser.c:10691) File "pandas\parser.pyx", line 924, in pandas.parser.TextReader._read_rows (pandas\parser.c:11437) ret = self._engine.read(nrows) File "D:\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1508, in read data = parser.read() File "D:\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 939, in read return _read(filepath_or_buffer, kwds) File "D:\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 401, in _read File "D:\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 646, in parser_fTraceback (most recent call last): File "", line 1, in
解決:把整個(gè)文件里面的半角","換成全角",“
原因:沒有設(shè)定分隔符的情況下,默認(rèn)使用","作為分隔條符。
總結(jié)
以上是生活随笔為你收集整理的python导入csv报错_Python Pandas read_csv报错的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: numpy 随机数_数据分析numpy基
- 下一篇: python的类程序的结构_Python