【问题标题】:Is there a better/cleaner way to format this script有没有更好/更干净的方法来格式化这个脚本
【发布时间】:2019-10-15 02:48:21
【问题描述】:

我对 python 还是很陌生(编写了 4 个脚本来清理文件夹和移动一些东西),现在我有了一个新的挑战。

我有这个 csv 文件,其中包含一些 pdf 的数据(pdf 的名称、需要打印的时间等)

我有以下代码 atm,一切正常,但我发现它“不干净”,我希望有更好的方法来格式化一些东西。 这是我的代码:

import csv, shutil, os

with open('\\\\fatboy.leleu.be\\Psm\\Mailing Templates\\OhGreen\\Verwerking\\OhGreen POS week 19.csv') as csvfile:
    csvReader = csv.reader(csvfile, delimiter=';')
    rows = csv.reader(csvfile)
    headers = next(rows)

    for row in csvReader:
        NAAM = row[0]
        Formaat = row[1]
        Tekstkleur = row[2]
        Gouvy = row[3]
        LouvainLaNeuve = row[4]
        Nannine = row[5]
        Tournai = row[6]
        SaintGeorges = row[7]
        Aarschot = row[8]
        Balen = row[9]
        Ekeren = row[10]
        Ninove = row[11]
        SintKatelijneWaver = row[12]
        Olen = row[13]
        Zwijndrecht = row[14]

        if Gouvy > "0":
            file = NAAM + ".pdf"
            path = ("\\\\fatboy.leleu.be\\Psm\\Mailing Templates\\OhGreen\\Verwerking\\" + Formaat + "\\" + file)
            dest = ("\\\\fatboy.leleu.be\\Psm\\Mailing Templates\\OhGreen\\Verwerking\\2print\\" + Formaat + "\\" + NAAM + "_GOUVY" + ".pdf")
            shutil.copy(path, dest)
            print(dest , " changed name and moved to ", dest)

        if LouvainLaNeuve > "0":
            file = NAAM + ".pdf"
            path = ("\\\\fatboy.leleu.be\\Psm\\Mailing Templates\\OhGreen\\Verwerking\\" + Formaat + "\\" + file)
            dest = ("\\\\fatboy.leleu.be\\Psm\\Mailing Templates\\OhGreen\\Verwerking\\2print\\" + Formaat + "\\" + NAAM + "_LOUVAIN_LA_NEUVE" + ".pdf")
            shutil.copy(path, dest)
            print(dest , " changed name and moved to ", dest)

        if Nannine > "0":
            file = NAAM + ".pdf"
            path = ("\\\\fatboy.leleu.be\\Psm\\Mailing Templates\\OhGreen\\Verwerking\\" + Formaat + "\\" + file)
            dest = ("\\\\fatboy.leleu.be\\Psm\\Mailing Templates\\OhGreen\\Verwerking\\2print\\" + Formaat + "\\" + NAAM + "_NANNINE" + ".pdf")
            shutil.copy(path, dest)
            print(dest , " changed name and moved to ", dest)

此代码适用于 csv 中的每个城市名称(总共 12 个) 我想寻找一种方法让这个更清洁。 就像我不需要为每个城市创建一个 if 循环,并为所有标题名称等创建一个循环。

欢迎任何提示。 PS:我不会使用 Pandas,因为我无法正确安装它,而且它在基本 python 中已经可以正常工作了

【问题讨论】:

  • 这个问题可能更适合codereview.stackexchange.com。不过,请务必先阅读他们的提交指南。

标签: python python-3.x csv shutil reader


【解决方案1】:

首先,将字符串存储为变量。您似乎经常重复使用相同的字符串,因此如果将其存储为变量 BASE_PATH,您可以执行以下操作:

BASE_PATH = "\\\\fatboy.leleu.be\\Psm\\Mailing\\Templates\\OhGreen\\Verwerking\\"
path = BASE_PATH + Formaat + "\\" + file

甚至

BASE_PATH = "\\\\fatboy.leleu.be\\Psm\\Mailing\\Templates\\OhGreen\\Verwerking\\{}\\{}"
path = BASE_PATH.format(Formaat, file)

其次,您可以将要检查的变量存储为字典并遍历其键。例如:

BASE_PATH = "\\\\fatboy.leleu.be\\Psm\\Mailing\\Templates\\OhGreen\\Verwerking\\{}\\{}"
DEST_PATH = "\\\\fatboy.leleu.be\\Psm\\Mailing\\Templates\\OhGreen\\Verwerking\\2print\\{}\\{}.pdf"
name_index_mapping = {
  "GOUVY": 3,
  "LOUVAIN_LA_NEUVE": 4,
  "NANNINE": 5
}

for (name, index) in name_index_mapping.items():
  if row[index] > "0":
    file = NAAM + ".pdf"
    path = BASE_PATH.format(Formaat, file)
    dest = DEST_PATH.format(Formaat, NAAM, name)
    shutil.copy(path, dest)
    print(path, "changed name and moved to", dest)

最后,您确定需要将该值与文字字符串"0" 进行比较,而不是与数字0 进行比较吗?使用greater/smaller than 逻辑比较字符串会产生意想不到的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 2018-03-30
    • 1970-01-01
    • 2022-09-30
    • 2011-03-02
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    相关资源
    最近更新 更多