Я пытаюсь объединить несколько CSV-файлов и получаю ошибку utf-8, не знаю, что это значит. В основном у меня есть 7 "pm" файлов (номер 12-18), и я хочу объединить их все. Я в значительной степени там, но когда я пытаюсь экспортировать, я получаю ошибку utf-8, и я не уверен, что делать, потому что я не знаю, что это значит. Ниже приведен соответствующий код:
import numpy as np
import pandas as pd
import os
from glob import glob
from os import chdir
#import data frames
pm18 = pd.read_csv('pm18.csv', encoding='ISO-8859-1', low_memory=False)
pm17 = pd.read_csv('pm17.csv', encoding='ISO-8859-1', low_memory=False)
pm16 = pd.read_csv('pm16.csv', encoding='ISO-8859-1', low_memory=False)
pm15 = pd.read_csv('pm15.csv', encoding='ISO-8859-1', low_memory=False)
pm14 = pd.read_csv('pm14.csv', encoding='ISO-8859-1', low_memory=False)
pm13 = pd.read_csv('pm13.csv', encoding='ISO-8859-1', low_memory=False)
pm12 = pd.read_csv('pm12.csv', encoding='ISO-8859-1',low_memory=False)
#list files in directory
csv_file_path = (r'C:\Users\EDraklellis\Desktop\FebruaryDownloads\FebruaryDownloads')
chdir(csv_file_path)
# List all CSV files in the working dir
file_pattern = ".csv"
list_of_files = [file for file in glob(os.path.join(csv_file_path,'*.csv'))]
print(list_of_files)
#concatenate listed files
file_out = 'mergell84.csv'
def produceOneCSV(list_of_files, file_out):
# Consolidate all CSV files into one object
result_obj = pd.concat([pd.read_csv(file) for file in list_of_files])
result_obj.to_csv(file_out, index=False, encoding='utf-8')
produceOneCSV(list_of_files, file_out)
эта строка выдает следующую ошибку
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-113-bea8878ce7aa> in <module>
----> 1 produceOneCSV(list_of_files, file_out)
<ipython-input-112-e94873c1017a> in produceOneCSV(list_of_files, file_out)
3 def produceOneCSV(list_of_files, file_out):
4 # Consolidate all CSV files into one object
----> 5 result_obj = pd.concat([pd.read_csv(file) for file in list_of_files])
6 result_obj.to_csv(file_out, index=False, encoding='utf-8')
<ipython-input-112-e94873c1017a> in <listcomp>(.0)
3 def produceOneCSV(list_of_files, file_out):
4 # Consolidate all CSV files into one object
----> 5 result_obj = pd.concat([pd.read_csv(file) for file in list_of_files])
6 result_obj.to_csv(file_out, index=False, encoding='utf-8')
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, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
683 )
684
--> 685 return _read(filepath_or_buffer, kwds)
686
687 parser_f.__name__ = name
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
455
456 # Create the parser.
--> 457 parser = TextFileReader(fp_or_buf, **kwds)
458
459 if chunksize or iterator:
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
893 self.options["has_index_names"] = kwds["has_index_names"]
894
--> 895 self._make_engine(self.engine)
896
897 def close(self):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
1133 def _make_engine(self, engine="c"):
1134 if engine == "c":
-> 1135 self._engine = CParserWrapper(self.f, **self.options)
1136 else:
1137 if engine == "python":
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
1915 kwds["usecols"] = self.usecols
1916
-> 1917 self._reader = parsers.TextReader(src, **kwds)
1918 self.unnamed_cols = self._reader.unnamed_cols
1919
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._get_header()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 16: invalid start byte