我试图使用pandas和os从选定的目录中获取CSV文件,并迭代地删除指定的行,因为它们是以我不喜欢的形式生成的。在迭代之后,我希望csv以相同的名称+后缀输出,以指示迭代已经执行。
在长时间的中断之后,我将回到Python,但这是我目前为止所做的:
DIR = 'Dummy_Folder/'
JV_suffix = "Current-Voltage Data.csv"
# For each file name in the chosen directory (DIR)
for filename in os.listdir(DIR):
# If file name ends with chosen suffix perform...
if filename.endswith(JV_suffix):
# Read the original csv file InputName
data = pd.read_csv(filename,header=None)
# Defining the rows that we want to delete from the file
trim = [0,1,2,len(data)-1,len(data)-2,len(data)-3]
# Deleting these rows and defining new csv
trim_data = data.drop(trim,axis=0)
# Making data frame with trimmed csv
df = pd.DataFrame(trim_data)
# Outputting df to CSV with suffix of '_number'
df.to_csv('DIR/filename_{}'.format(filename),index=False)
else:
continue
当我在虚拟数据上运行这个时,它抛出错误FileNotFoundError: [Errno 2] No such file or directory: 'Dummy_Current-Voltage Data.csv'
感谢您花时间阅读这篇文章!
可能是文件的相对路径错误。
我假定您的文件夹Dummy/
在python项目路径上。如果找到正确的文件夹,文件夹内任何文件的正确相对路径应该是Dummy/filename
。
您可以使用os.path.join(foldername,filename)
连接文件夹名称和文件名。
感谢qouify和Sam的建议,这解决了问题,同时将输出CSV行更改为df.to_csv('trimmed_{}'.format(filename),index=False)
。我认为在文件名中包含DIR会导致问题。我将整个脚本附在下面,作为具有类似查询的其他人的演示
import pandas as pd
import os, os.path
DIR = 'Dummy_Folder/'
# For each file name in the chosen directory (DIR)
for filename in os.listdir(DIR):
# If file name ends with chosen suffix perform...
if filename.endswith("Current-Voltage Data.csv"):
# Read the original csv file
data = pd.read_csv(os.path.join(DIR, filename),header=None)
# Defining the rows that we want to delete from the file
trim = [0,1,2,len(data)-1,len(data)-2,len(data)-3]
# Deleting these rows and defining new csv
trim_data = data.drop(trim,axis=0)
# Making data frame with trimmed csv
df = pd.DataFrame(trim_data)
df.to_csv('trimmed_{}'.format(filename),index=False)
else:
continue