[英]Assigning a fieldname to a variable in Python
I am new to Python and I am trying to check for nulls in the csv I am processing. 我是Python的新手,我正在尝试检查正在处理的csv中是否为null。 I am using a DictReader object with key pair values.
我正在使用具有键对值的DictReader对象。 I am using the key pair values in the for loop to print out the information(kml in this instance).
我在for循环中使用密钥对值来打印出信息(在这种情况下为kml)。
I go to run the program and it is not liking my variable assignment. 我去运行程序,它不喜欢我的变量分配。 Here is the error I am receiving.
这是我收到的错误。
File "./csvtokml3.py", line 31
Latvariable = str(row["lat_degrees"]),Longvariable = str(row["lon_degrees"])
SyntaxError: can't assign to function call
Here is the code for the program. 这是程序的代码。
#!/usr/bin/python
#
#
#
import csv
#Input the file name.
fname = raw_input("Enter file name WITHOUT extension: ")
data = csv.DictReader(open(fname + '.csv'), delimiter = ',')
#Open the file to be written.
f = open('csv2kml.kml', 'w')
#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://www.opengis.net/kml/2.'>\n")
f.write("<Document>\n")
f.write(" <name>" + fname + '.kml' +"</name>\n")
for row in data:
f.write(" <Placemark>\n")
f.write(" <name>" + str(row["station"]) + "</name>\n")
### f.write(" <description>" + str(row[0]) + "</description>\n")
f.write(" <Point>\n")
#Check for nulls for lat and long
Latvariable = str(row["lat_degrees"]), Longvariable = str(row["lon_degrees"])
if Latvariable !=null and Longvariable !=null:
f.write(" <coordinates>" + str(row["lat_degrees"]) + "," + str(row["lon_degrees"]) + "</coordinates>\n")
f.write(" </Point>\n")
f.write(" </Placemark>\n")
f.write("</Document>\n")
f.write("</kml>\n")
f.close()
print "File Created. "
print "Press ENTER to exit. "
raw_input()
Your syntax is incorrect, you wanted: 您的语法不正确,您需要:
Latvariable, Longvariable = str(row["lat_degrees"]), str(row["lon_degrees"])
instead to assign multiple values to multiple names. 而是为多个名称分配多个值。 Alternatively, put the two statements on separate lines:
或者,将两个语句放在单独的行上:
Latvariable = str(row["lat_degrees"])
Longvariable = str(row["lon_degrees"])
You cannot combine multiple assignment statements with commas like you tried; 您不能像尝试过的那样用逗号将多个赋值语句组合在一起; that works in JavaScript but not in Python.
在JavaScript中有效,但在Python中无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.