【问题标题】:looping through folder of csvs python循环通过csvs python的文件夹
【发布时间】:2016-07-03 22:53:42
【问题描述】:

我一直在寻找一段时间,但我没有任何运气。这是我的问题:我有一个网络驱动器,里面装满了带有 CSV 子文件夹的文件夹。最终,这些 csv 需要导入到数据库中。基于结构,我希望从每一行中删除一行(每张表的第二行),并将其附加到一张新表中,以创建自己的表和表。不久前,我发现 Python 可以实现这一点。但是,我遇到了一些问题。我一步一步地做这件事,所以我不会因为不知道从哪里开始而感到不知所措。问题是我找到了所有的 CSV,但我无法打开每个 CSV 来读取任何行来写入文件。我一直在使用其他一些线程作为资源,但遇到了 IOError: [Errno 13] 权限被拒绝:“。” 来这里之前,我试图用尽所有选择,但现在我的时间不多了。我非常感谢您的帮助。

这是代码,从我玩了一段时间的 cmets 可以看出:

#!/usr/bin/python
import os
import csv
import sys

#output_file = sys.argv[1]
input_path = sys.argv[1] #I would pass a '.' here for current directory on the drive
#output_file = sys.argv[2]


def doWhatYouWant(line):
    print line
    return line 
    #let the function return, not only print, to get the value for use as below 

#filewriter = csv.writer(open(output_file,'wb'))
#This recursively opens opens .csv files and opens them

directory = os.path.join(input_path)
for root,dirs,files in os.walk(directory):
    for file in files:
       if file.endswith(".csv"):
           f=open(input_path, 'r')
           lines= f.readlines()
           f.close()

           #reader =csv.DictReader(f,delimiter=',')
           # writer = open("testsummary.txt",'wb')
           # writer = csv.writer(writer, delimiter=',')

           f=open(file.txt,'w')

           #for row in reader:
           #        writer.writerow(row[2])
            #   print(row[1])

           newline=doWhatYouWant(line)
           f.write(newline)
           f.close()


           #f.close()
           #print file

提前感谢大家的帮助。

【问题讨论】:

  • 你为什么有file.txt?你的意思是'file.txt'

标签: python loops csv recursion merge


【解决方案1】:

您必须计算文件的路径,例如:

for root, dirs, files in os.walk(directory):
    for file in files:
        if file.endswith(".csv"):
            f = open(os.path.join(root, file), 'r')

【讨论】:

    【解决方案2】:

    您收到IOError: [Errno 13] Permission denied: '.' 异常是因为您试图打开当前目录本身,就好像它是一个可读的文本文件:

    open(input_path, 'r')
    

    相反,您需要执行以下操作:

    open(os.path.join(root, file), 'r') 
    

    还可以考虑在打开文件时使用with。例如

    with open(filename, 'r') as f:
    

    【讨论】:

      【解决方案3】:

      我得到了一些帮助并进行了一些额外的研究并让代码正常工作。 感谢您所有的帮助。我相信我已经让它在我可以管理的地方工作。它将遍历目录中的所有文件夹,读取我想要的第二行的 csv,并将所有结果写入一张表。

      #!/usr/bin/python
      
      #imported modules that are used
      
      import csv
      import glob
      import os
      import sys
      import linecache
      
      
      # Where the two arguments that are passed when running the program are stored.   
      input_path = sys.argv[1] #input parameter accepted when running the program
      output_file = sys.argv[2] #output parameter is the name of the our put file 
      
      #The stored header for the .csv
      header = [] #Where I store the header to be printed later
      
      #Opens the output folder that will be written to with the .csv extension
      with open(output_file+'.csv','wb') as outfile:
          filewriter = csv.writer(outfile)
      #The header written here from the list created above ^
          filewriter.writerow(header)
      
      #The loop for the files found from the specified input that are named with a date and eyepiece information for a name
          for input_file in glob.glob(os.path.join(input_path,'*/[0-2]*.csv')):
              with open(input_file,'rU') as csv_file:
                  print "reading {:s}".format(input_file) #prints the names of the files processed to the console for a quick verification
                  reader_hopefully = csv.reader(csv_file, delimiter=',', quoting=csv.QUOTE_NONE)
                  next(reader_hopefully,None) #skips per line in the csv being read
                  row=next(reader_hopefully,None) #the row that is actually necessary stored in a variable
                  filewriter.writerow(row) #Writes necessary row to csv
                  csv_file.close() #Closes open csv when finished reading it
          outfile.close() 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-06
        • 1970-01-01
        • 1970-01-01
        • 2017-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多