【问题标题】:The translation result should be in Column B in the same excel sheet (fruits.xlsx)翻译结果应在同一个 Excel 表 (fruits.xlsx) 的 B 列中
【发布时间】:2021-01-20 12:24:20
【问题描述】:
  1. 以下代码翻译位于 A 列中的 fruits.xlsx 中的单词。
  2. 我希望输出/结果应该在 B 列中
import xlrd
import goslate

loc = r"C:\path\fruits.xlsx"
gs = goslate.Goslate()

wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)
  
for i in range(sheet.nrows): 
    print(gs.translate(sheet.cell_value(i, 0), 'de'))
    print(sheet.cell_value(i, 1)

我收到以下错误

 return self._cell_values[rowx][colx]
IndexError: list index out of range

请有人帮我将我的输出/结果写在 B 列中的相同 excel 中

【问题讨论】:

    标签: python xlrd goslate


    【解决方案1】:
    for i in range(sheet.nrows - 1)
    

    Python 索引从 0 开始,所以最后一行是 sheet.nrows - 1

    确保在最后一个打印语句中也添加右括号

    print(sheet.cell_value(i, 1))
    

    【讨论】:

    • 这段代码出现错误import xlrd import goslate loc = r"C:\path\fruits.xlsx" gs = goslate.Goslate() wb = xlrd.open_workbook(loc) sheet = wb.sheet_by_index(0) for i in range(sheet.nrows - 1): print(gs.translate(sheet.cell_value(i, 0), 'de')) print(sheet.cell_value(i, 1) 错误是:SyntaxError: unexpected EOF while parsing
    • @kumar print(sheet.cell_value(i, 1) 应该是 print(sheet.cell_value(i, 1))
    猜你喜欢
    • 2018-07-05
    • 2014-08-25
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多