-2

My question is different because the answer conveys the deeper problem which is concise code vs forest like variables. I believe I deserve reputation points for pointing out the clarity of concise shortened variable code is for finding the logical problem.

I'm working on a Dataquest tutorial Great site for data Python btw and having problems with this string cleaning code. The cleaned string comes back exactly as the original with nothing parsed out.

here's the code, I tried to make it as concise as possible:

ss = open('story.txt', 'r').read()
cc = [",", ".", ";", "\n", "'"]

print(ss + "\n\n\n")

cleaned_story = ""
def clean_text(x, y):
    cs = x
    for e in y:
        cs.replace(e, "")
    cs = cs.lower()
    return cs

cleaned_story = clean_text(ss, cc)
print(cleaned_story)
print(type(cleaned_story)) #this is just for weird test
David Agabi
  • 43
  • 1
  • 8
  • 2
    Read the documentation on `str.replace` function. It **returns** a new string. Python strings are immutable, and cannot be changed in place. – OneCricketeer Dec 10 '16 at 08:19
  • Also maybe helpful - http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python – OneCricketeer Dec 10 '16 at 08:20
  • No I realized it as soon as I shortened the variables to 1 or 2 digits. That's a good lesson because if the code is too cumbersome it could look like a forest. I lost two reputation points when I should have gained some for some code style lesson :( – David Agabi Dec 10 '16 at 08:29
  • Yes, thank you for providing a [mcve] – OneCricketeer Dec 10 '16 at 08:36

2 Answers2

0

Nevermind. I found the problem. Making the code more concise helped me see the error right away. I forgot to assign cs variable to itself after using the replace method.

I love Python so much!

Thanks StackOverflow. You are God!

David Agabi
  • 43
  • 1
  • 8
0

It looks like you were calling the function .replace(), which returns a modified version of the string, but not assigining anything to the return value. I changed the line to

cs = cs.replace(e, "")

and it worked fine.

ss = "this text, it is... a test;"
cc = [",", ".", ";", "\n", "'"]

print(ss + "\n\n\n")

cleaned_story = ""
def clean_text(x, y):
    cs = x
    for e in y:
        cs = cs.replace(e, "")
    cs = cs.lower()
    return cs

cleaned_story = clean_text(ss, cc)
print(cleaned_story)
print(type(cleaned_story)) #this is just for weird test
Ian Conway
  • 367
  • 1
  • 2
  • 13
  • yep...the original code was a forest so I shortened the variable names for more visibility and immediately identified the problem. Thank you for your help :) – David Agabi Dec 10 '16 at 08:37