Python 打印函数添加不需要的新行

Python Print function adding unwanted new line

我有一个读取文件的脚本。该文件包含几行文本数据,每一行对应一个球员,球员的每个属性之间用制表符分隔。

我的脚本将每一行拆分为每个玩家的数组,然后简单地将数据重建为一个字符串,我想将该字符串保存到一个单独的文件中,以便新文件中的每一行都对应一个玩家:

# -*- coding: utf-8 -*-

def main():
    exampleDataFile = open("exampleData.txt", "r")
    dataStorageFile = open("playerStrings.txt", "w+")

for line in exampleDataFile:
    modifiedLine = line.replace('“','"').replace('”','"')
    listOfComponents = modifiedLine.split("\t")
    uid = listOfComponents[0]
    gamerID = listOfComponents[1]
    userPhoneNumber = listOfComponents[2]
    _geoloc = listOfComponents[3]
    allocatedTimes = listOfComponents[4]
    clanName = listOfComponents[5]

    gamerString = ('let ' + uid + ' = player(gamerID: "' + gamerID + '", userPhoneNumber: "' + userPhoneNumber + '", _geoloc: makeCoordinates(points: (' + _geoloc + ")), allocatedTimes: makeallocatedTimes(" + allocatedTimes + '), clanName: "' + clanName + '")\n')
    print (gamerString)
    dataStorageFile.write(gamerString)

if __name__ == '__main__':
    main()

当我检查日志以及输出保存到的文件时,第一个输出是 printed/saved 到一行,这正是我想要的。但是,所有后续行都在最后 '")\n' 处断开。我得到的是:

let r2 = player(gamerID: "TE2", userPhoneNumber: "3456106340", _geoloc: makeCoordinates(points: (51.563601,  -0.118769)), allocatedTimes: makeallocatedTimes(mon:("0700","2300"),tue:("0700","2300"),wed:("0700","2300"),thu:("0700","2300"),fri:("0700","2300"),sat:("0700","2300"),sun:("0700","2300")), clanName: "Tesco
")

注意 ") 如何在单独的一行上。这不是我想要的,我想要这样:

let r2 = player(gamerID: "TE2", userPhoneNumber: "3456106340", _geoloc: makeCoordinates(points: (51.563601,  -0.118769)), allocatedTimes: makeallocatedTimes(mon:("0700","2300"),tue:("0700","2300"),wed:("0700","2300"),thu:("0700","2300"),fri:("0700","2300"),sat:("0700","2300"),sun:("0700","2300")), clanName: "Tesco")

我尝试打印出很长的字符串,每次打印它们都 print/save 成一行,但由于某些原因,当我 print/save 玩家输出时,我得到 ") 在单独的一行上,我不确定为什么?谢谢!

Python not 在读取文件时去除行尾的换行符。这意味着该文件的内容类似于

row1,value1
row2,value2

将被读取为两个字符串,包含 "row1,value1\n"" row2,value2\n"

制作类似 modifiedLine = line.strip('\n') 的东西作为 cleaning/preprocessing 的一部分。