简体   繁体   中英

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

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

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). 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:

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:

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:

 >>> 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 is a list that contains a string:

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

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

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

allt.split()[2] --> '-72.84' #This is still a string.

float(allt.split()[2]) --> -72.84 #This is now a 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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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