简体   繁体   English

从文件夹导入 Python 3 中的 .csv 文件

[英]Importing .csv file in Python 3 from folder

There are 2 csv files in same location: 1- candidates.csv 2- Store.csv同一位置有 2 个 csv 文件:1-Candidate.csv 2-Store.csv

When I'm importing candidates.csv filw while using this code, it is getting imported:当我在使用此代码时导入Candidate.csv filw 时,它会被导入:

data=pandas.read_csv("C:\\Users\\Nupur\\Desktop\\Ankit\\candidates.csv")

But when I'm using same code for importing Store.csv file, it is giving error:但是当我使用相同的代码导入 Store.csv 文件时,它给出了错误:

data=pandas.read_csv("C:\\Users\\Nupur\\Desktop\\Ankit\\Store.csv")

Error:错误:

UnicodeDecodeError Traceback (most recent call last) pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens() UnicodeDecodeError Traceback(最近一次调用最后一次)pandas._libs.parsers.TextReader._convert_tokens() 中的pandas_libs\\parsers.pyx

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype() pandas_libs\\parsers.pyx 在 pandas._libs.parsers.TextReader._convert_with_dtype()

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._string_convert() pandas._libs.parsers.TextReader._string_convert() 中的 pandas_libs\\parsers.pyx

pandas_libs\\parsers.pyx in pandas._libs.parsers._string_box_utf8() pandas._libs.parsers._string_box_utf8() 中的 pandas_libs\\parsers.pyx

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 9: invalid start byte UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 9 中的字节 0xf6:起始字节无效

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又发生了一个异常:

UnicodeDecodeError Traceback (most recent call last) in ----> 1 data=pandas.read_csv("C:\\Users\\Nupur\\Desktop\\Ankit\\Store.csv") ----> 1 data=pandas.read_csv("C:\\Users\\Nupur\\Desktop\\Ankit\\Store.csv") 中的 UnicodeDecodeError Traceback(最近一次调用最后一次)

C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\io\\parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, doublequote, delim_whitespace, low_memory, memory_map, float_precision) 676 skip_blank_lines=skip_blank_lines) 677 --> 678 return _read(filepath_or_buffer, kwds) 679 680 parser_f. C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\io\\parsers.py in parser_f(filepath_or_buffer,sep,delimiter,header,names,index_col,usecols,squeeze,prefix,mangle_dupe_cols,dtype,engine,converters,true_values 、false_values、skipinitialspace、skiprows、nrows、na_values、keep_default_na、na_filter、详细、skip_blank_lines、parse_dates、infer_datetime_format、keep_date_col、date_parser、dayfirst、迭代器、chunksize、压缩、千位、十进制、行终止符、escapechar、注释、编码,方言,tupleize_cols,error_bad_lines,warn_bad_lines,skipfooter,doublequote,delim_whitespace,low_memory,memory_map,float_precision) 676 skip_blank_lines=skip_blank_lines) 677 --> 678 return _read(filepath_or_buffer, 6079d) name = name姓名= 姓名

C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\io\\parsers.py in _read(filepath_or_buffer, kwds) 444 445 try: --> 446 data = parser.read(nrows) 447 finally: 448 parser.close() C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\io\\parsers.py in _read(filepath_or_buffer, kwds) 444 445 try: --> 446 data = parser.read(nrows) 447 最后:448 parser.close ()

C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\io\\parsers.py in read(self, nrows) 1034 raise ValueError('skipfooter not supported for iteration') 1035 -> 1036 ret = self._engine.read(nrows) 1037 1038 # May alter columns / col_dict C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\io\\parsers.py in read(self, nrows) 1034 raise ValueError('skipfooter not supported for iteration') 1035 -> 1036 ret = self._engine.read (nrows) 1037 1038 # 可以改变列 / col_dict

C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\io\\parsers.py in read(self, nrows) 1846 def read(self, nrows=None): 1847 C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\io\\parsers.py in read(self, nrows) 1846 def read(self, nrows=None): 1847
try: -> 1848 data = self._reader.read(nrows) 1849 except StopIteration: 1850 if self._first_chunk:尝试:-> 1848 数据 = self._reader.read(nrows) 1849 除了 StopIteration:1850 如果 self._first_chunk:

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader.read() pandas._libs.parsers.TextReader.read() 中的 pandas_libs\\parsers.pyx

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory() pandas._libs.parsers.TextReader._read_low_memory() 中的 pandas_libs\\parsers.pyx

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._read_rows() pandas._libs.parsers.TextReader._read_rows() 中的 pandas_libs\\parsers.pyx

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._convert_column_data() pandas._libs.parsers.TextReader._convert_column_data() 中的 pandas_libs\\parsers.pyx

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens() pandas._libs.parsers.TextReader._convert_tokens() 中的 pandas_libs\\parsers.pyx

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype() pandas_libs\\parsers.pyx 在 pandas._libs.parsers.TextReader._convert_with_dtype()

pandas_libs\\parsers.pyx in pandas._libs.parsers.TextReader._string_convert() pandas._libs.parsers.TextReader._string_convert() 中的 pandas_libs\\parsers.pyx

pandas_libs\\parsers.pyx in pandas._libs.parsers._string_box_utf8() pandas._libs.parsers._string_box_utf8() 中的 pandas_libs\\parsers.pyx

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 9: invalid start byte UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 9 中的字节 0xf6:起始字节无效

尝试使用这个,

data=pandas.read_csv("C:\\Users\\Nupur\\Desktop\\Ankit\\Store.csv",encoding = "ISO-8859-1")

If you face an encoding error due to encoding on your file not being the default as mentioned by the pd.read_csv() docs , you can find the encoding of the file by first installing chardet followed by the below code:如果由于文件编码不是pd.read_csv()文档中提到的默认编码而遇到编码错误,您可以通过先安装chardet后跟以下代码来找到文件的编码:

import chardet    
rawdata = open('D:\\path\\file.csv', 'rb').read()
result = chardet.detect(rawdata)
charenc = result['encoding']
print(charenc)

This will give you the encoding of the file.这将为您提供文件的编码。

Once you have the encoding, you can read as :一旦你有了编码,你可以读为:

pd.read_csv('D:\\path\\file.csv',encoding = 'encoding you found')

or或者

pd.read_csv(r'D:\path\file.csv',encoding = 'encoding you found')

You will get the list of all encoding here您将在此处获得所有编码的列表

Hope you find this useful.希望您觉得这个有帮助。

Did you tried:你有没有试过:

data=pandas.read_csv("C:\\Users\\Nupur\\Desktop\\Ankit\\Store.csv", encoding='utf-8')

If above doesn't work then seems you coding format is different, I would suggest to choose few encoding for Windows such as encoding='iso-8859-1' , encoding='cp1252' or encoding='latin1' .如果以上不起作用,那么您的编码格式似乎不同,我建议为 Windows 选择一些编码,例如encoding='iso-8859-1'encoding='cp1252'encoding='latin1'

OR try adding r in front of the filename, so that it will be considered a " raw string " so that backslashes won't be treated specially:或者尝试在文件名前添加r ,以便将其视为“ raw string ”,以便不会对反斜杠进行特殊处理:

data=pandas.read_csv(r"C:\\Users\\Nupur\\Desktop\\Ankit\\Store.csv", encoding='cp1252')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM