How to tab seperated txt file to csv in python
Question: I have a txt file of 7 columns and about 8000 rows. the name of the file is
"Azimuth"

The first row- Id Xs Xe Ys Ye Length Azimuth

and each row under is a unique measurement. so this is a txt file with 8000
measurements. the data is tab separated or so it seems in the file itself.

when i open it with Excel it converts it to columns and rows with no problem,
but when i try to read it with python, it reads the whole text as one column.

i tried to run this code that I found:

    
    
    f=open("Azimuth.txt")
    lines=f.readlines()
    result=[]
    for x in lines:
          result.append(x.split(' ')[0])
    f.close()
    print result
    

when i run it, the it prints the whole txt file as one long row (or column,
i'm not sure). I tried to convert it to csv file but that separated the whole
txt by commas, not as i was expected.

anyone can help? i need to build a code that relies on that file separated to
columns and rows.

I'm not a programmer, so i'll mention i use sublime editor and run the script
with the terminal (it works properly, the script ends with .py and i used the
#!/usr/bin/env python command)

thanks everyone


Answer: Use the [csv](https://docs.python.org/3.5/library/csv.html) module, using
`tab` as the delimiter:

    
    
    import csv
    with open("Azimuth.txt") as f:
       rows = csv.reader(f, delimiter="\t")
       for row in rows:
          # do whatever
    

If you want a list of all the rows you can `rows = list(csv.reader(f,
delimiter="\t"))` but you can iterate a row at a time using the logic above.
You also never need to call `readlines()` unless you actually want a list, in
python you can iterate over the file object so if you were to parse manually,
you could:

    
    
    with open("Azimuth.txt") as f:
      rows = [line.split("\t") for line in f]
    

Or line by line:

    
    
     with open("Azimuth.txt") as f:
         for line in f:
           row = line.split("\t")
           # use row ...
    

