【问题标题】:Python .csv writer leaving blank rowsPython .csv 编写器留下空白行
【发布时间】:2016-03-21 06:05:58
【问题描述】:

我在互联网上查看了有关使用 .csv 模块的信息;还有一些人强调了这个问题,但我还没有找到具体的解决方案。

目前,排序/写入/输出代码的工作范围是它将正确的数据输出到文件,但在正确的数据行之间留下空白行。

只是为以下代码提供一些背景知识;我必须只将每个人的最后 3 个分数输出到文件中,并且我将使用“名称,类别”对作为数据库的主键,即我将使用它来识别数据库的用户。变量 'totalscore' 、 'difficulty' 、 'username' 和 'Class' 已经在之前的代码中定义过(有很多,而且并不完全相关,所以我没有包含它)

savescore = 0
savescore = totalscore * (difficulty + 2) #Multiplies their score out of 10 by difficulty + 2
print("You got " + str(totalscore) + " Out of ten and scored " + str(savescore) + " points" ) 

with open("Scoredatabase.csv","r") as f:
    f=csv.reader(f)
    NameClassScoreList = []
    for line in f:
        NameClassScoreList.append(line[0:5]) # Creates a list of all of the entries in the .csv file (List of lists) in the order Name,Class,Score,Score,Score
NameClassList = []

for n in NameClassScoreList: 
    NameClassList.append([n[0], n[1]]) #Appends a  list of pairs of (Name,Class) for all the records in the .csv file
print(NameClassList)
print(NameClassScoreList)
userNameClass=[]
userNameClass=[username,Class] #Creates a variable storing the pair (Name,Class) for the user currently using the program, can later be used to compare with ones in the database

if userNameClass not in NameClassList: #Tests if a user with that username and class already exists (It should append it to the end before sorting)
    NameClassScoreList.append([username,Class,str(savescore),'0','0'])

for entry in NameClassScoreList: #Iterates through all of the entries within the NameClassScoreList (Which includes a list of entries in the .csv file)
    if entry[0:2] == [username,Class]: #Checks if the username,Class matches
        entry[4]=entry[3] #If it does, it moves all of the scores forwards, deleting the the 3rd last and then replaces it with the savescore
        entry[3]=entry[2]
        entry[2]=str(savescore)

NameClassScoreList = sorted(NameClassScoreList, key=itemgetter(1,0)) #Sorts the NameClassScoreList by Class, then Name alphabetically. 

with open("Scoredatabase.csv","w") as f:
    writer=csv.writer(f) #should be for n in NameClassScoreList
    for entryrow in NameClassScoreList:
        writer.writerow((entryrow[0],entryrow[1],entryrow[2],entryrow[3],entryrow[4]))

我想要么使问题在数据输出到文件时得到纠正,要么在输出不正确后修复它(即,根据它们在文件中的位置向上移动行文件)

如果您需要对问题进行任何澄清,请发表评论,我会尽快回复,谢谢。

【问题讨论】:

标签: python csv io


【解决方案1】:

我遇到了同样的问题,你必须覆盖行终止符,例如:

csv.writer(f, lineterminator='\n')

虽然行终止符的行为可能是系统特定的。 *nix 和 DOS/Windows 的行为不同。

我有一台windows机器,windows上正常的行终止符是'\r\n'。如果我使用上面的代码,我会得到一个以 '\r\n' 作为行终止符的文件: Single carriage return

如果我使用 '\r\n' 作为行终止符,我会得到 '\r\r\n': Double carriage return

我猜 csv 编写器会自动将 'nix 行终止符 ('\n') 替换为 windows 行终止符 ('\r\n'),因此您在 Windows 机器上得到的回车太多了。

编辑:我发现应该在 Windows 机器上使用 flag,但我第一次在文档中错过了它:

如果 csvfile 是文件对象,则必须在不同的平台上使用“b”标志打开它。

它的措辞似乎一点也不重要,所以我希望你能原谅我。

【讨论】:

    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 2015-10-09
    • 2016-04-22
    • 2021-07-07
    • 2014-06-08
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多