Reputation: 5
with open("C:\\Users\\Nav\\Desktop\\script\\names.txt", 'r+') as f:
for x in range (0, 100):
f_contents = f.readline()
name = f_contents
name2 = name
print(name.lower().replace(" ", "") + "@gmail.com" + "\n")
x = input()
With this code, I am trying to read a file with a full name on each line and format it, that works fine but when I add the "@gmail.com" and get it printed out it gets printed to two different lines in the console.
For example, my output is
austenrush
@gmail.com
yuvaanduncan
@gmail.com
jawadpatton
@gmail.com
hanifarusso
@gmail.com
kerysbeck
@gmail.com
safiyamcguire
@gmail.com
oluwatobilobamiddleton
@gmail.com
while I would like to get:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Upvotes: 1
Views: 45
Reputation: 531818
readline
doesn't strip the newline read from the file; you have to do that yourself.
f_contents = f.readline().rstrip("\n")
Files are iterable, though, so you don't need to call readline
explicitly.
from itertools import islice
with open("C:\\Users\\Nav\\Desktop\\script\\names.txt", 'r+') as f:
for f_contents in islice(f, 100):
name = f_contents.rstrip("\n").lower().replace(" ", "")
print(name + "@gmail.com" + "\n")
x = input()
Upvotes: 2