END in file after f.write ?! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: END in file after f.write ?! (/thread-11267.html) |
END in file after f.write ?! - hhanswurster - Jun-30-2018 Hey Guys, i try to log some settings every time my script runs. # write log entry f = open("/media/pi/6CD8-2DB41/images/log/log.txt","a") f.write("\n" + str(newest) +" " + str(g_f) + " " + str(g_iso)+ " " + str(luxvis) + " " + str(fiso) + " " + str(output[-1])) f.close() print "wrote log"output after severl runs: Quote:2018_06_30_20_25_25.arw 8.0 400 48 2 8/10 all looks good, but i dont want the "END" ?! anyone knows were this is comming from? RE: END in file after f.write ?! - ljmetzger - Jun-30-2018 My speculation is that '\nEND' is part of one of your variables since you provided incomplete code. See the following code example: # write log entry output = "uuuuu" f = open("aaa.txt","a") f.write("\n" + "xxx " + str(output[-1])) f.close() print "wrote log" When you have a problem like this, one of the easiest ways to solve the problem is to start with something that is known to work, and then start adding additional items one at a time. Don't be afraid to write lots of extra things either to the log file or the the screen with a 'print' statement as debugging/diagnostic tools.Lewis RE: END in file after f.write ?! - Larz60+ - Jul-01-2018 also, the method can be improved: with open('log.txt', 'a') as log: log.write('\n{} {} {} {} {} {}'.format(newest, g_f, g_iso, luxvis, fiso, output[-1])) print('wrote log')if you are using python 3.6 or newer, you can replace the write with: log.write(f'\n{newest} {g_f} {g_iso} {luxvis} {fiso} {output[-1]}') RE: END in file after f.write ?! - hhanswurster - Jul-01-2018 @ljmetzger you were right :) problem is the output[-1] variable. i do this this way: # get current shutterspeed from camera command = check_output(["/usr/local/bin/gphoto2","--get-config","/main/capturesettings/shutterspeed"]) output = command.decode().strip() output = output.split(' ')output of gphotot2 is: Quote:pi@raspberry:~ $ gphoto2 --get-config /main/capturesettings/shutterspeedthe splitted output: Quote:[u'Label:', u'Shutter', u'Speed\nReadonly:', u'0\nType:', u'RADIO\nCurrent:', u'1/1250\nEND'] i only need the "1/1250". any other idea than "str(output[-1]" ? RE: END in file after f.write ?! - ljmetzger - Jul-01-2018 I am assuming gphoto2 is the contents of your file, which I simulated using a string. I converted the contents of the string into a dictionary, using a colon : as the parse character.I am relatively new to Python, so I imagine there is a more efficient way to do this. mydata = """pi@raspberry:~ $ gphoto2 --get-config /main/capturesettings/shutterspeed Label: Shutter Speed Readonly: 0 Type: RADIO Current: 1/1250 END xxxx pi@raspberry:~ """ # Data as string print(type(mydata)) print(mydata) #Convert data to a list mydata = mydata.split("\n") print("") print("") print(type(mydata), len(mydata)) print(mydata) #Convert data to a dictionary mydict = {} for row in mydata: try: mystring = row.split(":") mydict[mystring[0]] = mystring[1] except: pass print("") print("") print("Dictionary follows:") print(type(mydict), len(mydict)) print(mydict) # Get 'Shutter speed' from the dictionary print("") shutter_speed = mydict.get('Current', 'There is no Shutter speed') print("Shutter speed = {}.".format(shutter_speed)) NOTE: In the following line, the 'no shutter speed text' is returned if the dictionary key value Current does not exist.shutter_speed = mydict.get('Current', 'There is no Shutter speed')Lewis |