I'm not sure what exactly you're trying to do here, as it appears you're
trying to prepend each input row with the delimiter character, but I'm not
clear on what that is supposed to accomplish in the context of processing
"C/TSV" (I see Joshua hit on this as well).
As background, if you know the input format (in particular, what they're
using as a delimiter), I would take out the "sniffer" parts of the code and
just declare the delimiter when you instantiate the CSV reader.
reader = csv.reader(input, delimiter='\t')
and see if that does what you're expecting.
The output you've pasted suggests to me that the sniffer is getting
confused on your input data and instantiating a csv.Dialect object that
you're not expecting. A few quick tests suggest that it's pretty easy to
confuse the sniffer.
If you want see whether that's going on, you might output some diagnostic
information right after you sniff the dialect:
print "Dialect"
print "\tDelimiter: '%s' (%d)" % ( dialect.delimiter,
ord(dialect.delimiter) )
if dialect.escapechar:
print "\tEscape Char: '%s' (%d)"
%(dialect.escapechar,ord(dialect.escapechar))
else:
print "\tNo escape character for dialect" + unicode(dialect)
I bet 4 non-redeemable quatloos that if you add this, you'll see it's
detecting 'L' as the delimiter.
As to "escapechar", on both the input and output side refers to a character
that says the next character should not be given its normal meaning. You'd
use this in a CSV file if you might have a field that contains the
delimiter character (e.g. in a "true" CSV, a title might have a comma in
it). So escapechar='\'' means "use the single quote as an escape
character").
HTH,
AC
On Mon, Nov 25, 2013 at 12:10 PM, Bohyun Kim <[log in to unmask]> wrote:
> Hi all,
>
> I am new to Python and was wondering if I can get some help with my short
> script. What I would like the script to do is:
> (1) Read the tab delimited file generated by Refworks
> (2) Output exactly the same file but the blank column added in front.
> (This is for prepping the exported tab delimited file from refworks so
> that it can be imported into MySQL; so any suggestions in the line of
> timtoady would be also appreciated.)
>
> This is what I have so far. It works, but then in the output file, I end
> up getting some weird character in each line in the second column (first
> column in the original input file). I also don't really get what
> escapechar=' ' does or what I am supposed to put in there.
>
> import csv
> with open('noid_refworks.txt','rU') as csvinput:
> with open('withid.txt', 'w') as csvoutput:
> dialect = csv.Sniffer().sniff(csvinput.read(1024))
> csvinput.seek(0)
> reader = csv.reader(csvinput, dialect)
> writer = csv.writer(csvoutput, dialect, escapechar='\'',
> quoting=csv.QUOTE_NONE)
> for row in reader:
> writer.writerow(['\t']+row)
>
> A row in the original file is like this (Tab delimited and no quotations,
> some fields have commas and quotation marks inside.):
>
> Reference Type Authors, Primary Title Primary Periodical Full
> Periodical Abbrev Pub Year Pub Date Free From Volume Issue
> Start Page Other Pages Keywords Abstract Notes Personal
> Notes Authors, Secondary Title Secondary Edition Publisher
> Place Of Publication Authors, Tertiary Authors, Quaternary
> Authors, Quinary Title, Tertiary ISSN/ISBN Availability
> Author/Address Accession Number Language Classification Sub
> file/Database Original Foreign Title Links DOI Call Number
> Database Data Source Identifying Phrase Retrieved Date
> Shortened Title User 1 User 2 User 3 User 4 User 5 User
> 6 User 7 User 8 User 9 User 10 User 11 User 12 User 13
> User 14 User 15
>
> A row in the output file is like this:
> (The tab is successfully inserted. But I don't get why I have L inserted
> after no matter what I put in escapechar)
>
> LReference Type Authors, Primary Title Primary Periodical
> Full Periodical Abbrev Pub Year Pub Date Free From Volume
> Issue Start Page Other Pages Keywords Abstract Notes
> Personal Notes Authors, Secondary Title Secondary Edition
> Publisher Place Of Publication Authors, Tertiary Authors,
> Quaternary Authors, Quinary Title, Tertiary ISSN/ISBN
> Availability Author/Address Accession Number Language
> Classification Sub file/Database Original Foreign Title Links
> DOI Call Number Database Data Source Identifying Phrase
> Retrieved Date Shortened Title User 1 User 2 User 3 User 4
> User 5 User 6 User 7 User 8 User 9 User 10 User 11
> User 12 User 13 User 14 User 15
>
>
> Any help or pointers would be greatly appreciated!
> ~Bohyun
>
|