在python中合并CSV文件[重复]
Posted
技术标签:
【中文标题】在python中合并CSV文件[重复]【英文标题】:Merging CSV files in python [duplicate] 【发布时间】:2019-10-30 21:27:39 【问题描述】:我一直在尝试将多个 csv 文件合并为一个,但它显示了一些错误。我是 python 新手,非常感谢您的帮助。
以下是我的代码:
import pandas as pd
import numpy as np
import glob
all_data_csv = pd.read_csv("C:/Users/Am/Documents/A.csv", encoding='utf-8')
for f in glob.glob('*.csv'):
df = pd.read_csv(f, encoding='utf-8')
all_data_csv= pd.merge(all_data_csv,df ,how= 'outer')
print(all_data_csv)
以及显示的错误:
Traceback (most recent call last):
File "pandas\_libs\parsers.pyx", line 1169, in pandas._libs.parsers.TextReader._convert_tokens
File "pandas\_libs\parsers.pyx", line 1299, in pandas._libs.parsers.TextReader._convert_with_dtype
File "pandas\_libs\parsers.pyx", line 1315, in pandas._libs.parsers.TextReader._string_convert
File "pandas\_libs\parsers.pyx", line 1553, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 1: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/internship/j.py", line 8, in <module>
df = pd.read_csv(f, encoding='utf-8')
File "C:\Users\Amreeta Koner\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\Amreeta Koner\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 435, in _read
data = parser.read(nrows)
File "C:\Users\Amreeta Koner\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 1139, in read
ret = self._engine.read(nrows)
File "C:\Users\Amreeta Koner\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 1995, in read
data = self._reader.read(nrows)
File "pandas\_libs\parsers.pyx", line 899, in pandas._libs.parsers.TextReader.read
File "pandas\_libs\parsers.pyx", line 914, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas\_libs\parsers.pyx", line 991, in pandas._libs.parsers.TextReader._read_rows
File "pandas\_libs\parsers.pyx", line 1123, in pandas._libs.parsers.TextReader._convert_column_data
File "pandas\_libs\parsers.pyx", line 1176, in pandas._libs.parsers.TextReader._convert_tokens
File "pandas\_libs\parsers.pyx", line 1299, in pandas._libs.parsers.TextReader._convert_with_dtype
File "pandas\_libs\parsers.pyx", line 1315, in pandas._libs.parsers.TextReader._string_convert
File "pandas\_libs\parsers.pyx", line 1553, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 1: invalid start byte
【问题讨论】:
试试:df = pd.read_csv(f, encoding='latin_1')
将 encoding='utf-8' 作为参数添加到 pd.read_csv()
@Erfan 感谢您的帮助!我进行了以下更改,但错误仍然存在。如果可能,请帮助我解决错误。
@AnubhavSingh 感谢您的帮助!我进行了以下更改,但错误仍然存在。如果可能,请帮助我解决错误。
@Amreeta Koner,如果可能,请将链接添加到 csv 文件
【参考方案1】:
您的 csv 文件中似乎有一个非 ascii 字符。我会查看答案here。希望对您有所帮助。
【讨论】:
嘿@Waleed S Khan,感谢您的帮助!那么代码中没有错误但是在csv文件中? 从您在问题中附加的错误看来,问题出在 csv 文件上,因为解析器无法解析非 ascii 字符。链接中的答案对您有帮助吗?【参考方案2】:#run the same code with little addon
pd.read_csv("C:/Users/Am/Documents/A.csv",header=0,encoding = "ISO-8859-1")
【讨论】:
感谢您的帮助!我进行了以下更改,但错误仍然存在。如果可能,请帮助我解决错误。以上是关于在python中合并CSV文件[重复]的主要内容,如果未能解决你的问题,请参考以下文章
使用Python Dictionary在Python中合并CSV文件