0

questions:

.TXT:
194220.00   38.4397984  S   061.1720742 W   0.035
194315.00   38.4398243  S   061.1721378 W   0.036

Python:

myList = ('38.4397984,061.1720742','38.4398243,061.1721378')

Answer questions. How to take floats from a txt to a Python list as strings

code:

with open('haha.txt') as f:
    for line in f:
        words = line.split()
        print words
        my_list.append(words[1] + words[3])

My test code doesn't produce the desired result. What is wrong with it? I am missing the ,...

['38.4397984061.1720742', '38.4398243061.1721378']
Community
  • 1
  • 1
haha
  • 11
  • 1
  • 3

2 Answers2

0

Have you tried the very obvious:

my_list.append(words[1] + "," + words[3])

?

Btw, small remark: Maybe it would have been more reasonable to ask this as a comment to the answer you already accepted in the other thread instead of opening another question.

Jakob S.
  • 1,851
  • 2
  • 14
  • 29
  • Alas, what he wrote would not fit very good in a comment. Let alone the code, which cannot be formatted properly... – glglgl Jul 05 '12 at 07:37
0

I don't exactly know what you want, but I think

my_list.append((words[1], words[3]))

adds a tuple to my_list, so the result should be

[('38.43979840', '061.1720742'), ('38.43982430', '061.1721378')]

Instead, you could as well do

my_list.append((float(words[1]), float(words[3])))

to convert the strings representing numbers to numbers:

[(38.4397984, 61.1720742), (38.4398243, 61.1721378)]
glglgl
  • 89,107
  • 13
  • 149
  • 217