我正在python 3.6上的以下结构上工作:
import csv
aircraft = input("Please insert the aircraft type : ")
characteristics = input("Please insert the respective aircraft characteristics: ")
with open("aircraft_list.csv","a",newline="") as output:
if aircraft not in open("aircraft_list.csv").read():
wr = csv.writer(output)
wr.writerow([aircraft + "," + characteristics])
# with squared brackets since otherwise each letter written as a separate string to the csv, separated by comma
else:
for row in enumerate(output):
data = row.split(",")
if data[0] == aircraft:
wr = csv.writer(output)
wr.writerow([characteristics],1)
我想以以下格式将输入内容写入csv:
B737,波音,1970等
A320,空中客车,欧盟等
只要飞机例如B737条目尚不存在,可以很容易地将其写入CSV。但是,一旦csv中已经存在B737属性,我想将特征(不是飞机)添加到已经为例如B737。特征的顺序无关紧要。
我希望将其他输入特征添加到csv中的正确行中。我该怎么办?
由于我是编码的新手,所以我尝试了基础知识并将其与在Stackoverflow上找到的代码结合在一起,但不幸的是,我无法使它正常工作。
您的帮助将非常棒,谢谢!