Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit</strong>: since the <code>csv</code> module doesn't deal as desired with quotes <em>inside</em> fields, it takes a bit more work to implement this functionality:</p> <pre><code>import re quoted = re.compile(r'"[^"]*"') class QuoteSaver(object): def __init__(self): self.saver = dict() self.reverser = dict() def preserve(self, mo): s = mo.group() if s not in self.saver: self.saver[s] = '"%d"' % len(self.saver) self.reverser[self.saver[s]] = s return self.saver[s] def expand(self, mo): return self.reverser[mo.group()] x = 'name="John Smith", age=34, height=173.2, location="US", avatar=":,=)"' qs = QuoteSaver() y = quoted.sub(qs.preserve, x) kvs_strings = y.split(',') kvs_pairs = [kv.split('=') for kv in kvs_strings] kvs_restored = [(k, quoted.sub(qs.expand, v)) for k, v in kvs_pairs] def converter(v): if v.startswith('"'): return v.strip('"') try: return int(v) except ValueError: return float(v) thedict = dict((k.strip(), converter(v)) for k, v in kvs_restored) for k in thedict: print "%-8s %s" % (k, thedict[k]) print thedict </code></pre> <p>I'm emitting <code>thedict</code> twice to show exactly how and why it differs from the required result; the output is:</p> <pre><code>age 34 location US name John Smith avatar :,=) height 173.2 {'age': 34, 'location': 'US', 'name': 'John Smith', 'avatar': ':,=)', 'height': 173.19999999999999} </code></pre> <p>As you see, the output for the floating point value is as requested when directly emitted with <code>print</code>, but it isn't and <strong>cannot</strong> be (since there <em>IS</em> no floating point value that would display <code>173.2</code> in such a case!-) when the <code>print</code> is applied to the whole <code>dict</code> (because that inevitably uses <code>repr</code> on the keys and values -- and the <code>repr</code> of <code>173.2</code> has that form, given the usual issues about how floating point values are stored in binary, not in decimal, etc, etc). You might define a <code>dict</code> subclass which overrides <code>__str__</code> to specialcase floating-point values, I guess, if that's indeed a requirement.</p> <p>But, I hope this distraction doesn't interfere with the core idea -- as long as the doublequotes are properly balanced (and there are no doublequotes-inside-doublequotes), this code does perform the required task of preserving "special characters" (commas and equal signs, in this case) from being taken in their normal sense when they're inside double quotes, even if the double quotes start <em>inside</em> a "field" rather than at the beginning of the field (<code>csv</code> only deals with the latter condition). Insert a few intermediate prints if the way the code works is not obvious -- first it changes all "double quoted fields" into a specially simple form (<code>"0"</code>, <code>"1"</code> and so on), while separately recording what the actual contents corresponding to those simple forms are; at the end, the simple forms are changed back into the original contents. Double-quote stripping (for strings) and transformation of the unquoted strings into integers or floats is finally handled by the simple <code>converter</code> function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload