我编写了一个程序来垂直连接一些csv文件。这是程序。
import os
import glob
import pandas
def concatenate(indir, outfile, colnames):
os.chdir(indir)
fileList=glob.glob('*.csv')
dfList=[]
for filename in fileList:
print(filename)
df=pandas.read_csv(filename,header=None)
dfList.append(df)
concatDf=pandas.concat(dfList,axis=0)
concatDf.columns=colnames
y=str(input("What do you want to name your file?"))
concatDf.csv_path = y
concatDf.to_csv(outfile,index=None)
def main():
indir = str(input("What is the directory that you want to concatenate?" ))
outfile = str(input("What is the directory that you want to export the merged file to?" ))
string_input = input("What are the column names?: ")
input_list = string_input.split()
colnames = [str(x) for x in input_list]
concatenate(indir, outfile, colnames)
但是,当我测试我的程序时,会出现一个小错误。以下是我的意见。
main()
What is the directory that you want to concatenate?/Users/hem/Desktop/Complete_Pilot_Copy/5555_1/DelayDiscounting
What is the directory that you want to export the merged file to?/Users/hem/Desktop/DelayedDiscountingAnalyzed
What are the column names?: Date SubjectID SessionID ProtocolID SiteID TaskID UserResponse LogDiscountRate LogDiscountRateStd QuestionRule NegativeDiscountFlag ZeroDiscountFlag BozoDiscountFlag gldomain ProposedValue1 ProposedDelay1 ProposedValue2 ProposedDelay2 ProposedValue3 ProposedDelay3 TrialStartTimeSec ResponseTimeSec
DDT_5555_1_HUBS071501_BU_062016_135920.csv
DDT_5555_1_HUBS071501_BU_062016_140010.csv
DDT_5555_1_HUBS071501_BU_062016_140051.csv
DelayedDiscounting_5555_1.csv
What do you want to name your file?5555_1DelayDiscounting
Traceback (most recent call last):
File "<ipython-input-2-58ca95c5b364>", line 1, in <module>
main()
File "<ipython-input-1-867fad0a7568>", line 26, in main
concatenate(indir, outfile, colnames)
File "<ipython-input-1-867fad0a7568>", line 17, in concatenate
concatDf.to_csv(outfile,index=None)
File "/Users/hem/anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line 1344, in to_csv
formatter.save()
File "/Users/hem/anaconda/lib/python3.5/site-packages/pandas/formats/format.py", line 1526, in save
compression=self.compression)
File "/Users/hem/anaconda/lib/python3.5/site-packages/pandas/io/common.py", line 424, in _get_handle
f = open(path, mode, errors='replace')
IsADirectoryError: [Errno 21] Is a directory: '/Users/hem/Desktop/DelayedDiscountingAnalyzed'
我该如何解决这个问题?我想这可能是我进入目录的方式?感谢
答案 0 :(得分:1)
错误说几乎所有outfile
应该是文件的路径,而不是目录。所以不是这个
y=str(input("What do you want to name your file?"))
concatDf.csv_path = y
concatDf.to_csv(outfile,index=None)
这样做:
y=str(input("What do you want to name your file?"))
concatDf.to_csv(os.path.join(outfile, y),index=None)