【问题标题】:Is it possible to summarize repetitive code in python?是否可以在python中总结重复代码?
【发布时间】:2023-02-25 02:23:01
【问题描述】:

我一直在尝试在 python 中创建一个应用程序,您可以在其中为文本选择字体,为此我有一个 excel,其中我有 347 个单元格,每个单元格都有一个字体名称。问题是我必须提取每个单元格的信息并将其存储在一个变量中,这非常慢。

为此,我使用了以下代码:


# Excel file with all the fonts
fonts_excel = 'Fonts.xlsx'

workbook = load_workbook(fonts_excel) # Load the entire workbook.

worksheet = workbook['Sheet1'] # Load the worksheet in which the fonts are on
all_rows = list(workbook.rows) # Load all the rows of thw worksheet

# Load all the cells
for cell in all_rows[1]:
    font1 = cell.value # Cell 1
for cell in all_rows[2]:
    font2 = cell.value # Cell 2
for cell in all_rows[3]:
    font3 = cell.value # Cell 3
for cell in all_rows[4]:
    font4 = cell.value # Cell 4
for cell in all_rows[5]:
    font5 = cell.value # Cell 5
for cell in all_rows[6]:
    font6 = cell.value # Cell 6
for cell in all_rows[7]:
    font7 = cell.value # Cell 7
for cell in all_rows[8]:
    font8 = cell.value # Cell 8
for cell in all_rows[9]:
    font9 = cell.value # Cell 9
for cell in all_rows[10]:
    font10 = cell.value # Cell 10
for cell in all_rows[11]:
    font11 = cell.value # Cell 11
for cell in all_rows[12]:
    font12 = cell.value # Cell 12
for cell in all_rows[13]:
    font13 = cell.value # Cell 13
for cell in all_rows[14]:
    font14 = cell.value # Cell 14
for cell in all_rows[15]:
    font15 = cell.value # Cell 15
for cell in all_rows[16]:
    font16 = cell.value # Cell 16
for cell in all_rows[17]:
    font17 = cell.value # Cell 17
for cell in all_rows[18]:
    font18 = cell.value # Cell 18
for cell in all_rows[19]:
    font19 = cell.value # Cell 19
for cell in all_rows[20]:
    font20 = cell.value # Cell 20
for cell in all_rows[21]:
    font21 = cell.value # Cell 21
for cell in all_rows[22]:
    font22 = cell.value # Cell 22
for cell in all_rows[23]:
    font23 = cell.value # Cell 23
for cell in all_rows[24]:
    font24 = cell.value # Cell 24
for cell in all_rows[25]:
    font25 = cell.value # Cell 25
for cell in all_rows[26]:
    font26 = cell.value # Cell 26
for cell in all_rows[27]:
    font27 = cell.value # Cell 27
for cell in all_rows[28]:
    font28 = cell.value # Cell 28
for cell in all_rows[29]:
    font29 = cell.value # Cell 29
for cell in all_rows[30]:
    font30 = cell.value # Cell 30

我想要的是能够放置 347 个源,而不必执行整个 for cell in all_rows[1]... 过程。这可能吗?谢谢。

【问题讨论】:

  • 使用一个数组作为字体,然后使用一个循环来填充数组?
  • 为什么要像for cell in all_rows[10] 一样遍历行,然后像font10 = cell.value 一样将单元格值一遍又一遍地分配给同一个变量?这只是将 all_rows[10] 行的最后一个单元格值分配给该变量。也许这就是您最终想要的,但反复设置值然后覆盖它们是低效且没有意义的。
  • @DaveS 我该怎么做?我是 python 的新手,我真的不知道该怎么做。
  • @RandomDavis 我应该怎么做才能不这样做?
  • 也许你应该解释一下你打算如何使用你稍后在代码中捕获的字体......否则很难知道你真正需要什么......

标签: python


【解决方案1】:

如果您想要与您的代码等效的代码,最简单的方法是将字体存储在列表中。但是您的代码的工作方式是循环遍历每一行中的每个单元格,并将变量的值设置为每个单元格的值,覆盖该变量的先前值,直到循环中到达该行中的最后一个单元格。

因此,假设您实际上希望每行中的最后一个单元格值是字体值,您只需访问每行中最后一个单元格的索引,如:

fonts = []

for row in all_rows:
    fonts.append(row[-1]) # [-1] is the last item's index

或者,如果你想导入每一行中的每个单元格,你会这样做:

fonts = []

for row in all_rows:
    for cell in row:
        fonts.append(cell)

我不太清楚你在要求什么,但希望这足够清楚,仍然可以帮助你。如果您共享示例数据,那么它会使您尝试做的事情变得更加明显。


如果你想将它们存储在 dict 中以便你可以通过名称引用它们,你可以这样做:

fonts = {}

for i in range(len(all_rows)):
    fonts['font' + str(i)] = row[-1]

这样,您就可以访问fonts['font1'] 之类的字体。但我真的不认为这样做有什么意义。像 fonts[1] 一样访问它们要容易得多。

【讨论】:

  • 这或多或少是我想要的,但我想将每个单元格保存在一个变量中,这样我就可以例如做print(font1),我不知道你是否理解我的意思。
  • 你可以那样做print(fonts[1])。动态创建和引用数百个变量是一种糟糕的做法,并且是一种反模式。不过你可以使用dict,我会写一个例子。但它仍然没有任何意义或对你有任何帮助。
  • 如果您只是按照建议将字体保存到一个数组中,您可以通过执行print(fonts[11])之类的操作来打印 font12 ...
  • fontDict = {f'font{rowIdx}':all_rows[rowIdx][-1] for rowIdx in range(len(all_rows))}
  • 非常感谢,在您的帮助下,我制作了一个包含所有字体列表的变量,正如您所说,现在我可以轻松地 print(fonts_list[23]) 并显示该行中字体的名称。
猜你喜欢
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 2011-10-24
  • 2013-09-17
  • 2013-02-09
  • 2017-10-22
  • 2013-04-30
  • 2010-09-18
相关资源
最近更新 更多