Python 2.7: CSV 文件的格式不正确

Python 2.7: CSV file is not writing in correct format

我正在尝试编写一个新的 csv 文件来存储排序后的分数。分数存储在名为 "scores_int" 的列表中,并且已经排序。

# Should write a new csv file every time containing the sorted scores    
with open("srt_Scores.csv", "w") as sortfile:
    w = csv.writer(sortfile)
    w.writerow([scores_int])

我遇到的唯一问题是排序后的 csv 文件没有以正确的格式保存。我想要的是完全覆盖旧的 csv 文件,并用新列表替换所有值(我知道效率低下,但它完成了我需要的工作。)

当前 srt_Scores.csv 文件如下所示:

https://gyazo.com/2ad5428d27860d0b72d1554ca9f23a3a

我想做的是让我的 csv 文件看起来像这样:

https://gyazo.com/57cab4a01c93b477344156b141f09689

如果有人能帮助我,我将不胜感激。

假设 scores_int 是一个列表,您只需将 writerow 替换为 writerows 并使此数组中的每个值成为一个 1 元素列表:

w.writerows([[v] for v in scores_int])