gpt4 book ai didi

python - 返回一个字符串,如何格式化输出

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:20 26 4
gpt4 key购买 nike

我有代码分析一个 csv 文件并返回一列的最大值和同一行中另一列的值作为最大值。我在格式化输出时遇到问题。

代码:

import csv

def bigQuake(inputfilename):
with open(inputfilename,"r") as input_file:
reader = csv.reader(input_file)

maxvalue = 0.0
location = None
for line in reader:
try:
p = float(line[4])
if p > maxvalue:
maxvalue = p
location = line[13]
except ValueError:
pass
return "The largest earthquake was a", maxvalue, "magnitude earthquake", location + "."

当前输出是什么样的:

>>> bigQuake("file.csv")
>>> ('The largest earthquake was a', 6.3, 'magnitude earthquake', '13km N of Kunisaki-shi, Japan.')

我想要的:

>>> bigQuake("file.csv")
>>> 'The largest earthquake was a 6.3 magnitude earthquake 13km N of Kunisaki-shi, Japan.'

我该如何解决这个问题?

最佳答案

使用这个,

return "The largest earthquake was a" + str(maxvalue) + "magnitude earthquake" + location + "."

return "The largest earthquake was a %s magnitude earthquake %s." % (maxvalue, location)

关于python - 返回一个字符串,如何格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22775909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com