2016-07-01 24 views
-1

我正在制作一个Python脚本以将CSV转换为GeoJSON文件,但是,我想用不同的数据集重新使用它,并且不想硬编码任何东西。我正在尝试将项目列表传递给循环内的字符串格式化程序。我不断收到错误,并看到Python可以将一个列表作为函数中的参数并单独遍历它,但似乎无法使用字符串格式化程序来完成此操作。将列表传递给Python中的字符串格式化程序

如何将未知数量的参数传递给循环内的字符串格式化程序?

iter = 0 
for row in rows: 
    iter += 1 
    if iter >= 2: 
    latitude = lat_long_location["latitude"] 
    longitude = lat_long_location["longitude"] 
    property_arr = [row[longitude], row[latitude]] 
    item_index = 0 
    for ind, item in enumerate(row): 
     if ind is latitude or ind is longitude: 
     print("next") 
     else: 
     property_arr.append(item) 
    #output += template.format(*property_arr) 
    output += template % (*property_arr) 
+0

下一个应该做什么?同样使用'纬度'和'经度'是不好的想法。 'is'检查对象的身份 –

+0

oops,我的意思是print(“next”)。感谢您指出了这一点! –

+0

@NicholasGati:注释掉的代码:'template.format(* property_arr)'应该适合你(只要模板合适)。 – SuperSaiyan

回答

0

这不是位置扩展的工作原理。但反正它是错误的工具。

output += template % tuple(property_arr) 
+0

它的工作原理!我对Python相当陌生,所以我不知道这一点,它困扰着我。非常感谢! –

+0

@PadraicCunningham:将格式目标序列转换为元组。 –

相关问题