如何連接27行?我得到「TypeError:無法連接'str'和'list'對象。」是否因爲我在符號列表中使用了變量變量?Python&Excel連接
這是我講的線:
ws.cell(1,i+1).value = "The price of" + symbolslist[i] + " is " + price
下面是我的代碼和行rewst 27
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)
're.findall()'返回一個列表。它抱怨'價格',而不是'symbolslist'。 –