Python Forum
get the number in the line in text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get the number in the line in text file
#1
I need your help (again)
I try to get the number who is stock in the line I need to get. this number can be up to three digits (like ###) and I need to get it and make a var with it, increment it by one, and replace the value in the line per the new value but keeping the other lines intact. my text file look like that.
14
23
3
7
58
1
7
41
41
327
my text file have 40 line (but I don't think that will impact the script)
I have already try this:
f = open("C:\data\data.txt")
    line=f.readlines(1)
    f.close()
    line=str(line)
    try:
        line=int(line)
    except ValueError:
        pass
    line+1
    print(line)
    open("C:\data\data.txt",'w').write(lines.join(line))
    break
but they write 0 whatever the file contain. and I try many other solution was never result except error message. I have search for answer for my problem in many forum, maybe I search in the wrong place. I am really grateful to you who answers us, is probably not easy and you do your possible each time to reply to everybody. thank you.
Reply
#2
first, you are overwriting data.text each time you run, use a different file name for output
you can greatly simplify the code
def process_file(filename_in, filename_out):
    with open(filename_in) as fp, open(filename_out, 'w')  as fo:
        for line in fp:
            intval = int(line.strip())
            print(intval)
            fo.write('{}\n'.format(intval))

process_file('c:\data\data.txt', 'c:\data\newdata.txt')
Reply
#3
thank for you reply
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Number stored as text with openpyxl CAD79 2 333 Yesterday, 10:17 AM
Last Post: CAD79
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 571 Aug-22-2023, 10:14 AM
Last Post: GYKR
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,527 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 856 Jan-31-2023, 09:29 PM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,110 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,360 Sep-27-2022, 01:38 PM
Last Post: buran
  Graphic line plot with matplotlib, text file in pytho khadija 2 1,375 Aug-15-2022, 12:00 PM
Last Post: khadija
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,648 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Skipping line in text without Restarting Loop IdMineThat 4 1,473 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  Print to a New Line when Appending File DaveG 0 1,216 Mar-30-2022, 04:14 AM
Last Post: DaveG

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020