首页 > 解决方案 > 使用python组合csv时的值错误

问题描述

Python新手在这里,第一次海报:

尝试合并 csvs。我已将所有文件放在一个文件夹中。试图将它们组合成 1 个 csv。

from os import chdir
from glob import glob
import pandas as pdlib

# Produce a single CSV after combining all files
def produceOneCSV(list_of_files, file_out):
   # Consolidate all CSV files into one object
   result_obj = pdlib.concat([pdlib.read_csv(file) for file in list_of_files])
   # Convert the above object into a csv file and export
   result_obj.to_csv(file_out, index=False, encoding="utf-8")

# Move to the path that holds our CSV files
csv_file_path = 'C:/GolfPython/'
chdir(csv_file_path)
print(csv_file_path)
# List all CSV files in the working dir
file_pattern = ".csv"
list_of_files = [file for file in glob('*.{}'.format(file_pattern))]
print(list_of_files)

file_out = "ConsolidateOutput.csv"
produceOneCSV(list_of_files, file_out)`

使用我在网上找到的代码。

收到以下错误:** 文件“C:\Users\dsitar\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\reshape\concat.py”,第 262 行,在init raise ValueError('没有要连接的对象')

ValueError:没有要连接的对象**

标签: pythonpandas

解决方案


您正在使用 file_pattern = ".csv" 然后在 glob('*.{}'.format(file_pattern)) 中放置另一个点(。)

因此,您的程序正在搜索格式为 ..csv 的文件,这些文件显然不存在。

要修复它,您可以执行以下操作之一

1.)将 file_pattern 更改为“csv”(无点)或
2.)将字符串格式更改为 '*{}'.format(file_pattern)'(字符串中无点)


推荐阅读