Where is the syntax for the dict() constructor ?!

  • Thread starter Captain Poutine
  • Start date
J

John Machin

That's how Excel and the `csv` module do it.



Why don't you try yourself? The `csv` module returns two records, the
first has six items:

1: erik
2: viking
3: ham
4: spam and eggs
5: He said "Ni!"
6: line one

'line two' is the only item in the next record then.

The rules for quoting when writing can be expressed as:
def outrow(inrow, quotechar='"', delimiter=','):
out = []
for field in inrow:
if quotechar in field:
field = quotechar + field.replace(quotechar, quotechar*2) +
quotechar
elif delimiter in field or '\n' in field:
# See note below.
field = quotechar + field + quotechar
out.append(field)
return delimiter.join(out)

Note: characters other than delimiter and \n can be included in the
"to be quoted" list.

What readers do with data that can *not* have been produced by a
writer following the rules can get worse than BlackJack's example.

Consider this: file nihao1.csv contains the following single line:
'Is the "," a mistake in "Ni, hao!"?\r\n'

Openoffice.org's Calc 2.1 shows the equivalent of
['Is the "', ' a mistake in Ni', ' hao!"?\n'] in a Text Import window,
but then silently produces nothing. A file with two such lines causes
5 fields to be shown in the window -- it apparently thinks the
newlines are inside quoted fields!

Gnumeric 1.7.6 silently produces the equivalent of
result = ['Is the "', ' a mistake in ', 'hao!"?']
map(len, result) -> [8, 14, 6]
What happened to Ni?
Multiple such lines produce multiple rows.

Excel 11.0 (2003) silently produces in effect
result = ['Is the "', ' a mistake in Ni', ' hao!"?']
map(len, result) -> [8, 16, 7]
Multiple such lines produce multiple rows.

The csv module does what Excel does.

Consumers of csv files are exhorted to apply whatever sanity checks
they can. Examples:
(1) If the csv file was produced as a result of a database query, the
number of columns should be known and used as a check on the length of
each row received.
(2) A field containing an odd number of " characters (or more
generally, not meeting whatever quoting convention might be expected
in the underlying data) should be treated with suspicion.

Cheers,
John
 
H

Hendrik van Rooyen

8<------------ nice explanation of quoting problems -------------
(2) A field containing an odd number of " characters (or more
generally, not meeting whatever quoting convention might be expected
in the underlying data) should be treated with suspicion.

How does one program to treat something with suspicion?

My stuff tends to either accept or reject...

This reminds me (for no discernible reason) of the word
"eschew", and of an illustration in a Goon author's book
of someone eschewing an architectural example with a
pocket sized eschewing instrument - a "pocket eschewer".
(He is shown peering at it through the eyepiece)

Maybe such fields should be eschewed instead.

- Hendrik
 
S

Steve Holden

Hendrik said:
8<------------ nice explanation of quoting problems -------------


How does one program to treat something with suspicion?

My stuff tends to either accept or reject...

This reminds me (for no discernible reason) of the word
"eschew", and of an illustration in a Goon author's book
of someone eschewing an architectural example with a
pocket sized eschewing instrument - a "pocket eschewer".
(He is shown peering at it through the eyepiece)

Maybe such fields should be eschewed instead.
Would we do that with esteeth?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
H

Hendrik van Rooyen

Steve Holden said:
Would we do that with esteeth?

Ok Steve you've got me - my dictionary goes from
estate to esteem to ester...

The US spelling of "esthete" may have a bearing...

- Hendrik
 
S

Steve Holden

Hendrik said:
Ok Steve you've got me - my dictionary goes from
estate to esteem to ester...

The US spelling of "esthete" may have a bearing...

- Hendrik

Sorry - dreadful joke. Since teeth chew, I wondered whether esteeth
might eschew. [Graon ...]

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
H

Hendrik van Rooyen

Steve Holden said:
Sorry - dreadful joke. Since teeth chew, I wondered whether esteeth
might eschew. [Graon ...]

*grin*

*Wonders if he can extend this troll to get Steve to explain what "teeth" are.*

; - )

- Hendrik
 
S

Steve Holden

Hendrik said:
Steve Holden said:
Sorry - dreadful joke. Since teeth chew, I wondered whether esteeth
might eschew. [Graon ...]

*grin*

*Wonders if he can extend this troll to get Steve to explain what "teeth" are.*

; - )

- Hendrik
Bite me :)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top