![](/img/trans.png)
[英]Why are the outputs not the same when it is either a list, int or string?
[英]Saving list in file as int and when reading why is it in string?
我正在保存一個 integer 列表來歸檔
number = [3808640804, 552553035, 815157969, 1623809649, 1851153805, 4058081409, 2438887622, 2833416221, 1727496343, 3172042750] txt = f"H:\data_set\\testing\\1K\chunk.txt" with open(txt, 'w', encoding='utf-8', errors='ignore') as filee: for line in number: filee.write(f"{line}\n")
但是當我使用腳本讀取文件時
txt = f"H:\data_set\\testing\\1K\chunk.txt" with open(txt, 'r', encoding='utf-8', errors='ignore', buffering=100000) as filee: g = filee.readlines() print(type(g[1])) print(g[0:10])
<class 'str'> ['3808640804\n', '552553035\n', '815157969\n', '1623809649\n', '1851153805\n', '4058081409\n', '2438887622\n', '2833416221\n', '1727496343\n', '3172042750\n']
而我應該得到這個 output:
[3808640804, 552553035, 815157969, 1623809649, 1851153805, 4058081409, 2438887622, 2833416221, 1727496343, 3172042750]
您可以嘗試使用以下代碼讀取文件嗎:
with open(txt, 'r') as filee:
numbers = [int(line.strip()) for line in filee]
print(numbers)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.