0

I have a .txt file with data arranged as follows:

3430 4735 1
3430 4736 1
3430 4941 2
3430 5072 1
3430 5095 1
3430 5230 1
3430 5299 1
3430 5386 1
3430 5552 1
3430 5555 1
3430 5808 1
3430 5853 1
3430 5896 1
3430 5988 1
3430 6190 4
3430 6191 1
3430 6225 1
3430 6296 1

How can I create Python lists from this, one containing the numbers from the first column, and the other containing the numbers from the second column?

user8321044
  • 3
  • 1
  • 1
  • 3

4 Answers4

1

Take a look on pandas library, it's very useful for data flow. http://pandas.pydata.org/

Or you can do this directly :

list1 = []
list2 = []
list3 = []
with open('test.txt', 'r') as f:
    content = f.readlines()
    for x in content:
        row = x.split()
        list1.append(int(row[0]))
        list2.append(int(row[1]))
        list3.append(int(row[2]))
Jérémy Caré
  • 315
  • 3
  • 12
0
one_list = []
another_list = []
with open("somefile.txt", "r") as this_file:
    for line in this_file:
        one_list.append(line.split(' ')[0])
        another_list.append(line.split(' ')[1])

Puts the first elements from each line in the file into one list and all the second elements into another list.

TLOwater
  • 638
  • 3
  • 13
0

Something like this?

flst = []
slst = []
tlst = []

file = open('your.txt', 'r')
for line in file:
    a = line.split()
    flst.append(int(a[0]))
    slst.append(int(a[1]))
    tlst.append(int(a[2]))

Create three empty lists, open the file and read through every line of the txt-file. Then split the lines and add in every line the first, second and third string to a respective list.

trotta
  • 1,232
  • 1
  • 16
  • 23
0

list1 = []

list2 = []
whith open('a.txt','r') as fh:
  lineList = fh.readlines()
  for line in listList:
    numbers = line.strip().split()
    list1.appned(numbers[0])
    list2.extend(numbers[1:])

print(list1)
print(list2)
Fuji Komalan
  • 1,979
  • 16
  • 25