2015-02-23 21 views
-2

我想让我的函数搜索两个索引,分别是“name”和“state”。这是完成并分配给两个单独的变量。名称已经是一个字符串,我们希望将状态转换为字符串。我想连接这两个varibales。 nearcity是一个字符串,状态不是。我想要将状态转换为字符串并将它们连接起来。如何做呢。其简单的我想,但我是新来的蟒蛇如何连接两个变量,其中一个是字符串而另一个不是

if valTest: 
      idx = layer.fieldNameIndex('name') #name of the attribute column with the cities in it 
      idx1 = layer.fieldNameIndex('state') 
      nearCity = f.attributes()[idx] 
      state = f.attributes()[idx1] 
+0

请**编辑**的问题,包括你的代码和完整的错误回溯。 'state&cities'不是一个有效的Python标识符,请尝试'states_and_cities'。 – jonrsharpe 2015-02-23 16:47:33

+0

我试过state_and_cities = nearCity +''+ str(状态)。这两个值都写入一个csv文件中的一个单元格中。如何使它分开? – Farook 2015-02-23 16:54:24

+0

...用逗号分隔它们?你没有显示任何CSV处理,所以几乎不可能猜测你正在做什么。请阅读http://stackoverflow.com/help/mcve – jonrsharpe 2015-02-23 16:58:31

回答

1

这应该这样做:

s = near_city + "," + str(state) 
+0

是的,这两个值都是在csv文件的一个单元格中编写的。这应该如何解决? – Farook 2015-02-23 16:57:41

+0

看到我编辑的答案!! – 2015-02-23 17:00:55

+0

谢谢大家的时间和耐心 – Farook 2015-02-23 17:06:34

相关问题