0
我想將csv文件中的日期轉換爲句子格式。例如:「2012年8月27日」到2012和8月27日,「2013年1月4日」 2013年1月4日,這是代碼我有:從csv文件轉換日期到句子格式
def getStockData(company):
baseurl = 'http://quote.yahoo.com/d/quotes.csv?s=GOOG&f=sl1d1t1c1ohgvj1pp2owern&e=.csv'
web_page = urllib.request.urlopen(baseurl)
lines = web_page.read().decode()
web_page.close()
read = csv.reader([lines])
row = next(read)
print('The last trade for', row[0], 'is $'+row[1], ', change rate is '+row[11], 'on '+row[2],'. The open was '+row[5],'and the previous close was '+row[10],'.')
行[2]指示的時間。任何想法如何實現這一目標?
看到https://docs.python.org/2/library/datetime.html#datetime.date.strftime – njzk2
好的,我決定使用row [2] .strftime('%B%d%Y' )在我的打印語句,但然後它給了我「TypeError:無法將'list'對象隱式轉換爲str',我需要轉換爲str的列表是什麼? – holaprofesor
@holaprofesor:'row [2]'是一個'str'(不知道爲什麼你會看到'list'),而'strftime'不是'str'的方法。你需要'datetime.date.strptime'來分析'str',然後'datetime.data.strftime'來以新的格式對其進行格式化。我不知道你爲了得到那個錯誤而做了什麼,因爲你給的代碼對這個錯誤沒有意義。 – ShadowRanger