0

I'm stuck on replacing "-" with whitespace in Python. I've searched Stack Overflow and tried the code below, but it doesn't do what I want.

import string

text1 = ['why-wont-these-dashes-go-away']
for i in text1:
 str(i).replace("-", " ")
print "output 1: " 
print text1

text2 = ['why-wont-these-dashes-go-away']
text2 = [x.strip('-') for x in text2]
print "output 2: " 
print text2

text3 = ['why-wont-these-dashes-go-away']
text3 = [''.join(c for c in s if c not in string.punctuation) for s in text3]
print "output 3: " 
print text3

text4 = ['why-wont-these-dashes-go-away']
text4 = [' '.join(c for c in s if c not in string.punctuation) for s in text3]
print "output 4: " 
print text4

Here's my output:

output 1: 
['why-wont-these-dashes-go-away']
output 2: 
['why-wont-these-dashes-go-away']
output 3: 
['whywontthesedashesgoaway']
output 4: 
['w h y w o n t t h e s e d a s h e s g o a w a y']

Here's what I want:

['why wont there dashes go away']

I know text1, text2, and text3 are each lists with one item which is a string. It's probably something small I'm overlooking, any ideas?

zfa
  • 105
  • 1
  • 2
  • 7
  • Output 1 would work if you reassigned the return value of replace to the list... `strip` is only for ending characters... The other 2 loop character by character – OneCricketeer Mar 18 '16 at 03:30
  • You are doing several things wrong. Please see the [official Python tutorial](https://docs.python.org/2.7/tutorial/index.html). – TigerhawkT3 Mar 18 '16 at 03:50

7 Answers7

4

You have the following errors:

Method 1: You are not assigning the return value of replace to any variable

Method 2: Strip only strips the characters from the beginning and the end of the string

Method 3 and 4: You are joining every character using either an empty string ('') or space (' '), not every word.

You can try this method:

text1 = [x.replace('-', ' ') for x in text1]

or this:

text1 = [' '.join(x.split('-')) for x in text1]
Selcuk
  • 57,004
  • 12
  • 102
  • 110
3

text1 is a list, the list has one element which is a string 'why wont these dashes go away' at the 0th location. So, simply use:

text1 = [text1[0].replace('-',' ')]

print text1
['why wont these dashes go away']
YBathia
  • 894
  • 1
  • 8
  • 18
2

The manipulation you are doing in your loop has no effect on the data in the list, what you should do instead is create a new list with your data:

[s.replace('-', ' ') for s in text1]
idjaw
  • 25,487
  • 7
  • 64
  • 83
0

Your first example doesn't work because casting i to str makes a copy of the string, which you then fix. Just do:

i.replace("-", " ")

Your second example uses strip, which isn't what you want.

Your third and fourth examples wipe out the dashes, which also can't work.

Neil G
  • 32,138
  • 39
  • 156
  • 257
0

your ['why-wont-these-dashes-go-away'] is already a list element. So you just need to do

text1 = ['why-wont-these-dashes-go-away']
print text1[0]..replace('-',' ')
aroy
  • 452
  • 2
  • 10
0

You could split on - and join

Following output 1, you also need to reassign the values of the list

for i,s in enumerate(text1):
    text1[i] = ' '.join(s.split('-')) 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0
list1 = ['why-wont-these-dashes-go-away', 'a-sentence-with-dashes']
list1 = [text.replace('-',' ') for text in list1]
print list1

Output: ['why wont these dashes go away', 'a sentence with dashes']
Osmond Bishop
  • 7,560
  • 14
  • 42
  • 51