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()
使用此代码,我尝试读取每行上具有全名的文件并对其进行格式化,这可以正常工作,但是当我添加“ @ gmail.com”并打印出来时,它会打印到两个不同的行在控制台中。
例如,我的输出是
austenrush
@gmail.com
yuvaanduncan
@gmail.com
jawadpatton
@gmail.com
hanifarusso
@gmail.com
kerysbeck
@gmail.com
safiyamcguire
@gmail.com
oluwatobilobamiddleton
@gmail.com
我希望获得:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
readline
不会剥离从文件读取的换行符;您必须自己做。
f_contents = f.readline().rstrip("\n")
但是文件是可迭代的,因此您无需显式调用readline
。
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()