简体   繁体   中英

Saving list in file as int and when reading why is it in string?

I am saving a integer list to file with

 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")

But When I read the file using script

 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])
I get this output:

 <class 'str'> ['3808640804\n', '552553035\n', '815157969\n', '1623809649\n', '1851153805\n', '4058081409\n', '2438887622\n', '2833416221\n', '1727496343\n', '3172042750\n']

Whereas I should get this output:

 [3808640804, 552553035, 815157969, 1623809649, 1851153805, 4058081409, 2438887622, 2833416221, 1727496343, 3172042750]

Can you try reading the file with this code:

with open(txt, 'r') as filee:
numbers = [int(line.strip()) for line in filee]
print(numbers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM