[Tutor] name shortening in a csv module output
Peter Otten
__peter__ at web.de
Thu Apr 23 10:42:40 CEST 2015
Jim Mooney wrote:
> I'm trying the csv module. It all went well until I tried shortening a
> long first name I put in just to exercise things. It didn't shorten.
> Original file lines:
> Stewartrewqrhjeiwqhreqwhreowpqhrueqwphruepqhruepqwhruepwhqupr|Dorsey|
nec.malesuada at Quisqueporttitoreros.com|Cariboo
> My result:
> Stewartrewqrhjeiwqhreqwhreowpqhrueqwphru Dorsey
It did shorten:
>>> len("Stewartrewqrhjeiwqhreqwhreowpqhrueqwphruepqhruepqwhruepwhqupr")
61
>>> len("Stewartrewqrhjeiwqhreqwhreowpqhrueqwphru")
40
> if len(line[0]) > 40: # problem here - didn't shorten
> line[0] = line[0][:40]
> print("{0:<20s} {1:<20s}".format(line[0], line[1]))
You have to make up your mind if you want to shortend to 40 or 20 chars.
> And I also got weird first characters on the header line. What went wrong?
> Original file lines:
>
> first_name|last_name|email|city|state or region|address|zip
...
> My result:
>
> Ï»¿First Name Last Name # odd characters on header line
...
Ï»¿
is the UTF-8 BOM (byte order mark) interpreted as Latin 1.
If the input is UTF-8 you can get rid of the BOM with
with open("data.txt", encoding="utf-8-sig") as csvfile:
...
More information about the Tutor
mailing list