0

I have a seemingly simple problem. I have a dataset: archive.ics.uci.edu/ml/machine-learning-databases/acute/diagnosis.data and I want to replace the "no"s to "0"s and "yes" to "1"s

I have tried this code:

fString = open("diagnosis.data","r")

fBool = open("diagnosis1.txt","w")

for line in fString:

        line.replace("no","0")

        line.replace("yes","1")

        fBool.write(line) 

fString.close()

fBool.close()

The only thing that happened is that the last yes/no gets an ਍ഀ added. I dont know why it's not working.

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • Strings are not *mutable*, that is, they cannot be changed. The `str.replace` method therefore returns a new string with the replacement. So, you need `line = line.replace("no", "0")`. – Booboo Oct 09 '19 at 15:59
  • Possible duplicate of [Why doesn't calling a Python string method do anything unless you assign its output?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) – Georgy Oct 09 '19 at 16:03
  • With line = line.replace... it gives me the same exact outcome – meisterich 0 Oct 10 '19 at 11:44
  • Could it be that the problem is that the file diagnosis.data is in an array format so you can't edit the while line and have to go in each line row to row? – meisterich 0 Oct 10 '19 at 11:58

3 Answers3

2

Since replace returns the modified string you need to assign it. The original is left untouched. I guess you need:

with open("diagnosis.data", "r") as fString, open("diagnosis1.txt", "w") as fBool:
    for line in fString:
        nline = line.replace("no", "0")
        nline = nline.replace("yes", "1")
        fBool.write(nline)
hootnot
  • 1,005
  • 8
  • 13
1

Your issue may be that open() doesn't return a list of strings (or some other iterable type), and therefore you can't do for line in fString:, because that will not yield strings which you can then .replace().

Instead you need to do something like:

fString = open("diagnosis.data","r")

lines = fString.read().split('\n')

fBool = open("diagnosis1.txt","w")

for line in lines:

    newLine = line.replace("no","0")

    newLine = newLine.replace("yes","1")

    fBool.write(newLine) 

fString.close()
fBool.close()

This approach gets a list of strings, each of which is a line of a file, and the iterates through that. You need to make sure you use the .replace() method correctly as well, because it returns the new string, but doesn't modify the original string.

ThePythonator
  • 154
  • 2
  • 13
  • But the code copies all the lines to the new file correctly. It just doesn't replace the strings. If it were a problem with the for line in fString then it wouldn't copy the lines right? – meisterich 0 Oct 10 '19 at 11:39
  • I tried this approach and it doesn't replace the Strings and also it copys all the lines in a single line – meisterich 0 Oct 10 '19 at 11:53
  • This _should_ work, can I see your original data file so I can get a better idea of what data is being handled – ThePythonator Oct 10 '19 at 15:10
0

The .replace string method returns the string with the replaced parameters but it doesnt change the object so:

>>> k="lolo"
>>> k.replace('l','k')
'koko'
>>> k
'lolo'
>>> k=k.replace('l','k')
>>> k
'koko'

What you want is:

line=line.replace("no","0")

or with an auxiliar variable:

aux=line.replace("no","0").replace("yes","1")
paltaa
  • 2,985
  • 13
  • 28