我有以下文件(克)..Python:如何讀取列表格式的文件?
-verifiziert.com | [1401832800]
00.pm | [1418511600, 1418598000, 1418943600]
00.re | [1410213600, 1417906800, 1418425200, 1419116400, 1418770800, 1417993200]
-verifizierungen.ne | [1401832800]
0.mk | [1414796400, 1415919600, 1417129200, 1416783600]
,我想放入d [域] =數字的字典。並且對於列表中的每個數字,我想要一個整數,因爲它當前是一個字符串。
我使用這個代碼:
d = defaultdict(list)
for line in g:
line = line.strip('\n')
domain, bl_dates= line.split('|')
bl_dates = [int(i) for i in bl_dates]
d[domain].append(bl_dates)
但我得到這個錯誤,好像列表不被識別爲一個列表:
Traceback (most recent call last):
File "test.py", line 12, in <module>
bl_dates = [int(i) for i in bl_dates]
ValueError: invalid literal for int() with base 10: '['
任何人可以幫助我嗎?
在SO應該包括你的代碼,你嘗試過什麼,你去哪裏失敗等... – alfasin 2015-02-09 09:42:40
@alfasin檢查更新 – UserYmY 2015-02-09 09:43:47
你是一個問題按'|'分割線字符,但我沒有看到任何'|'在你的文件上。 – danielfranca 2015-02-09 09:46:22