在python3.6中向csv中的现有行添加输入

2024-06-28 11:15:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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。但是,一旦B737属性已经存在于csv中,我就想将特征(而不是飞机)添加到已经为例如B737所做的输入中。特征的顺序并不重要

我想额外的输入特征被添加到正确的行在我的csv。我该怎么做

因为我是一个新的编码,我尝试了基础知识,并结合它的代码,我发现在Stackoverflow,但不幸的是,我不能让它工作

你的帮助太好了,谢谢


Tags: csvtheinputoutputaswith特征open