csv 内に新しい列を作成する 2 つのスクリプトがあり、それぞれが csv を開いて新しい列を追加します。理想的には、csv を csv1 に保存してから csv1 を開いて csv2 として再保存するのではなく、これを 1 つのステップで実行できるようにしたいと考えています。
スクリプト1
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))
スクリプト 2
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))
上記の 2 つのスクリプトは、非常に単純な例の csv で使用されるものです。
例 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
目的の出力 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
2 つのスクリプトをマージしようとして、次のコードを試しました。
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))
エラーにはなりませんが、新しい csv で次のように出力されるだけです。
data,text,data,the cat sits on the table,text,dat,Are you sure table exists?
の使用に自信がありませんが、理由はわかりません.join
。建設的なコメントをいただければ幸いです。