Search code examples
pythonlistimportpython-importgenfromtxt

Import data into Python from txt file


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?


Solution

  • 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]))