Search code examples
python-3.xreadlines

python 3.x readlines()


After a pause I've currently started to work with python again but right at the start I encountered an annoying (and at least for me not solvable...) problem. I want to open a normal .txt file with tabular content so I can iterate over specific 'columns' to gather all the information I need. The problem is that I don't get each line of the document as a list but instead python creates strings of each line.I also tried .readlines() but thats doesn't work either. I work on a Win7 PC and the code goes as followed:

with open('C:\\filepath...\\file.txt') as file:
 for f in file:
  print(f[0])

I also have to add that I also worked with python in the past and never encountered such problem so if anyone knows a solution I would really appreciate some help. Thank you in advance.


Solution

  • you just need to split:

    TheList = []
    with open('C:\\filepath...\\file.txt') as file:
        for f in file:
            TheList.append(f.split('\t'))