3

This is probably a silly question but I don't really know much about Python yet. I'm writing this code that is suppose to take the input for values of X and the input for values of Y, write them in files and print them on the screen (later on I'll plot them with matplotlib). But I'm getting on my outputs two parenthesis that I didn't want them to be there. Here's the code (I'm programming on Ubuntu with Python 3.4):

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

The output I'm getting in the files is something like (10, 20, 30) and the output I'm getting on the screen is something like ['(10','20','30)']. How can I get rid of these parenthesis?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Victor José
  • 367
  • 1
  • 4
  • 12
  • possible duplicate of [Adding all the numbers from a list together(not summing). Python 3.3.2](http://stackoverflow.com/questions/18816813/adding-all-the-numbers-from-a-list-togethernot-summing-python-3-3-2) – seaotternerd May 09 '15 at 04:19
  • I suspect you're using Python 2, not Python 3. (The code you post is not valid in Python 3.) In that case, if you replace `input` with `raw_input`, that will avoid the parentheses being created in the first place. If you *are* using Python 3, then there shouldn't be any parentheses in your output, and you don't need to convert to `str` before in your `write` method calls. – Mark Dickinson May 09 '15 at 12:24
  • wow you are right, my bad. I'm really new to python programming. I checked and my ubuntu came with python 2.7 installed, not 3.4. Thanks for the heads up, i'll look around to understand the difference. – Victor José May 09 '15 at 18:53

2 Answers2

5

You can do with str.strip:

>>> '(10, 20, 30)'.strip('()')
'10, 20, 30'
famousgarkin
  • 13,687
  • 5
  • 58
  • 74
2

You can remove first and last character on printing:

print listax[1:-1]
Raphael Amoedo
  • 4,233
  • 4
  • 28
  • 37