-1

I am very new to coding, I have a text file that contain data as

Test a value: 1134
Test b value: 2354
Test c value: 4827 

And so on I am trying to write a script that will go through this file and increment values of certain tests by 1. For example I need my out put file to look like

Test a value: 1135
Test b value: 2355
Test c value: 4827 

I need to do this 1000 time so it will take it forever to do it one by one. Appreciate any help you can provide.

I have tried reading the file and using for loop, not sure what I’m doing wrong, it just keep saying invalid.

f=open('Test.txt' ,mode='r')
h = f.readlines()
for line in h:
    list = line[h].split(" ")

for i in line:
    if(line.isdigit()):
        temp = list[i] +1
        list[i] = temp
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • Please [edit] your question to make you problem clearer, with a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) if possible. – ljmc Jan 17 '23 at 17:42
  • 1
    Welcome to SO. _"I have tried reading the file and using for loop"_ You are more likely to get a response if you add your attempt (as text) to your question. – 001 Jan 17 '23 at 17:46
  • You have correctly identified that your task requires you to perform multiple steps -- 1. read the file, 2. Parse it so that you know which line corresponds to which test and its value. 3. Increment the values for the tests that you need. 4. Write back to the file. Which of these steps are you stuck on? What _specifically_ are you having trouble with? – Pranav Hosangadi Jan 17 '23 at 17:50
  • I am having trouble with step 2, parsing it. – New to coding Jan 17 '23 at 18:11
  • Welcome to SO! Stack Overflow is not a code-writing or tutorial service. Please [edit](https://stackoverflow.com/posts/75150203/edit) your question and post [what you have tried so far](https://meta.stackoverflow.com/q/261592), including example input, expected output, the actual output (if any), and the [full text of any errors or tracebacks](https://meta.stackoverflow.com/q/359146), all as formatted text in the question itself. Do not post images of text. The code should be a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Woodford Jan 17 '23 at 18:25
  • I just edited it to show what I have so far, it’s my first day of coding, so my apologies if it’s completely wrong. – New to coding Jan 17 '23 at 18:34
  • This is not a good task for your first day of coding. You should learn to walk before you try to run. You would be much better served by following a few beginners tutorials -- you can find lots of these on the web. The reason I suggest a tutorial and a more structured approach towards learning programming is that you learn a whole bunch of related concepts that you need to apply to any general programming problem. In this case, nested loops, string operations, file operations, etc. are all concepts you should learn _individually_ before trying to apply all together. – Pranav Hosangadi Jan 17 '23 at 18:38
  • There are _lots_ of things wrong with your code, to the point where it needs to be completely redone. For example: `line` is already an element of `h`. `line[h]` is wrong. Your `for i in line` loop only starts _after_ the _entire_ `for line in h` loop, so it only operates on the last line of the file. Why check `line.isdigit()` when you're iterating over the line character-by-character? Why should `list[i] + 1` work? If `list` is the result of splitting a string, it will contain _strings_. You can't add an integer (`1`) to a string. – Pranav Hosangadi Jan 17 '23 at 18:42
  • The core issue here is the same as the other question you just deleted, and both are the same as the question I'm linking. It does solve your problem. The problem is that when you read text from the file, it is **not an integer, even if it composed of digit symbols**, so you must convert it first. The linked duplicate explains how that works. – Karl Knechtel Jan 20 '23 at 11:04
  • I was able to change it into integer but when I go to use while function and increment value by 1 till it reaches 5000, it’s only printing out 1 number, instead of all of them upto 5000 – New to coding Jan 20 '23 at 11:53

1 Answers1

0

While I agree with the comments posted to your question, here is a simple solution to help you get started.

with open('file1.txt','r') as f, open('file2.txt','w') as new2:
  lines = f.readlines()
  print(lines)
  for line in lines:
    i = int(line.split(':')[-1]) + 1
    new_line = line.replace(line.split(':')[-1], str(i))
    print(new_line)
    new2.write(new_line)

First, I read one text file and opened another text file to write (this is to prevent overwriting in your case. You can modify this to read and write to the same file, inplace).

Next, I read the lines of the first text file, which reads into a list. I then take each item of the list, find the number by splitting on ":" (assuming this is your condition since you haven't mentioned otherwise). I increment this number and replace it. I then write the new line into another file.

You may want to consider if all the lines are in the same format. This solution is with that assumption.

Hope this helps you get started!

Shivika P
  • 115
  • 8