我的代码工作正常,但我希望它将值写入文本文件。当我尝试这样做时,我得到'语法无效'。当我使用python shell时,它可以工作。所以我不明白为什么它不能在我的脚本中工作。
我打赌这是愚蠢的,但为什么不把数据输出到文本文件?
#!/usr/bin/env python
#standard module, needed as we deal with command line args
import sys
from fractions import Fraction
import pyexiv2
#checking whether we got enough args, if not, tell how to use, and exits
#if len(sys.argv) != 2 :
# print "incorrect argument, usage: " + sys.argv[0] + ' <filename>'
# sys.exit(1)
#so the argument seems to be ok, we use it as an imagefile
imagefilename = sys.argv[1]
#trying to catch the exceptions in case of problem with the file reading
try:
metadata = pyexiv2.metadata.ImageMetadata(imagefilename)
metadata.read();
#trying to catch the exceptions in case of problem with the GPS data reading
try:
latitude = metadata.__getitem__("Exif.GPSInfo.GPSLatitude")
latitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLatitudeRef")
longitude = metadata.__getitem__("Exif.GPSInfo.GPSLongitude")
longitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLongitudeRef")
# get the value of the tag, and make it float number
alt = float(metadata.__getitem__("Exif.GPSInfo.GPSAltitude").value)
# get human readable values
latitude = str(latitude).split("=")[1][1:-1].split(" ");
latitude = map(lambda f: str(float(Fraction(f))), latitude)
latitude = latitude[0] + u"\u00b0" + latitude[1] + "'" + latitude[2] + '"' + " " + str(latitudeRef).split("=")[1][1:-1]
longitude = str(longitude).split("=")[1][1:-1].split(" ");
longitude = map(lambda f: str(float(Fraction(f))), longitude)
longitude = longitude[0] + u"\u00b0" + longitude[1] + "'" + longitude[2] + '"' + " " + str(longitudeRef).split("=")[1][1:-1]
## Printing out, might need to be modified if other format needed
## i just simple put tabs here to make nice columns
print " \n A text file has been created with the following information \n"
print "GPS EXIF data for " + imagefilename
print "Latitude:\t" + latitude
print "Longitude:\t" + longitude
print "Altitude:\t" + str(alt) + " m"
except Exception, e: # complain if the GPS reading went wrong, and print the exception
print "Missing GPS info for " + imagefilename
print e
# Create a new file or **overwrite an existing file**
text_file = open('textfile.txt', 'w')
text_file.write("Latitude" + latitude)
# Close the output file
text_file.close()
except Exception, e: # complain if the GPS reading went wrong, and print the exception
print "Error processing image " + imagefilename
print e;
我看到的错误是:
text_file = open('textfile.txt','w')
^
SyntaxError: invalid syntax
答案 0 :(得分:1)
你可以列表错误吗?......行:
print " \n A text file has been created with the following information \n"
print "GPS EXIF data for " + imagefilename
似乎是错误的表格
编辑:您发布的代码 - 其中一条跟踪 - 也是错误的表格。
答案 1 :(得分:1)
文件打开位于第一个try块内。除了阻止之外,它在第二次尝试之外。将它移到第一次尝试除了块之外或增加缩进以将它们包括在第一个try块中。它应该在那里工作正常。
同样移动(增加缩进)try中的两个print语句。
这对你有用:
#!/usr/bin/env python
#standard module, needed as we deal with command line args
import sys
from fractions import Fraction
import pyexiv2
#checking whether we got enough args, if not, tell how to use, and exits
#if len(sys.argv) != 2 :
# print "incorrect argument, usage: " + sys.argv[0] + ' <filename>'
# sys.exit(1)
#so the argument seems to be ok, we use it as an imagefile
imagefilename = sys.argv[1]
#trying to catch the exceptions in case of problem with the file reading
try:
metadata = pyexiv2.metadata.ImageMetadata(imagefilename)
metadata.read();
#trying to catch the exceptions in case of problem with the GPS data reading
try:
latitude = metadata.__getitem__("Exif.GPSInfo.GPSLatitude")
latitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLatitudeRef")
longitude = metadata.__getitem__("Exif.GPSInfo.GPSLongitude")
longitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLongitudeRef")
# get the value of the tag, and make it float number
alt = float(metadata.__getitem__("Exif.GPSInfo.GPSAltitude").value)
# get human readable values
latitude = str(latitude).split("=")[1][1:-1].split(" ");
latitude = map(lambda f: str(float(Fraction(f))), latitude)
latitude = latitude[0] + u"\u00b0" + latitude[1] + "'" + latitude[2] + '"' + " " + str(latitudeRef).split("=")[1][1:-1]
longitude = str(longitude).split("=")[1][1:-1].split(" ");
longitude = map(lambda f: str(float(Fraction(f))), longitude)
longitude = longitude[0] + u"\u00b0" + longitude[1] + "'" + longitude[2] + '"' + " " + str(longitudeRef).split("=")[1][1:-1]
## Printing out, might need to be modified if other format needed
## i just simple put tabs here to make nice columns
print " \n A text file has been created with the following information \n"
print "GPS EXIF data for " + imagefilename
print "Latitude:\t" + latitude
print "Longitude:\t" + longitude
print "Altitude:\t" + str(alt) + " m"
except Exception, e: # complain if the GPS reading went wrong, and print the exception
print "Missing GPS info for " + imagefilename
print e
# Create a new file or **overwrite an existing file**
text_file = open('textfile.txt', 'w')
text_file.write("Latitude" + latitude)
# Close the output file
text_file.close()
except Exception, e: # complain if the GPS reading went wrong, and print the exception
print "Error processing image " + imagefilename
print e;