'Consolidating two scripts into one new one (appending columns to csv)

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.

Script1

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))   

Script2

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))   

The two above scripts are those used on a very simple example csv.

Example inputcsv1.csv

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

Desired output csv:

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

In an attempt to merge the two scripts I tried the following 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))

It doesn't yield an error, but it only outputs the following in the new csv.

data,text,data,the cat sits on the table,text,dat,Are you sure table exists?

I am not sure for why, although I am not confident in my use of .join. Any constructive comments would be appreciated.



Solution 1:[1]

I think this is close to what you're looking for -- it's what I meant about putting the if statements from the two scripts inside a single for loop. It could be optimized, but I've tried to keep it simple so you can easily understand what is being done.

with open("inputcsv1.csv", "rt") as input_file:
    header = input_file.readline()[:-1]  # remove trailing newline
    # add a title to the header for each of the two new columns
    header += ",Table exists?,Are you sure table exists?"
    output_lines = [header]

    for line in input_file:
        line = line[:-1]  # remove trailing newline
        cols = line.split(',')  # split line in columns based on delimiter
        # add first column
        if 'table' in cols[3]:
            line += ",table exists"
        else:
            line += ",No table found"
        # add second column
        if 'table' in cols[3]:
            line += ",table definitely exists"
        else:
            line += ",No table was not found"
        output_lines.append(line)

with open("TestMurgedOutput.csv", "wt") as output_file:
    output_file.write("\n".join(output_lines))

Contents of the TestMurgedOutput.csv file created:

title1,title2,title3,Table or no table?,title4,Table exists?,Are you sure table exists?
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

Solution 2:[2]

Your output_lines2 list consists of just one element (because all the lines from the file were read in the first for loop), so join has no effect on it, and write statement outputs single element of output_lines2 list. Try this:

with open("test.csv", "r") as input_file:
header = input_file.readline()[:-1] #this is to remove trailing '\n'
header += ",Table exists?"
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 exists"
     else:
            output_lines[-1]+=",No table found"
     if 'table' in line.split(",")[3]:
            output_lines[-1]+=",table definitely exists"
     else:
            output_lines[-1]+=",No table was not found"
with open("output.csv", "w") as output_file:
output_file.write("\n".join(output_lines))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2