[英]How can I remove these parenthesis in the output?
这可能是一个愚蠢的问题,但我还不太了解Python。 我正在编写这段代码,假设要获取X值和Y值输入的输入,将它们写入文件并在屏幕上打印(稍后我会用matplotlib绘制它们)。 但我在输出中得到两个括号,我不希望它们在那里。 这是代码(我使用Python 3.4在Ubuntu上编程):
xcoord = open("x.dat","w")
xinput = input('Enter with the values for x separeted by coma (ex: 10,25,67):')
xcoord.write(str(xinput))
xcoord.close()
ycoord = open("y.dat","w")
yinput = input('Enter with the values for y separeted by coma (ex: 10,25,67):')
ycoord.write(str(yinput))
ycoord.close()
xcoord = open("x.dat","r")
listax = xcoord.read().split(',')
ycoord = open("y.dat","r")
listay = ycoord.read().split(',')
print listax
print listay
我在文件中得到的输出类似于(10,20,30),我在屏幕上得到的输出类似于['(10','20','30)']。 我该如何摆脱这些括号?
你可以用str.strip
做:
>>> '(10, 20, 30)'.strip('()')
'10, 20, 30'
您可以删除打印中的第一个和最后一个字符:
print listax[1:-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.