Note that there are some explanatory texts on larger screens.

plurals
  1. POPython compare bombs if files not sorted
    primarykey
    data
    text
    <p>I have written some code to compare two files via a search string. </p> <p>The file = master data file The checkfile = list of states &amp; regions</p> <p>When I have more than 1 state in the file that is not in sorted order it bombs out.</p> <p>How can i get this to work without having to sort my "file"</p> <p>The Error message: Traceback (most recent call last): File "./gangnamstyle.py", line 27, in csvLineList_2 = csv2[lineCount].split(",") IndexError: list index out of range</p> <p>My code:</p> <pre><code>#!/usr/bin/python import csv file = raw_input("Please enter the file name to search: ") #File name checkfile = raw_input("Please enter the file with the search data: ") #datafile save_file = raw_input("Please enter the file name to save: ") #Save Name search_string = raw_input("Please type string to search for: ") #search string #row = raw_input("Please enter column text is in: ") #column number - starts at 0 #ID_INDEX = row #ID_INDEX = int(ID_INDEX) f = open(file) f1 = open(save_file, 'a') csv1 = open(file, "r").readlines() csv2 = open(checkfile, "r").readlines() #what looks for the string in the file copyline=False for line in f.readlines(): if search_string in line: copyline=True if copyline: f1.write(line) for lineCount in range( len( csv1) ): csvLineList_1 = csv1[lineCount].split(",") csvLineList_2 = csv2[lineCount].split(",") if search_string == csvLineList_2[0]: f1.write(csvLineList_2[2]) f1.close() #close saved file f.close() #close source file #csv1.close() #csv2.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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