Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem has more to do with the CSV format, since <code>difflib</code> has no idea it's looking at columnar fields. What you need is to figure out into which field the guide is pointing, so that you can adjust it when printing the columns.</p> <p>If your CSV files are <em>simple</em>, i.e. they don't contain any quoted fields with embedded commas or (shudder) newlines, you can just use <code>split(',')</code> to separate them into fields, and figure out where the guide points as follows:</p> <pre><code>def align(line, guideline): """ Figure out which field the guide (^) points to, and the offset within it. E.g., if the guide points 3 chars into field 2, return (2, 3) """ fields = line.split(',') guide = guideline.index('^') f = p = 0 while p + len(fields[f]) &lt; guide: p += len(fields[f]) + 1 # +1 for the comma f += 1 offset = guide - p return f, offset </code></pre> <p>Now it's easy to show the guide properly. Let's say you want to align your columns by printing everything 12 spaces wide:</p> <pre><code>diff=difflib.ndiff(...) for line in diff: code = line[0] # The diff prefix print code, if code == '?': fld, offset = align(lastline, line[2:]) for f in range(fld): print "%-12s" % '', print ' '*offset + '^' else: fields = line[2:].rstrip('\r\n').split(',') for f in fields: print "%-12s" % f, print lastline = line[2:] </code></pre> <p>Be warned that the only reliable way to parse CSV files is to use the <code>csv</code> module (or a robust alternative); but getting it to play well with the diff format (in <em>full</em> generality) would be a bit of a headache. If you're mainly interested in readability and your CSV isn't too gnarly, you can probably live with an occasional mix-up.</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. This table or related slice is empty.
    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