0

我正在尝试让我的代码写入到单元格A1、、A2和Excel 中A3A4但是,当我使用它时,我收到此错误消息

无效的单元格坐标 (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。

4

2 回答 2

2

EDIT

Your i=0 on the first pass but there is no zero column. Also, I think the cell is looking for row index which is a number not a character 'A'.

ws.cell(1,i+1).value =  "The price of" + symbolslist[i] + " is " + price 
于 2013-10-05T22:45:57.907 回答
0

使用 ws.cell().value 是不正确的。我有错误说'int'对象没有属性'replace',但是在我将行更改为以下内容后,错误消失了: ws.cell(row=1,column=i+1).value = "The price of" + str(symbolslist[i]) + " 是 " + str(price)

顺便说一句,我使用的是 Python 2.7 和最新版本的 openpyxl

于 2013-10-06T02:34:57.777 回答