Login  Register

Re: Update: Can't read .csv from Google Analytics into SPSS

Posted by Albert-Jan Roskam on Apr 07, 2011; 7:39am
URL: http://spssx-discussion.165.s1.nabble.com/Multiple-imputation-tp1075737p4287919.html

So you want to convert a csv file from one csv dialect into another? Yep, the Python csv module can do that. It's a built-in module; no need to download anything. Then you might still be faced with the encoding issue. I find such issues alwasy a bit obscure and difficult. There is no fail safe way to know whether the file is in unicode or not, and what byte order it has (although a b.o.m., if present, helps). Something like this:
 
import csv, contextlib
f_in = open("d:/my_nasty_csv.csv", "rb")
f_out = open("d:/my_slick_and_neat_csv.csv", "wb")
with contextlib.nested(f_in ,  f_out) as (infile, outfile):
  writer = csv.writer(outfile)
  for line in csv.reader(infile):
     writer.writerow([unicode(item).encode("utf-8") for item in line])
 
You can also catch the encoding error (if it occurs) and print on which line it occurs to find out what character is the culprit.
 
Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: Rick Oliver <[hidden email]>
To: [hidden email]
Sent: Tue, April 5, 2011 9:40:48 PM
Subject: Re: [SPSSX-L] Update: Can't read .csv from Google Analytics into SPSS

Python has a CSV module. Don't know if it or something else in Python can address your problem. But if it can be solved with list manipulation, then you can do it Python, since it brings in the whole files as a list (or a tuple, I forget which).



From:        Ruben van den Berg <[hidden email]>
To:        [hidden email]
Date:        04/05/2011 02:31 PM
Subject:        Update: Can't read .csv from Google Analytics into SPSS
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Dear all,

I just discovered that I get a warning when I try to save the .xls file as an .xls file. It says something like "the file possibly contains functions that are not compatible with Unicode text". When I persist and save it anyway, I can read it into SPSS normally afterwards.

So from here, I'm off-topic since this has now become an .xls question in an SPSS mailing list. But if anybody knows how to "convert" or "adapt" a whole bunch of such nasty .csv files like this (perhaps with Python?), I'd be very grateful!

Thanks again,

Ruben