【发布时间】:2019-05-23 14:47:00
【问题描述】:
我正在使用 pandas 读取多个文件,并且需要使用原始文件名_更正的文件名保存每个文件。如何使用原始文件名 + 前缀或数字重命名输出文件?
import pandas as pd
import glob
import os
#Read all files in using pandas
path = r'J:\Temp\\'
all_files = glob.glob(path + "/*.97o")
for filename in all_files:
df = pd.read_csv (filename)
df = df.replace(to_replace ='1997 7 23 ', value = '2019 5 23 ', regex = True)
df = df.replace(to_replace ='97 7 23', value = '19 5 23', regex = True)
df.to_csv('J:\Temp\94512040_corrected.97o', index=False)
应该调用输出文件:filename_corrected.97o
【问题讨论】:
-
镜头只需要附加到每个循环的文件名上
-
你可以使用
df.to_csv('J:\Temp\'+filename+'_corrected.97o', index=False) -
@Bugs404 干扰,但你的答案不起作用
标签: python pandas dataframe save