Python Forum
END in file after f.write ?!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
END in file after f.write ?!
#1
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
END
2018_06_30_20_30_19.arw 8.0 250 47 2 10/10
END
2018_06_30_20_35_17.arw 8.0 250 47 2 10/10
END
2018_06_30_20_40_18.arw 8.0 250 46 2 10/10
END
2018_06_30_20_45_17.arw 8.0 250 45 2 13/10
END
2018_06_30_20_50_19.arw 8.0 250 40 2 13/10
END
2018_06_30_20_55_19.arw 8.0 250 38 2 13/10
END
2018_06_30_21_00_19.arw 8.0 250 38 2 13/10
END
2018_06_30_21_05_23.arw 8.0 250 24 2 32/10
END
2018_06_30_21_10_22.arw 8.0 250 23 2 32/10
END
2018_06_30_21_15_21.arw 8.0 250 23 2 32/10

all looks good, but i dont want the "END" ?! anyone knows were this is comming from?
Reply
#2
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"
Output:
xxx u xxx u xxx u xxx u
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#3
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]}')
Reply
#4
@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/shutterspeed
Label: Shutter Speed
Readonly: 0
Type: RADIO
Current: 1/1250
END
pi@raspberry:~
the 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]" ?
Reply
#5
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))
Output:
<class 'str'> pi@raspberry:~ $ gphoto2 --get-config /main/capturesettings/shutterspeed Label: Shutter Speed Readonly: 0 Type: RADIO Current: 1/1250 END xxxx pi@raspberry:~ <class 'list'> 7 ['pi@raspberry:~ $ gphoto2 --get-config /main/capturesettings/shutterspeed', 'Label: Shutter Speed', 'Readonly: 0', 'Type: RADIO', 'Current: 1/1250', 'END xxxx', 'pi@raspberry:~ '] Dictionary follows: <class 'dict'> 5 {'pi@raspberry': '~ ', 'Label': ' Shutter Speed', 'Readonly': ' 0', 'Type': ' RADIO', 'Current': ' 1/1250'} Shutter speed = 1/1250.
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 404 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,501 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,428 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,499 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,587 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,897 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  How to write in text file - indented block Joni_Engr 4 6,438 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  Upgrading from 2 to 3 and having file write problems KenHorse 2 1,478 May-08-2022, 09:47 PM
Last Post: KenHorse
  Cursor write 3rd file empty paulo79 3 1,871 Mar-10-2022, 02:51 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020