-1

我有一个小问题我总是得到错误:

TypeError:无法连接“str”和“list”对象

import csv
import os
fp = "filepath/testfile.csv"
file = open(fp)
lines =file.readlines()
for line in lines:
    line = line.strip()
    fields = line.split(',') #comma seperated
    branch = fields[0].split() #splitting
    lat = fields[1].split()
    lng = fields[2].split()
    web = fields[3].split()
    email = fields[4].split()
    adress = fields[5].split()

    print ("branch: " + branch) #print splitted
    print ("lat: " + lat)
    print ("lng: " + lng)
    print ("web :" + web)
    print ("email: " + email)
    print ("address: " + address)

f = open('filepath/csv2kml.kml', 'w')
fname = "testing_Actions"
#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://earth.google.com/kml/2.1'>\n")
f.write("<Document>\n")
f.write("   <name>" + fname + '.kml' +"</name>\n")
for row in lines:
    f.write("   <Placemark>\n")
    f.write("       <ExtendedData>\n")
    f.write("           <Data name=the branch name>\n")
    f.write("               <value>\n")
    f.write("               " + str(branch) + "\n")
    f.write("               </value>\n")
    f.write("           </Data>\n")
    f.write("           </Data name=Web>\n")
    f.write("               <value>\n")
    f.write("               " + str(web) +"\n")
    f.write("               </value>\n")
    f.write("           </Data>\n")
    f.write("           </Data name=email>\n")
    f.write("               <value>\n")
    f.write("               " + str(email) + "\n")
    f.write("               </value>\n")
    f.write("           </Data>\n")
    f.write("       <description>" + str(address) + "</description>\n")
    f.write("       <Point>\n")
    f.write("           <coordinates>" + str(lat) + "," + str(lng) + "</coordinates>\n")
    f.write("       </Point>\n")
    f.write("   </Placemark>\n")
f.write("</Document>\n")
f.write("</kml>\n")
print ("File Created. ")
f.close
file.close()

我找不到我的错误。

4

3 回答 3

1

我找不到我的错误

你可以。如果您完全阅读了错误消息,它还会告诉您错误发生在哪几行。

是在这里吗?

print ("branch: " + branch) #print splitted

在以下几行中,您尝试连接 string"branch"和 list branch

尝试将其替换为:

print ("branch: {}".format(branch)) #print splitted
于 2013-01-16T11:01:03.620 回答
0

更好的是,使用 KML 库:http: //googlegeodevelopers.blogspot.ca/2012/01/introducing-pykml-python-library-for.html

几乎每种语言都有一个。

于 2013-01-18T17:29:19.737 回答
-3

我解决了!

print ("branch: " + branch) #print splitted
print ("lat: " + lat)
print ("lng: " + lng)
print ("web :" + web)
print ("email: " + email)
print ("address: " + address)

编辑为

print ("branch: " + `branch`) #print splitted
print ("lat: " + `lat`)
print ("lng: " + `lng`)
print ("web :" + `web`)
print ("email: " + `email`)
print ("address: " + `address`)

f.write("           <Data name=the branch name>\n")
f.write("               <value>\n")
f.write("               " + `branch` + "\n")
f.write("               </value>\n")
f.write("           </Data>\n")

等等

于 2013-01-16T11:10:22.253 回答