gpt4 book ai didi

python - 如何从本地电脑加载多个csv文件

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:43 26 4
gpt4 key购买 nike

编辑/解决方案: 通过在 csv 中保留相同的分隔符并删除来解决 .joinpath('data')

我正在尝试使用示例 python 代码,我需要从本地 AD 加载多个 CSV 文件。

我尝试将文件加载到 datadir 中:

datadir = Path(r'C:\Users\russel_peters\Downloads\data').resolve().joinpath('data')

filenames = {
'spelling-bees': 'spelling-bee-winners.csv',
'deaths': 'cdc-causes-of-death.csv',
}

实际代码:

如何加载多个数据集的存储路径。

datadir = Path('.').resolve().joinpath('data')

filenames = {
'first-file': 'first-file.csv',
'second-file': 'second-file.csv',
}

filenames = {k: datadir.joinpath(v) for k, v in filenames.items()}

for fn in filenames.values():
assert fn.exists(), 'File not found: {}'.format(fn)


def standardize(s):
"""standardize column name."""
res = re.sub(r'\W+', '_', s.lower())
if res.endswith('_'):
res = res[:-1]
if res.startswith('_'):
res = res[1:]
return res

最佳答案

我建议使用 for 循环来断言文件是否存在。以 csv 格式打开文件并执行 csv 文件所需的任何功能。

for fn in filenames.values:
if fn.exists():
with open(fn, 'wb') as csvfile:
...

关于python - 如何从本地电脑加载多个csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59601633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com