2013-06-28 68 views
-2

我的代碼完美地工作,但我希望它將值寫入文本文件。當我嘗試這樣做時,我會得到'無效的語法'。當我使用python shell時,它可以工作。所以我不明白爲什麼它不能在我的腳本中工作。無法寫入文本文件python 2.7無效語法?

我敢打賭,這是愚蠢的,但爲什麼不輸出數據到文本文件?

#!/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

請勿包括*實際的錯誤*你看。我們現在必須猜測什麼可能是錯的。 –

+0

看起來你正在混合標籤和空格。在你的腳本上運行'python -tt'來計算你的縮進已經走到哪裏了。 –

+0

查看*前面的*行,確保右括號,大括號和括號的數目等於括號,括號和括號的數目。 –

回答

1

文件打開位於第一個try塊內。這是除了塊之外的第二次嘗試。將它移出第一次嘗試以外的塊或增加縮進以將它們包括在第一次嘗試塊中。它應該在那裏工作得很好。

還要在try中同時移動(增加縮進)兩個打印語句。

這會爲你工作:

#!/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; 
+0

謝謝,現在就開始工作吧。你是對的。感謝你和每個幫助過我的人。 – user2519572

1

會爲你而製表錯了...該行:

print " \n A text file has been created with the following information \n" 
print "GPS EXIF data for " + imagefilename  

似乎錯列

編輯:您發佈的代碼 - 其中一個跟蹤表也是錯誤的。

+0

可能是SO上的可視化問題,因爲代碼使用製表符和空格 –