Python迭代写入Excel

我试图让我的代码写入Excel中的单元格A1A2A3A4 。 但是,当我使用它,我得到这个错误消息

无效单元坐标(A)

“A”保持不变,但是“I”随着每次重复而改变。 我将如何解决27号线的问题? 我想让Python接受“A”作为列引用。

这是第27行:

 ws.cell('A',i).value = "The price of", symbolslist[i], " is ", price" 

和我的整个Python脚本:

 from openpyxl import Workbook import urllib import re from openpyxl.cell import get_column_letter wb = Workbook() dest_filename = r'empty_book.xlsx' ws = wb.create_sheet() ws.title = 'Stocks' symbolslist = ["aapl","spy","goog","nflx"] i=0 while i<len(symbolslist): #counts each object as 1 in the list url = "http://finance.yahoo.com/q?s="+symbolslist[i]+"&q1=1" htmlfile = urllib.urlopen(url) htmltext = htmlfile.read() regex = '<span id="yfs_l84_'+symbolslist[i]+'">(.+?)</span>' pattern = re.compile(regex) price = re.findall(pattern,htmltext) print "The price of", symbolslist[i], " is ", price ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + price i+=1 wb.save(filename = dest_filename) 

我也使用这个作为参考 ,从openpyxl。

编辑

第一遍你的i = 0,但是没有零列。 另外,我认为这个单元格正在寻找一个不是“A”字符的行索引。

 ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + price 

这是使用ws.cell()。值是不正确的。 我有错误说'int'对象没有属性'replace',但是在我把这行改为下面的代码后,错误消失了:ws.cell(row = 1,column = i + 1).value =“The price “+ str(symbolslist [i])+”是“+ str(价格)

顺便说一下,我正在使用Python 2.7和最新版本的openpyxl