python - 在一行中打印文本文件中的文本

标签 python python-3.x

下面的代码打印出这样的文本:

 John
  Smith
  02/07/1234

首先它缩进了两行,我将如何更改代码以将其打印为: John Smith 02/07/1234 在一条线上?

with open("Forename") as f1, open("Surname") as f2, open("Date of birth") as f3:
    for forename, surname, birthdate in zip(f1,f2,f3):
        print (forename, surname, birthdate)

最佳答案

尝试:

with open("Forename") as f1, open("Surname") as f2, open("Date of birth") as f3:
    for forename, surname, birthdate in zip(f1,f2,f3):
        print("{} {} {}".format(forename.strip(' \t\n\r'), surname.strip(' \t\n\r'), birthdate.strip(' \t\n\r')))

.strip('\t\n\r') 删除前导和尾随制表符返回和空格,.format() 格式化要打印的字符串一种很好的可控方式。

关于python - 在一行中打印文本文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45642607/

相关文章:

python - Django 子域

python - 如何从网络加载 PyQt4 中的 .ico 文件

python - 如何在 Django 测试中强制交易中的竞争条件?

python-3.x - Keras 自定义层/约束来实现相等的权重

Python 3 BS4 - 返回指定 <tr> 中的第一个 <td>

python-3.x - Python 3 没有名为 AppKit 的模块

python - 在 OpenErp 7 中查询对象

python - 如何实现超时功能?

python-3.x - NameError:名称 '??' 未定义

python - Pandas 按字符串从列中删除元素