我可以用python写CSV表单orderdict吗

vbkedwbf  于 2023-03-11  发布在  Python
关注(0)|答案(2)|浏览(80)

我下面的代码有什么问题,错误说是不可写的

with open('F:\learning\coding\python learning\jadi excersisse\work with files\grades_for_exercise.csv') as f:
        myFile=csv.reader(f)
        person_mean=OrderedDict()
        for row in myFile:
            name=row[0]
            person_grades=[]
            for number in row[1:]:
                person_grades.append(int(number))
            person_mean[name]=mean(person_grades)
        print(list(person_mean.items()))
with open ('F:\learning\coding\python learning\jadi excersisse\work with files\one_result.csv') as csvfile:
        csvwriter=csv.writer(csvfile)
        csvwriter.writerows((person_mean))

我需要的数据是维滕在csv文件

cyvaqqii

cyvaqqii1#

据我所知,这就是如何在python中读写csv文件

import csv

data = [["a", "b", "c"], ["d", "e", "f"]]

def save_in_csv(data):
    with open("your_csv_file.csv", "w", newline="") as f:
        writer = csv.writer(f)
        writer.writerows(data)

def open_csv():
    with open("your_csv_file.csv", "r") as f:
        reader = csv.reader(f)
        for row in reader:
            print(row)

save_in_csv(data)
open_csv()

我希望这对你有帮助

vq8itlhq

vq8itlhq2#

通过阅读open()函数的文档,我发现了问题,需要在open函数中写入模式,当我在文件中添加写入模式“w”时,问题就解决了。

相关问题