告诉我如何替换python数据
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
如何阅读a.txt文件并按以下格式制作? 输出结果:
['abcd.com', 0],
['*', 66999306],
['asdf.com', 150744025],
['asfd.df.com', 193139033],
['fdsa.com', 907938122],
['bank.com', 2638989462],
['fire.com', 4151822166],
['ms.com', 7026079907]
答案 0 :(得分:0)
file = open('a.txt', 'r')
l = []
for line in file:
l.append( line.split())
然后,如果您希望第二部分为整数,则可以使用list comprehension:
l = [ [i[0], int(float(i[1]))] for i in l]
输出
[['abcd.com', 0],
['*', 66999306],
['asdf.com', 150744025],
['asfd.df.com', 193139033],
['fdsa.com', 907938122],
['bank.com', 2638989462],
['fire.com', 4151822166],
['ms.com', 7026079907] ]