简体   繁体   English

txt 文件到数组

[英]Txt file to arrays

I was wondering as I'm kinda new to python, how may I convert my txt file as :我想知道我对 python 有点陌生,我如何将我的 txt 文件转换为:

-20.92060089 -27.72520065 -27.55229950 
-20.92469978 -27.69650078 -27.55340004 
-20.92469978 -27.69890022 -27.55170059

To arrays thats kind :到数组那类:

[[-20.92060089, -27.72520065, -27.55229950],
 [-20.92469978, -27.69650078, -27.55340004],
 [-20.92469978, -27.69890022, -27.55170059]]

Each of these lines can be split on " ", and you can use a list comprehension to grab this all at once.这些行中的每一行都可以在“”上拆分,并且您可以使用列表理解来一次性获取所有内容。

I saved your text file as /tmp/tfile.txt , so then I can do this:我将您的文本文件保存为/tmp/tfile.txt ,然后我可以这样做:

>>> array = [ line.split() for line in open("/tmp/tfile.txt").readlines() ]
>>> array
[['20.92060089', '-27.72520065', '-27.55229950'], ['-20.92469978', '-27.69650078', '-27.55340004'], ['-20.92469978', '-27.69890022', '-27.55170059']]
>>> len(array)
3
>>> array[0]
['20.92060089', '-27.72520065', '-27.55229950']
>>> array[1]
['-20.92469978', '-27.69650078', '-27.55340004']
>>> array[2]
['-20.92469978', '-27.69890022', '-27.55170059']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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