[英]One print statement two variables get printed on two different lines
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
雖然我想得到:
austenrush@gmail.com
yuvaanduncan@gmail.com
jawadpatton@gmail.com
hanifarusso@gmail.com
kerysbeck@gmail.com
safiyamcguire@gmail.com
oluwatobilobamiddleton@gmail.com
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.