Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The error you're getting is probably due to the file lengths not being equal.</p> <p>It's not exactly clear from what you've written, what you're hoping to do. It looks to me like (maybe) you want to find a search term in "master file", and if you find it, write the line you find to the "save file". It also looks to me like you want to find that same search term in the very first field of the "check file", and if you find it, write the contents of the third field into the "save file". If that's wrong, it's because your code has bugs.</p> <p>Either way, there's a bunch of issues in the code you've posted, and you're probably going to get at least some mileage out of using the <code>csv</code> module to do what you're trying to do.</p> <p>Maybe post a fuller problem description.</p> <p>Edit:</p> <pre><code>import csv import sys def build_state_lookup(fn): with open(fn) as infile: reader = csv.reader(infile) # throw away first line reader.next() # now build a dictionary mapping state to region lookup = {state: region for (state, _, region) in reader} return lookup def process_big_file(in_fn, checkfile, out_fn): lookup = build_state_lookup() with open(in_fn) as infile: with open(out_fn, 'w') as outfile: reader = csv.reader(infile) writer = csv.writer(outfile) # output the header row writer.writerow(reader.next() + ['Region']) for row in reader: state = row[0] region = lookup.get(state, "No Region Found") row.append(region) writer.writerow(row) def main(): process_big_file(*sys.argv[1:]) if __name__ == '__main__': main() </code></pre>
    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.
 

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