我有代码分析一个 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)
