kyoungwon cho kyoungwon cho -3 years ago 54
Python Question

Tell me how to replace python data, How can I read the a.txt file and make it in the following format?

Tell me how to replace python data

a.txt

abcd.com 0.0
* 6.6999306E7
asdf.com 1.50744025E8
asfd.df.com 1.93139033E8
fdsa.com 9.07938122E8
bank.com 2.638989462E9
fire.com 4.151822166E9
ms.com 7.026079907E9


How can I read the a.txt file and make it in the following format?
Output result :

['abcd.com', 0],
['*', 66999306],
['asdf.com', 150744025],
['asfd.df.com', 193139033],
['fdsa.com', 907938122],
['bank.com', 2638989462],
['fire.com', 4151822166],
['ms.com', 7026079907]

Answer Source
file = open('a.txt', 'r')

l = []
for line in file:
    l.append( line.split())

Then if you want the second part to be integer, you can use list comprehension:

l = [ [i[0], int(float(i[1]))] for i in l]

output

[['abcd.com', 0], 
['*', 66999306], 
['asdf.com', 150744025], 
['asfd.df.com', 193139033], 
['fdsa.com', 907938122], 
['bank.com', 2638989462], 
['fire.com', 4151822166],
['ms.com', 7026079907] ]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download