9418012345, hello world.
9418154321, hello world.
94xxxxxxxx, hello world.
.
.
.
.
.
.
.
1000 mobile number
but same message.
I already have mobile numbers pasted in every line. How can I add comma and same message after all numbers.
Asked
Active
Viewed 87 times
-2

project cell
- 9
- 2
-
2Read the file that has only the numbers, line by line. Write out new lines one by one, with the string, to a different file. Replace the first file with the second. – gspr May 16 '21 at 18:31
3 Answers
0
You can read the file lines and store them in a list of strings and then add the string to all lines:
text_to_add = ', hello world'
with open('numbers.txt') as f:
lines = f.read().splitlines()
with open('result_file.txt', "w") as f:
for line in lines:
print(line + text_to_add, file=f)

Shivam Roy
- 1,961
- 3
- 10
- 23
0
text_to_add = '' #add your text here
#create an file to write the new edited text
op_file = open("myfile.txt","a")#append mode
with open("myfile.txt") as fp:
Lines = fp.readlines()
for line in Lines:
newline = str(line) + ',' + text_to_add
op_file.write(newline+'\n')
op_file.close() #close the file

Sai Pardhu
- 289
- 1
- 12
0
Read the content and iterate over the lines by adding the message you want and then write it to a file again with new content.
message = "HelloWorld"
with open("number.txt") as fd:
lines = fd.read().splitlines()
with open("file_with_text.txt","w") as fd:
for line in lines:
print(f'{line}, {message}', file=fd)

sprao
- 34
- 3