我有分析csv文件的代碼,並返回列的最大值和與maxvalue相同行中的另一列的值。我在格式化輸出時遇到問題。返回一個字符串,如何格式化輸出
代碼:
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.'
我該如何解決這個問題?
第二個解決方案會更好,因爲它會忽略一些意想不到的錯誤。 –
什麼樣的錯誤? – AspirationZ
@AspirationZ請參閱此鏈接https://docs.python.org/2/library/stdtypes.html#string-formatting – dhana