0

I'm trying to do some text processing that's mostly re.sub() commands. I know I can't alter a string, but even after I try converting the string to a list and changing it element by element, there's still no change in the output.

What I want to know is: a) where I'm going wrong with my code and b) would I be better off using a bytearray than a list?

with open("responses_test.txt", "r+") as f:
  responsesIPA = f.readlines()

for row in responsesIPA:
  row = list(row)
  row = [i.lower() for i in row]
  row = [re.sub("3", u"\u0259", i) for i in row]
  row = "".join(row)
bkula
  • 541
  • 3
  • 10
  • 22
  • Strings are immutable , but you can assign a new value , ie `row = re.sub("3", u"\u0259", row)` – t.m.adam Apr 24 '17 at 17:35
  • Please provide example input and expected output. – Uriel Apr 24 '17 at 17:35
  • *there's still no change in the output*: did you mean to save `row` somewhere after you changed it? – Brian Apr 24 '17 at 17:36
  • @t.m.adam that would not be the problem in that case. Plus, the code you provided expects `row` as string, when it is a list. – Uriel Apr 24 '17 at 17:37
  • @Uriel what i mean is that the OP can replece his for loop with this : `for row in responsesIPA: row = re.sub("3", u"\u0259", row)` – t.m.adam Apr 24 '17 at 17:39
  • To clarify, an example of the input is a line of text like: F EJ D3R The output I'm hoping for from that is: f ej dər – bkula Apr 24 '17 at 17:39
  • @bkula in that case `for row in responsesIPA: print(row.lower().replace("3","\u0259"))` would suffice. – Uriel Apr 24 '17 at 17:46
  • A little help on MCVE, make the question actually demonstrate the problem. Instead of reading a file, just comment that out and say responsesIPA = [u'J FE D3R'] and actually print something in the code snippet. Then tell us what you actually printed and what you expected to be printed. – Kenny Ostrom Apr 24 '17 at 18:05
  • Thanks, @KennyOstrom! Very solid advice. – bkula Apr 24 '17 at 18:08

1 Answers1

0

You don't need to convert to list to achieve what you are trying to achieve.

for row in responsesIPA:
   row = row.lower()
   row = row.replace("3","\u0259")
   print row
alpeshpandya
  • 492
  • 3
  • 12
  • I tried this, as well as doing row = re.sub("3", u"\u0259", row) instead of row = row.replace("3","\u0259") but my output is still exactly the same as my input. – bkula Apr 24 '17 at 17:48
  • @bkula what is your python version? – Uriel Apr 24 '17 at 17:53
  • How exactly are you using row? Are you trying to reflect your updated string to responsesIPA list? – alpeshpandya Apr 24 '17 at 17:58
  • Not that it really matters here, but I wouldn't modify the loop variable. Just print the function results, and leave "row" under the control of the for loop. – Kenny Ostrom Apr 24 '17 at 17:58
  • 2.7. This solution is actually working for me now. I discovered that the issue is I had a print statement outside the for loop. Like I was printing responsesIPA because eventually there will be a lot more replace statements in there. Not sure why having the print statement outside the loop is causing this not to work, though. – bkula Apr 24 '17 at 18:03
  • Not sure why would you try to print a loop variable outside of loop. If you can update question with code where you are printing it, we can help. – alpeshpandya Apr 24 '17 at 18:05