TEXT MANIPULATION (in CSV format)

Emile van Sebille emile at fenx.com
Thu Jun 15 10:27:12 EDT 2000


You'll need to be more careful than Bill pointed
out in his reply.  If you know your source data
is clean, you should have no problems.  Otherwise,
proceed cautiously.  In particular, if you must parse
CSV, watch out for embedded commas, eg: name fields
like "Abe Lincoln, Pres" or address lines like
"123 Main, Apt C".  You'd be better off if you
could avoid CSV files by seeing if, for example,
tab delimited files could be provided instead.

It-can-be-parsed-I-just-don't-like-to-ly y'rs,

Emile van Sebille
emile at fenx.com
-------------------


----- Original Message -----
From: Bill Scherer <scherbi at bam.com>
To: Robin Porter <robinporter72 at yahoo.ca>
Cc: <python-list at python.org>
Sent: Thursday, June 15, 2000 7:11 AM
Subject: Re: TEXT MANIPULATION (in CSV format)


>
>
> Robin Porter wrote:
>
> > I am wondering how difficult it would be to use PYTHON
to go through a
> > CSV (Comma separated value) formatted file, take two of
the fields and
> > merge them into one field.
> >
> > An example would be:
> >
> > Field 1             Field 2
> >
> > "K"           ,       "1234"
> >
> > I want to change to:
> >
> > Field 1
> >
> > K1234
> >
>
> Easy:
>
> -----------------------
> #untested...
> import string
>
> split = string.split
> strip = string.strip
>
> filename = "/some/path/and/file.csv"
>
> newFile = open(filename + '.new', 'w')
>
> for line in open(filename).readlines():
>     f1, f2 = map(strip, split(line, ",",  1))
>     newFile.write("%s%s\n" % (f1, f2))
>
> newFile.close()
> -----------------------
>
> This will work fine if your original file is not huge.
> If your file is huge, you'll want do your readlines in
chuncks so as not
> to swamp memory.
>
>
>
> > Thanks in advance for your assistance.
>
> Your welcome.
>
>
> --
> William K. Scherer
> Sr. Member of Applications Staff
> Verizon Wireless
>
>
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list