python,打印出来在同一行

with open('E:/filter.txt', 'rb') as f:

for i in f:
    i = str(i)
    if 'mailbox' in i:
        a = re.match('b.*/(.*)".*', i)
        e = '"' + a.group(1) + '"'
        print(e)
    if 'name' in i:
        i = i.replace('"', '').replace("'", '').replace('bname=', '').replace('\\r\\n', '')
        n = '"' + i + '"'
        print(n)
        
        

打印出来是:
"loic@gmail.com"
"Loic"

请问我要怎么打印出来说是:
"loic@gmail.com" "Loic"

让它在同一行?

小白,谢谢

阅读 3.3k
3 个回答

换成

print(n, end=" ")

end默认是"\n"

一楼的答案是可以的,如果需要做更多的操作可以这样:

for i in f:
    i = str(i)
    mail, name = '', ''
    
    if 'mailbox' in i:
        a = re.match('b.*/(.*)".*', i)
        mail = '"' + a.group(1) + '"'
        
    if 'name' in i:
        i = i.replace('"', '').replace("'", '').replace('bname=', '').replace('\\r\\n', '')
        name = '"' + i + '"'
    
    
    if mail and name:
        # 或者做其他的操作,比如一起存入到数据库中等等
        print('%s,%s' % (mail, name))

print(e) 后面加个逗号: print(e),

推荐问题