[英]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.