Python Forum
How to add 1 in a text file ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add 1 in a text file ?
#1
Do anyone know how to add 1 to a text file.
For example :
Test.txt contain:
test 1
test 2
test 3

I wish to add 1 to the file and write to a new file NewTest.txt.
So the newtest.txt should output like it
test 2
test 3
test 4

with open('test.txt') as f:
new_file = open("newtest.txt", "w")
for item in f.readlines():
Reply
#2
Use code tag BBcode help,use Ctrl+Shift+v when copy in to preserve indentation.
with open('data.txt') as f,open('new.txt', 'w') as f_out:
    lst = [i.strip() for i in f]
    lst.append('test 4')
    f_out.write('\n'.join(lst))
To append to existing file.
with open('data.txt', 'a') as f:
    f.write('test 4\n')
Reply
#3
Actually, i want to change the integer to plus 1
test 1 +1
test 2 +1
test 3 +1
How do i change ? All the integer add 1

so the new file should look like
test 2
test 3
test 4
Reply
#4
since test 1 is a string, you need something like:
mystr = 'test 1'
mystr = mystr[:-1] + str(int(mystr[-1:]) + 1)
print(mystr)
which will produce:
Output:
test 2
[align=initial !important]   [/align]
#s3gt_translate_tooltip_mini { display: none !important; }
Reply
#5
is there any example with reading a file
How does this implement into a file
mystr = 'test 1'
mystr = mystr[:-1] + str(int(mystr[-1:]) + 1)
print(mystr)
Reply
#6
it doesn't, but snippsat showed you how to do that.
with open('data.txt') as f,open('new.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
        line = line[:-1] + str(int(line[-1:]) + 1)
        f_out.write('{}\n'.format(line))
Reply
#7
just to mention that this will work with singe digits, i.e. it will not work with test 10 for example
Reply
#8
Thanks it work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,061 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,574 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,839 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,247 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,325 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,353 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,096 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 19,417 Jul-17-2020, 01:45 PM
Last Post: marvel_plato
  Rename file from value in text file Nuge93 1 2,140 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk
  Trying to make column based file from text file scor1pion 7 3,392 Jul-16-2019, 02:43 PM
Last Post: scor1pion

Forum Jump:

User Panel Messages

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