简体   繁体   中英

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. I am using a DictReader object with key pair values. I am using the key pair values in the for loop to print out the information(kml in this instance).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM