Python Forum
Appending to a text string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending to a text string
#1
Hi All

I have the following code which reads the contents of a file, appends 1,2,3, etc to the contents and then writes the results to a new file:

f = open("text-file.txt", "rt")
s = f.read()
f.close()
f = open("new-text-file.txt", "wt")
f.write(s + '1')
f.write(s + '2')
f.write(s + '3')
f.close()
The code is working but I want to append to the text string 200 times and wondered if there is a while loop or something like that I can use to achieve the same thing. I don't want to write out f.write(s + '4') 200 times.

Thanks for your help.
Reply
#2
Use a for loop with range
for count in range(1,201):
    print(count)
Output:
1 2 3 ... ... ... 198 199 200
Reply
#3
Set a count that keeps track of the number of times and then use while loop like :
count = 0
while count <= 200 :
    # your code
    count += 1
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
Hi there

Thanks for the reply. Now I've got:
f = open("password.txt", "rt")
s = f.read()
f.close()
f = open("password2.txt", "wt")

count = 0
while count <= 200 :
    f.write(s)
    count += 1  

f.close()
However, the code prints the contents of the file 200 times. How do I append the count to the string? i.e, the contents of the file says password. I want the output to read password1password2password3, etc..

Thanks
Reply
#5
Adjusted above code
text = 'password'

for count in range(1,201):
    print(f'{text}{count}')
Output:
password1 password2 password3 ... ... ... password198 password199 password200
Reply
#6
If you already have a file with password,then you do not need a new loop to add to those password.
Just iterate over password and add enumerate() for numbering.
Output:
abc def ghi
with open('password.txt') as f,open('new_password.txt', 'w') as f_out:
    for index,line in enumerate(f, 1):
        line = line.strip()
        f_out.write(f'{line} {index}\n') 
Output:
abc 1 def 2 ghi 3
If just want to create this from a constant string,then just write to file what @Yoriz posted.
Reply
#7
Hi All

I think I'm nearly there with the code:

f = open("file.txt", "rt")
s = f.read()
f = open("new-file.txt", "wt")

for count in range(1,201):
    s+=str(count)
    print({s},{count})
   
f.write(s) 
f.close()
However, its printing the string within the file followed by the count, for example its writing to the new-file like this:

password12345678910111213, etc

What I'm looking to achieve is the below:

password1password2password3, etc.

Thank you
Reply
#8
Did you look at my post?
What dos your file.txt contain,are the new password on each line that you want to add numbering to?

When you do this it read all(whole file) to one text object,and you can not add numbering as all will be added last as you show.
s = f.read()
See how i loop over file content and add to numbering to each line.
Reply
#9
Hi

I tried your code and I got the following error message. I'm using Python 2.7:

invalid syntax: test.py, line 4, pos 39 in file C:\python-scripts\test.py
f_out.write(f'{line} {index}\n')

The print operation is doing this correctly:

f = open("password.txt", "rt")
s = f.read()
f = open("password2.txt", "wt")

for count in range(1900,2021):
    s+str(count)
    print({s},{count})
But how do I write this operation to a file. I only have one line in the file which is password.

Thanks
Reply
#10
Well that must be the problem - you are using python 2.7 - therefore that must be a reason the code isn't working? Switch to python 3
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,605 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  Reading a text until matched string and print it as a single line cananb 1 2,020 Nov-29-2020, 01:38 PM
Last Post: DPaul
  Using an integer to manipulate a string/text variable rexyboy2121 1 1,748 Apr-22-2020, 01:37 AM
Last Post: michael1789
  Trying to extract Only the capitol letters from a string of text Jaethan 2 2,177 Feb-27-2020, 11:19 PM
Last Post: Marbelous
  program that search string in text file and do something alon30 1 3,247 Aug-04-2017, 08:10 AM
Last Post: buran
  How to find exact matching string from the text desul 5 4,103 Apr-17-2017, 04:31 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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