user2311700
user2311700

Reputation: 17

printing specific cols in python txt file

I'm trying to read in my data stored in a text file containing 25 columns split up by, e.g

erd,thr,yui

I want to select the 4th and 13th columns and write them to a text file , side by side e.g

Mark , baseball

I have researched and found code which can do this for a single column but i cant get it working for two , does anyone know how to do this ?

Heres the code I was trying to use

col = 2 # third column
filename = '4columns.txt'
third_column = [line[:-1].split('\t')[col] for line in open(filename,
'r')]

Upvotes: 1

Views: 75

Answers (1)

svk
svk

Reputation: 5919

Python has a "csv" module (comma-separated values) which you can use for something like this.

import csv

with open( "myfile.txt", "r" ) as f:
    for row in csv.reader( f ):
        print row[3], ",", row[13]

Upvotes: 4

Related Questions