简体   繁体   English

尝试从列表中的txt文件输入数据,然后创建一个列表,然后将值分配给各行

[英]Trying to input data from a txt file in a list, then make a list, then assign values to the lines

allt = []
with open('towers1.txt','r') as f:   
    towers = [line.strip('\n') for line in f]
    for i in towers:
        allt.append(i.split('\t'))
    print allt [0]

now i need help, im inputting this text 现在我需要帮助,即时输入此文本

mw91 42.927 -72.84 2.8
yu9x 42.615 -72.58 2.3
HB90 42.382 -72.679 2.4

and when i output im getting 当我输出即时消息

['mw91 42.927 -72.84 2.8']

where in my code and what functions can i use to define the 1st 2nd 3rd and 4th values in this list and all the ones below that will output, im trying 我在代码中的何处以及可以使用哪些函数来定义此列表中的第一,第二,第三和第四值,以及下面所有将输出的值,我正在尝试

allt[0][2] or 
allt[i][2] 

but that dosent give me -72.84, its an error, then other times it goes list has no attribute split 但那个剂量给我-72.84,这是一个错误,然后其他时候它没有列表拆分属性

update, maybe i need to use enumerate?? 更新,也许我需要使用枚举? i need to make sure though the middle 2 values im imputing though can be used and numbers and not strings because im subtracting them with math 我需要确保虽然可以使用中间2个值进行插补,但可以使用数字和数字而不是字符串,因为我用数学减去了它们

Are you sure those are tabs? 您确定这些是标签吗? You can specify no argument for split and it automatically splits on whitespace (which means you won't have to strip newlines beforehand either). 您不能为split指定任何参数,它会在空白处自动分割(这意味着您也不必事先去除换行符)。 I copied your sample into a file and got it to work like this: 我将您的样本复制到文件中,并使其工作如下:

allt = []
with open('towers1.txt','r') as f:   
   for line in f:
        allt.append(line.split())

>>>print allt[0]
['mw91', '42.927', '-72.84', '2.8']
>>>print allt[0][1]
'42.927'

Footnote: if you get rid of your first list comprehension, you're only iterating the file once, which is less wasteful. 脚注:如果您摆脱了对列表的第一个理解,则只需要对文件进行一次迭代,这样就减少了浪费。


Just saw that you want help converting the float values as well. 刚刚看到您还需要转换浮点值的帮助。 Assuming that line.split() splits up the data correctly, something like the following should probably work: 假设line.split()正确地分割了数据,则类似以下的内容应该可以工作:

allt = []
with open('towers1.txt','r') as f:   
   for line in f:
         first, *_else = line.split() #Python3
         data = [first]
         float_nums = [float(x) for x in _else]
         data.extend(float_nums)
         allt.append(data)

>>>print allt[0]
['mw91', 42.927, -72.84, 2.8]

For Python2, substitute the first, *_else = line.split() with the following: 对于Python2,将first, *_else = line.split()替换为以下内容:

first, _else = line.split()[0], line.split()[1:]

Finally (in response to comments below), if you want a list of a certain set of values, you're going to have to iterate again and this is where list comprehensions can be useful. 最后(响应下面的评论),如果您想要一组特定值的列表,则必须再次进行迭代,这对于列表理解很有用。 If you want the [2] index value for each element in allt , you'll have to do something like this: 如果您想要allt每个元素的[2]索引值,则必须执行以下操作:

 >>> some_items = [item[2] for item in allt]
 >>> some_items
 [-72.84, -72.58, -72.679]

[] implies a list. []表示一个列表。

'' implies a string. ''表示一个字符串。

allt = ['mw91 42.927 -72.84 2.8'] allt = ['mw91 42.927 -72.84 2.8']

allt is a list that contains a string: allt是一个包含字符串的列表:

allt[0] --> 'mw91 42.927 -72.84 2.8' allt [0]->'mw91 42.927 -72.84 2.8'

allt[0][2] --> '9' allt [0] [2]->'9'

allt.split() --> ['mw91', '42.927', '-72.84', '2.8'] allt.split()-> ['mw91','42 .927','-72.84','2.8']

allt.split()[2] --> '-72.84' #This is still a string. allt.split()[2]->'-72.84'#这仍然是一个字符串。

float(allt.split()[2]) --> -72.84 #This is now a float. float(allt.split()[2])-> -72.84#这是一个float。

I think this should also work 我认为这也应该起作用

with open('towers.txt', 'r') as f:
    allt = map(str.split, f)

And if you need the values after the first one to be floats... 并且如果您需要第一个浮点数之后的值,则为浮点数...

with open('towers.txt', 'r') as f:
    allt = [line[:1] + map(float, line[1:]) for line in map(str.split, f)]

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

相关问题 试图用txt数据制作一个列表数组,Python - Trying to make a list array with txt data, Python 从 txt 输入到列表的数据 - Python - Data from txt input to list - Python 从 txt 文件中读取行并创建一个字典,其中值是元组列表 - Reading lines from a txt file and create a dictionary where values are list of tuples 我正在尝试将随机数写入文件,然后从 Python 中的行创建一个列表,但列表也需要 '\\n' - I'm trying to write random numbers to a file and then make a list from lines in Python but list takes '\n' too 将值列表从txt文件转换为字典 - convert list of values from a txt file to dictionary Python - 从 txt 中读取空格和 | 分隔以将值添加到列表 - Python - Read lines from txt that are space and | delimited to add values to list 从 .txt 文件中获取数据并将其排序到列表中 - Getting data from .txt file and sort it into a list 我试图让机器人从 700 行的实际 txt 文件中发送 10 行文本 - I'm trying to make a bot send 10 lines of text from an actual txt file of 700 lines 我正在尝试将Python列表导出到.txt文件,但文本文件最终的行数太少 - I'm trying to export a Python list to a .txt file, but the text file ends up having too few lines 尝试从2个符号之间的txt文件中提取文本并存储在列表中 - Trying to extract text from a txt file between 2 symbols and store in a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM