使用Python将文件路径写入新的csv?

时间:2018-02-20 20:34:44

标签: python csv parsing for-loop

我是python的新手并尝试从文件夹中的文件列表创建csv文件。本质上,代码应该基于文件夹中的文件来写行,通过解析文件名来隔离唯一标识符(这里是城市的名称),然后将该标识符写入列,然后是原始文件附件的名称/文件路径。这段代码写入标题," City"和"附件"然后停止并返回错误声明,文件夹中没有PDF(事实上,文件夹中有100个PDF文件)。

以下是我编辑时遇到问题的代码:

attachments_folder = "H:/Attachments"
attachments_table = attachments_folder + "\\" + "attachments_Table.csv"

for f in os.listdir(attachments_folder):
    file_name, file_ext = os.path.splitext(f)
    f_file, f_city = file_name.split('_')
    writer = csv.writer(open(attachments_table, "wb"), delimiter=",")
    writer.writerow(["City", "Attachment"])
    if str(f).find(".pdf") > -1:
        writer.writerow([f_city, f])
    else:
        print "Error: no PDF's found"
我提前道歉,这可能是一个笨拙和/或令人讨厌的代码。我很好奇,如果我需要打破这里发生的两件事(解析文件名,然后写行到csv行),但是我在这个重新格式化的版本中遇到了语法错误:

for f in os.listdir(attachments_folder):
    file_name, file_ext = os.path.splitext(f)
    f_file, f_city = file_name.split('_')

writer = csv.writer(open(attachments_table, "wb"), delimiter=",")
writer.writerow(["City", "Attachment"])

for f in os.listdir(attachments_folder):
    writer.writerow(["City", "Attachment"])
    if str(f).find(".pdf") > -1:
        writer.writerow([f_city, f])
    else:
        print "Error: no PDF's found"

对于我在这里失踪的任何指导都将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

这里有几个问题:

  1. 最重要的是,你在循环的每一步都覆盖了文件,从而删除了之前步骤中可能写入的内容。
  2. 其次,可能会增加混乱,你的警告并没有说实话。可能已找到一个或多个PDF。
  3. 我通过在循环之前只打开一次文件来解决第一个问题。我通过修改现有警告并在找到PDF时添加另一个正面状态修复了第二个问题。这是你的程序的重写(在Python 3中):

    # In Python 3, newline='' prevents extra blank rows. You don't need this in Python 2.
    # You can also leave 'wb' as the file mode in Python 2.
    with open(attachments_table, 'w', newline='') as output:
        writer = csv.writer(output, delimiter=",")
        writer.writerow(['City', 'Attachment'])
    
        for f in os.listdir(attachments_folder):
            file_name, file_ext = os.path.splitext(f)
            f_file, f_city = file_name.split('_')
            if str(f).find('.pdf') > -1:
                print('The current file is a PDF.')
                writer.writerow([f_city, f])
            else:
                print('The current file is not a PDF.')