python - 使用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:没有要连接的对象**
解决方案
您正在使用 file_pattern = ".csv" 然后在 glob('*.{}'.format(file_pattern)) 中放置另一个点(。)
因此,您的程序正在搜索格式为 ..csv 的文件,这些文件显然不存在。
要修复它,您可以执行以下操作之一
1.)将 file_pattern 更改为“csv”(无点)或
2.)将字符串格式更改为 '*{}'.format(file_pattern)'(字符串中无点)
推荐阅读
- c# - 如何修复 Azure 登录尝试时的重定向?
- c# - 如何使用 C# mongo 驱动程序跟踪 mongo 命令
- security - 异常 ALPN 配置不正确
- shell - Notification for process that has been active for more than X time
- javascript - GitHub API 合并拉取请求(合并按钮)未找到响应
- php - Ubuntu 18 服务器使用 Wordpress 引发 Apache 错误
- r - 使用 PRACMA 与 Integral2 进行双重积分会给出错误 Fun(x, y, ...) 中的错误:缺少参数“c”,没有默认值
- jboss7.x - Drools 无法解析记录器
- java - Spring Boot Test MalformedURLException:未知协议:类路径
- ruby-on-rails - 在 Windows 上安装 Rails gem 时出错