Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R's read.table equivalent in Python

I'm trying to move some of my processing work from R to Python. In R, I use read.table() to read REALLY messy CSV files and it automagically splits the records in the correct format. E.g.

391788,"HP Deskjet 3050 scanner always seems to break","<p>I'm running a Windows 7 64 blah blah blah........ake this work permanently?</p>

<p>Update: It might have something to do with my computer. It seems to work much better on another computer, windows 7 laptop. Not sure exactly what the deal is, but I'm still looking into it...</p>
","windows-7 printer hp"

is correctly separated into 4 columns. 1 record can be split over many lines and there are commas all over the place. In R I just do:

read.table(infile, header = FALSE, nrows=chunksize, sep=",", stringsAsFactors=FALSE)

Is there something in Python that can do this equally well?

Thanks!

like image 861
mchangun Avatar asked Oct 23 '13 07:10

mchangun


1 Answers

You can use csv module.

from csv import reader
csv_reader = reader(open("C:/text.txt","r"), quotechar="\"")

for row in csv_reader:
    print row

['391788', 'HP Deskjet 3050 scanner always seems to break', "<p>I'm running a Windows 7 64 blah blah blah........ake this work permanently?</p>\n\n<p>Update: It might have something to do with my computer. It seems to work much better on another computer, windows 7 laptop. Not sure exactly what the deal is, but I'm still looking into it...</p>\n", 'windows-7 printer hp']

length of output = 4

like image 153
VIKASH JAISWAL Avatar answered Oct 13 '22 00:10

VIKASH JAISWAL