Note that there are some explanatory texts on larger screens.

plurals
  1. POConsolidating two scripts into one new one (appending columns to csv)
    primarykey
    data
    text
    <p>I have two scripts which create new columns within a csv, each of them opening the csv and appending a new column. Ideally rather than saving csv to csv1 then opening csv1 and resaving it as csv2 I would like to be able to do this in one step.</p> <h3>Script1</h3> <pre><code>with open("inputcsv1.csv", "r") as input_file: header = input_file.readline()[:-1] #this is to remove trailing '\n' header += ",Table exists?" output_lines = [header] for line in input_file: output_lines.append(line[:-1]) if 'table' in line.split(",")[3]: output_lines[-1]+=",table exists" else: output_lines[-1]+=",No table found" with open("outputcsv1.csv", "w") as output_file: output_file.write("\n".join(output_lines)) </code></pre> <h3>Script2</h3> <pre><code>with open("outputcsv1.csv", "r") as input_file: header = input_file.readline()[:-1] #this is to remove trailing '\n' header += ",Are you sure Table exists?" output_lines = [header] for line in input_file: output_lines.append(line[:-1]) if 'table' in line.split(",")[3]: output_lines[-1]+=",table definitely exists" else: output_lines[-1]+=",No table was not found" with open("outputcsv2.csv", "w") as output_file: output_file.write("\n".join(output_lines)) </code></pre> <p>The two above scripts are those used on a very simple example csv.</p> <h3>Example inputcsv1.csv</h3> <pre><code>title1,title2,title3,Table or no table?,title4 data,text,data,the cat sits on the table,text,data data,text,data,tables are made of wood,text,data data,text,data,the cat sits on the television,text,data data,text,data,the dog chewed the table leg,text,data data,text,data,random string of words,text,data data,text,data,table seats 25 people,text,data data,text,data,I have no idea why I made this example about tables,text,data data,text,data,,text,data </code></pre> <h3>Desired output csv:</h3> <pre><code>title1,title2,title3,Table or no table?,title4,Table exists?,Are you sure Table exist data,text,data,the cat sits on the table,text,data,table exists,table definitely exists data,text,data,tables are made of wood,text,data,table exists,table definitely exists data,text,data,the cat sits on the television,text,data,No table found,No table was not found data,text,data,the dog chewed the table leg,text,data,table exists,table definitely exists data,text,data,random string of words,text,data,No table found,No table was not found data,text,data,table seats 25 people,text,data,table exists,table definitely exists data,text,data,I have no idea why I made this example about tables,text,data,table exists,table definitely exists data,text,data,,text,data,No table found,No table was not found </code></pre> <h3>In an attempt to merge the two scripts I tried the following code:</h3> <pre><code>with open("inputcsv1.csv", "r") as input_file: header = input_file.readline()[:-1] #this is to remove trailing '\n' header2 = input_file.readline()[:-2] #this is to remove trailing '\n' header += ",Table exists?" header2 += ",Are you sure table exists?" output_lines = [header] output_lines2 = [header2] for line in input_file: output_lines.append(line[:-1]) if 'table' in line.split(",")[3]: output_lines[-1]+=",table exists" else: output_lines[-1]+=",No table found" for line in input_file: output_lines.append(line[:-2]) if 'table' in line.split(",")[3]: output_lines2[-2]+=",table definitely exists" else: output_lines2[-2]+=",No table was not found" with open("TestMurgedOutput.csv", "w") as output_file: output_file.write("\n".join(output_lines).join(output_lines2)) </code></pre> <p>It doesn't yield an error, but it only outputs the following in the new csv.</p> <pre><code>data,text,data,the cat sits on the table,text,dat,Are you sure table exists? </code></pre> <p>I am not sure for why, although I am not confident in my use of <code>.join</code>. Any constructive comments would be appreciated.</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.
 

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