TEXT MANIPULATION (in CSV format)
Bill Scherer
scherbi at bam.com
Thu Jun 15 10:11:25 EDT 2000
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
More information about the Python-list
mailing list