gpt4 book ai didi

Python2.7 : Syntax error while using print sheet values with a script

转载 作者:行者123 更新时间:2023-12-01 04:00:15 26 4
gpt4 key购买 nike

我正在尝试使用 Python 2.7 中的 XLRD 包打印一些单元格内容,并使用 Spyder IDE 创建脚本。我在 IDE 上使用命令 printsheet.cell_value() 时看到语法错误,而在 Windows 命令提示符下使用命令时似乎返回正确的值。我看到的错误消息如下:

print sheet.cell_value(0, col)
^
SyntaxError: invalid syntax

我的代码:

from __future__ import print_function
import xlrd

file_name="C:/Users/Documents/Python Learn/1.xlsx"
workbook=xlrd.open_workbook(file_name)
sheet=workbook.sheet_by_index(0)
NumberOfRows=sheet.nrows
NumberOfColumns=sheet.ncols
for col in range(sheet.ncols):
print sheet.cell_value(0, col)

最佳答案

你说from __future__ import print_function。这意味着 print 不再是语句,并且不带括号的 print ... 是无效语法。只需添加括号即可:

for col in range(sheet.ncols):
print(sheet.cell_value(0, col))

关于Python2.7 : Syntax error while using print sheet values with a script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36691979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com