如何将文本文件转换为csv当文本文件名是从其他一些文件读取?

olhwl3o2  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(89)

我有一个文件“list_csv”,其中包含以下文件名:
hierarchy1.rpt hierarchy2.rpt
我想阅读转换上述rpt文件到csv。
我尝试了以下代码:file2 = open('list_csv ',' r')Lines1 = file2.readlines()for line in Lines1:dataframe1 = pd.read_csv(line)read_file.to_csv(line)
但它出错了:文件未找到错误:[错误2]文件b'hierarchy2.rpt\n'不存在:b'hierarchy1.rpt\n'

gblwokeq

gblwokeq1#

import pandas as pd

# Read the list of file names
with open('list_csv', 'r') as file2:
    Lines1 = file2.readlines()

# Loop through the file names
for line in Lines1:
    # Remove newline character
    line = line.strip()
    
    # convert it to CSV
    dataframe1 = pd.read_csv(line)
    
    # Save as a CSV file
    dataframe1.to_csv(line.replace('.rpt', '.csv'), index=False)

相关问题