0

I use openpyxl for my school Excel

Code is:

import openpyxl
import random

workbook = openpyxl.Workbook()  #创建工作簿
worksheet = workbook.active  #打开活动Sheet(0)
for row in range(1,55): #54行
    for column in range(1,16): #15列
        randomValue = random.choice([36.1,36.2,36.3,36.4,36.5,36.6,36.7,36.8,36.9]) #随机选择范围内数
        #随机生成一个数
        _ = worksheet.cell(row=row, column=column, value="{0}".format(randomValue)) #传入随机数值
        #填入单元格
        print(row,column,randomValue) #反馈当前进度
workbook.save("初二11班.xlsx") #保存

I am tired of that _, why use it? 为什么要用 _? 它有什么作用?我无法理解

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

2 Answers2

1

In the code you’ve posted, the assignment to _ serves no purpose whatsoever — remove it.

worksheet.cell(row=row, column=column, value="{0}".format(randomValue)) #传入随机数值

In general (but not here!), underscore variable names are used to indicate that a value needed to be assigned but isn’t used. But in your code there’s no need for any assignment, hence no need for the underscore.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

_ variable is used to store the return values which are not needed by the code.