看板 Python 關於我們 聯絡資訊
我使用sax來做xml的parsing,以下為幾行關鍵程式碼 #取得xml結果後,把string存到theDate,並寫入parsed.txt elif self.CurrentData == "theDate": with open ("./parsed.txt",'a',encoding = 'utf-8') as f: f.write("theDate =" + self.theDate) f.write('\n') ======================== #前面有寫讀取parsed.txt,所以這邊單純就是把theDate寫成一個list elif line.startswith('theDate') == True: if len(line.strip().replace(u'theDate =', u'')) > 0: theDate_l.append(str(line.strip().replace(u'theDate=', u'')) + "") else: theDate_l.append("1960/01/01") ======================== 重點來了 theDate = datetime.datetime.strptime(theDate, '%Y-%m-%d') 這段我常常會出現ValueError,錯誤原因是殘缺的日期可能是: ValueError: time data '98-01-15' does not match format '%Y-%m-%d' 但是它的正確格式其實是'1998-01-15'之類的 有趣的是,出了這樣的錯誤之後,我直接重新跑那個日期的資料,結果是沒有失敗過, 也就是說一般抓因為某種因素可能會出錯,但是我目前完全找不到頭緒,我看 parsed.txt裡面紀錄的資料也都是正常的 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.251.144.85 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1571252987.A.947.html
outshaker: 輸入檔案的時候,要檢查 theDate 的格式對不對 10/17 07:53