简体   繁体   English

Python 2.7:CSV文件未以正确的格式写入

[英]Python 2.7: CSV file is not writing in correct format

I am trying to write a new csv file to store sorted scores. 我正在尝试编写一个新的csv文件来存储排序分数。 The scores are stored in a list called "scores_int" and have already been sorted. 分数存储在名为“ 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])

The only problem that I am experiencing is that the sorted csv file is not being saved in the right format. 我遇到的唯一问题是排序后的csv文件没有以正确的格式保存。 What I want is to completely overwrite the old csv file and just replace all the values with the new list(inefficient I know, but it does the job I need it to.) 我想要的是完全覆盖旧的csv文件,然后用新列表替换所有值(我知道效率不高,但是它可以完成我需要的工作。)

Currently the srt_Scores.csv file looks like this: 当前,srt_Scores.csv文件如下所示:

https://gyazo.com/2ad5428d27860d0b72d1554ca9f23a3a https://gyazo.com/2ad5428d27860d0b72d1554ca9f23a3a

What I want to do is to make my csv file write look like this: 我想要做的是使我的csv文件写入如下所示:

https://gyazo.com/57cab4a01c93b477344156b141f09689 https://gyazo.com/57cab4a01c93b477344156b141f09689

If anyone could help me it would be appreciated. 如果有人可以帮助我,将不胜感激。

假设scores_int是一个列表,你只需要更换writerowwriterows ,使这个阵列中的1元素列表中的每个值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM