【问题标题】:f-strings multi-line printing wrong orderf-strings 多行打印顺序错误
【发布时间】:2020-08-23 12:15:46
【问题描述】:

我想用以下 python (3.8) 代码打印出一份报告,但是您可以看到 pandas 信息打印在顶部而不是底部。对于这种奇怪的行为,我似乎找不到任何解决方案。有关如何解决此问题的任何想法?

time = 600
file = 'election_file.csv'
report = (
    f'Data Diagnostic Report\n'
    f'Date & time: {time}\n'
    f'Data file: {file}\n'
    f'Data frame header (first five lines):\n {election.head()}\n\n'
    f'Data frame information:\n {election.info()}\n'
    f'end of report\n'
)
print(report)

输出:

time = 600...
<class 'pandas.core.frame.DataFrame'>
Index: 10 entries, Adams to Butler
Data columns (total 8 columns):
 #   Column   Non-Null Count  Dtype  
---  ------   --------------  -----  
 0   Obama    10 non-null     float64
 1   Romney   10 non-null     float64
 2   margin   10 non-null     float64
 3   state    10 non-null     object 
 4   total    10 non-null     int64  
 5   turnout  10 non-null     float64
 6   voters   10 non-null     int64  
 7   winner   9 non-null      object 
dtypes: float64(4), int64(2), object(2)
memory usage: 720.0+ bytes
Data Diagnostic Report
Date & time: 600
Data file: election_file.csv
Data frame header (first five lines):
                Obama     Romney     margin state   total    turnout  voters  \
Adams      35.482334  63.112001  27.629667    PA   41973  68.632677   61156   
Allegheny  56.640219  42.185820  14.454399    PA  614671  66.497575  924351   
Armstrong  30.696985  67.901278  37.204293    PA   28322  67.198140   42147   
Beaver     46.032619  52.637630   6.605012    PA   80015  69.483401  115157   
Bedford    22.057452  76.986570  54.929118    PA   21444  66.619031   32189   

           winner  
Adams      Romney  
Allegheny   Obama  
Armstrong  Romney  
Beaver     Romney  
Bedford    Romney  

Data frame information:
 None
end of report

【问题讨论】:

  • 在我看来 election.info() 返回 None 这正是您看到的打印内容。 f'{func()}' 字符串的问题在于,当您创建字符串时,必须在创建字符串时调用func()。然后它被打印出来。所以你可以看到election.head()election.info()都被在创建report的过程中调用了,但是report是在后面打印出来的。

标签: python pandas printing f-string


【解决方案1】:

解决此问题的方法是将报告一分为二:

time = 600
file = 'election_file.csv'
report = (
    f'Data Diagnostic Report\n'
    f'Date & time: {time}\n'
    f'Data file: {file}\n'
    f'Data frame header (first five lines):\n {election.head()}\n\n'
    f'Data frame information:\n '
)
print(report)
election.info()

report = (
    f'\n'
    f'end of report\n'
)
print(report)

【讨论】:

  • 谢谢!没有我想要的那么优雅,但它确实有效。这是有道理的:在另一点上,我正在调用我编写的一些函数,它们也出现在顶部,直到我在其中使用了 return 语句。
【解决方案2】:

那是因为dataframe.info() 在被调用时会打印出来。因此,首先您会看到调用dataframe.info() 的结果(在这种情况下为输出),然后才打印 f 字符串。

换句话说,解释器首先评估 f 字符串中使用的所有“变量”,然后才打印格式化字符串本身。这也是你看到的原因

Data frame information:
  None

在输出中。 info() 打印数据然后返回None(很像内置的print)。

这种行为很容易重现:

def foo():
    print('foo')

print(f'Generated string {foo()}')

这个输出

foo
Generated string None

【讨论】:

    【解决方案3】:

    这是因为 df.info() 会在调用它的那一刻打印出来,并且不返回任何内容。 试试这个,应该可以解决:

    import io
    buf = io.StringIO()
    election.info(buf=buf)
    electioninfo = buf.getvalue()
    
    report = (
        f'Data Diagnostic Report\n'
        f'Date & time: {time}\n'
        f'Data file: {file}\n'
        f'Data frame header (first five lines):\n {election.head()}\n\n'
        f'Data frame information {electioninfo}:\n '
    )
    print(report)
    

    【讨论】:

    • 这将产生完全相同的输出。
    • 对不起,我的错,我从本地复制的东西有误,请立即检查
    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多