Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python打印函数添加不需要的新行_Python_String_Data Processing - Fatal编程技术网

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

Python打印函数添加不需要的新行,python,string,data-processing,Python,String,Data Processing,我有一个读取文件的脚本。该文件包含多行文本数据,每行对应一个播放机,播放机的每个属性由一个选项卡分隔 我的脚本将每一个播放器的行分解成一个数组,然后简单地将数据重建成一个字符串,我想保存到一个单独的文件中,这样新文件中的每一行对应一个播放器: # -*- coding: utf-8 -*- def main(): exampleDataFile = open("exampleData.txt", "r") dataStorageFile = open("playerString

我有一个读取文件的脚本。该文件包含多行文本数据,每行对应一个播放机,播放机的每个属性由一个选项卡分隔

我的脚本将每一个播放器的行分解成一个数组,然后简单地将数据重建成一个字符串,我想保存到一个单独的文件中,这样新文件中的每一行对应一个播放器:

# -*- 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()
当我检查日志和保存输出的文件时,第一个输出被打印/保存到一行,这正是我想要的。但是,所有后续行都在最后的“
”)\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")
我试着打印出很长的字符串,每次打印它们都打印/保存到一行,但是由于某种原因,当我打印/保存玩家输出时,我得到了
在另一行,我不知道为什么?谢谢

在读取文件时,Python不会在行尾去除换行符。这意味着该文件的内容如下

row1,value1
row2,value2
将被读取为两个字符串,分别包含
“行1,值1\n”
“行2,值2\n”


作为清理/预处理的一部分,制作类似于
modifiedLine=line.strip('\n')

您知道末尾有
\n
,这会产生一个新行吗?如果你想准确地打印出要打印的内容,只需执行
打印(“某物”,end=“”)
。但最可能的问题是,您的一个变量在数据中包含
\n
。因此,请执行
listOfComponents[4]。strip()
以清除这些内容。您可以共享文件中的那一行吗…@多谢您的快速响应,我知道我在末尾有
\n
,但是您会注意到这是在
之后。也就是说,在每个
listOfComponents[x]的末尾添加了
.strip()
已经奏效了,非常感谢!如果你将此作为答案发布,我会接受。很高兴它奏效,@Slam击败了我,所以如果它能以你希望的方式解决问题,请接受这个答案。^^很高兴它奏效了!:)