-4

I have tryed a bunch of other ways of doing it from other forums. But none of them seem to work. Maybe i am just writing them wrong. I have a text file that I want python to replace the "0" with a "1" my text file looks like this

0
hello
more stuff
meh

i want it to be

1
hello
more stuff
meh

Thanks, this is probably really simple. If anyone can post a way to replace the 0 with a 1 or delete a whole line and then rewrite it. Either one works. Thanks I also want it to work if the 0 was on the third line or some other line. You would of course have to change what line it targets

Matt
  • 61
  • 1
  • 9
  • 2
    Do you want to replace every `0` with a `1`? If you have the number `10`, would you want it to become `11`? – Matias Cicero Dec 08 '17 at 16:52
  • I want it to replace just the "0" on that certain line to make it "1" ( just the 0 by its self) – Matt Dec 08 '17 at 16:53
  • 2
    SO is not your coding service, please edit your question and show us what you have tried so far. Please take the [tour](https://stackoverflow.com/tour) if you haven't already. – ext Dec 08 '17 at 16:54
  • 1
    So where is your code to read the text file and parse the values? Surely you have some starting point and got stuck somewhere rather than turning to us here to write another answer for reading/writing text files to join the thousands of existing answers? – roganjosh Dec 08 '17 at 16:55
  • The code is in the same folder as the text document, I know how to write but not replacing a certain line. I have done some research and all of it didn't work – Matt Dec 08 '17 at 16:56
  • How about this: read file into a list (each line is an element), modify first element of the list, write list back to the file. – Pavel Dec 08 '17 at 16:58
  • @Matt Please, be more specific. When you say you want to replace *just the `0` on that certain line*, what exactly do you mean? Do you want to replace the `0` which is located on the first line of the file? Are there any more `0`s you'd want to replace? Do you want to replace the `0`s that are not next to any character? – Matias Cicero Dec 08 '17 at 16:58
  • It would just be the 0 on the first line and then be able to change it so if it was somewhere else (a diffrent line) it would work (if you changed the line it changed), So yea just the one 0 – Matt Dec 08 '17 at 17:01
  • How about just using sed? `cat myfile.txt | sed '1s/^0$/1/'` – ext Dec 08 '17 at 17:06
  • Did you try this answer? https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python (replacing a line and creating a whole new file) – tjiagoM Dec 08 '17 at 17:07
  • 1
    Or this: https://stackoverflow.com/questions/16622754/how-do-you-replace-a-line-of-text-in-a-text-file-python – tjiagoM Dec 08 '17 at 17:08
  • Possible duplicate of [How do you replace a line of text in a text file (python)](https://stackoverflow.com/questions/16622754/how-do-you-replace-a-line-of-text-in-a-text-file-python) – tjiagoM Dec 08 '17 at 17:09
  • I cant seem how to get these to work in my code. – Matt Dec 08 '17 at 17:11
  • import fileinput for line in fileinput.input('inFile.txt', inplace=True): print line.rstrip().replace('oldLine', 'newLine'), Im not good with replacing these variables for my script does the Oldline and newline change and if so what to? – Matt Dec 08 '17 at 17:12

1 Answers1

1

Supposing you want to replace the 0 on the first line of the file:

def replace_first_line(path, value, condition=None):
    with open(path, 'r') as file:
        lines = file.read().split('\n')
    if lines:
        first_line = lines[0]
        if not condition or first_line.strip() == condition:
            first_line = first_line.replace(condition or first_line, value)
        lines[0] = first_line
    with open(path, 'w') as file:
        file.write('\n'.join(lines))

# Change 'your_file.txt' to the name of your file
replace_first_line('your_file.txt', '1', condition='0')
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154