Logging Error

Posted on
Sun Jan 23, 2022 2:48 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Logging Error

I've got a python script that is logging song titles, artist, etc. and while the script works fine, I do run into an error message occasionally. Here is the part of my script that I'm sure is causing the problem:
Code: Select all
Track2 = indigo.variables[142028227].value #just played Track
Artist2 = indigo.variables[918614858].value #just played Artist
Album2 = indigo.variables[374325596].value #Just played Album
RunTime = indigo.variables[118845916].value #math played time

import time
time = time.strftime("%m" "%d"" " "%I"":""%M""%p")
list ="\t\n%s \t%s \t%s \t%s \t%s" %(time, RunTime, Track2, Artist2, Album2)


with open('/Users/kitchen/SynologyDrive/MyLogs/StudioSongs.txt', 'a') as outfile:
    output = list
    outfile.write(output)


and here is the error message:
Code: Select all
Script Error                    embedded script: 'ascii' codec can't encode character u'\xe9' in position 204: ordinal not in range(128)
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 29, at top level
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 204: ordinal not in range(128)

I have no doubt this is because part of the text of one of the fields I'm logging has a character that isn't a value... in this case
André
so half-way guessing I need to convert the text from the fields into strings as opposed to values, yes?

I used this snipped elsewhere on another script:
Code: Select all
time = (indigo.server.getTime().strftime('%Y-%m')) #2022-01

date_string = str(time)

so tried this:
Code: Select all
TrackX = indigo.variables[142028227].value #just played Track
Track2 = str(TrackX)
but this give me the same error messages so I'm guessing this requires a more complex solution. Guessing the UnicodeEncoderError is a big clue so need to somehow convert my un-Unicode text to something else...

Posted on
Sun Jan 23, 2022 3:36 pm
matt (support) offline
Site Admin
User avatar
Posts: 21416
Joined: Jan 27, 2003
Location: Texas

Re: Logging Error

Try changing this line from:

Code: Select all
list ="\t\n%s \t%s \t%s \t%s \t%s" %(time, RunTime, Track2, Artist2, Album2)

to:

Code: Select all
list = u"\t\n%s \t%s \t%s \t%s \t%s" %(time, RunTime, Track2, Artist2, Album2)

Image

Posted on
Sun Jan 23, 2022 6:13 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Logging Error

Hey Matt
Thanks for your reply. I went back to my original script posted here and copied your line in where indicated, but still get the same error message.

Part of the bigger process is to trigger AirFoil to populate variable entries before they are written to the log file. So what are in the variables comes directly from AirFoil, which of course gets the info from the Music app. The error only happens when special characters are in any of the fields I'm logging.

Posted on
Sun Jan 23, 2022 6:27 pm
matt (support) offline
Site Admin
User avatar
Posts: 21416
Joined: Jan 27, 2003
Location: Texas

Re: Logging Error

Can you copy/paste your entire script? The error indicates it is on line 29 but the script you posted isn't that long.

Image

Posted on
Sun Jan 23, 2022 6:36 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Logging Error

Here it is...
Code: Select all
import time
time = (indigo.server.getTime().strftime('%H:%M:%S')) #Start Time
indigo.variable.updateValue(1059249795, time) #write start time to variable

from datetime import datetime
startTimeVariable = indigo.variables[744500332].value  # start time
endTimeVariable = indigo.variables[1059249795].value  # End Time

startTime = datetime.strptime(startTimeVariable, "%H:%M:%S")
endTime = datetime.strptime(endTimeVariable, "%H:%M:%S")

# write just played and running time to txt file

runningTime = endTime - startTime 
indigo.variable.updateValue(118845916, str(runningTime))  # does math = Running time

Track2 = indigo.variables[142028227].value #just played Track
Artist2 = indigo.variables[918614858].value #just played Artist
Album2 = indigo.variables[374325596].value #Just played Album
RunTime = indigo.variables[118845916].value #math played time

import time
time = time.strftime("%m" "%d"" " "%I"":""%M""%p")
list = u"\t\n%s \t%s \t%s \t%s \t%s" %(time, RunTime, Track2, Artist2, Album2)



with open('/Users/kitchen/SynologyDrive/MyLogs/StudioSongs.txt', 'a') as outfile:
    output = list
    outfile.write(output)

Posted on
Sun Jan 23, 2022 6:46 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Logging Error

and even this short version of the script give the same error message when
André
is in the variable]
Code: Select all
Track2 = indigo.variables[142028227].value #just played Track
list = u"\t\n%s" %(Track2)

with open('/Users/kitchen/SynologyDrive/MyLogs/StudioSongs.txt', 'a') as outfile:
    output = list
    outfile.write(output)


Code: Select all
Script Error                    embedded script: 'ascii' codec can't encode character u'\xe9' in position 6: ordinal not in range(128)
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 8, at top level
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 6: ordinal not in range(128)

Posted on
Sun Jan 23, 2022 7:13 pm
FlyingDiver online
User avatar
Posts: 7211
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Logging Error

You can't use %s to substitute a unicode value.

Change

Code: Select all
list = u"\t\n%s" %(Track2)


to
Code: Select all
list = u"\t\n{}".format(Track2)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jan 23, 2022 7:33 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Logging Error

Ok so with this code, I still get the same error message when André is in the variable:
Code: Select all
Track2 = indigo.variables[142028227].value #just played Track
list = u"\t\n{}".format(Track2)

with open('/Users/kitchen/SynologyDrive/MyLogs/StudioSongs.txt', 'a') as outfile:
    output = list
    outfile.write(output)


Code: Select all
Script Error                    embedded script: 'ascii' codec can't encode character u'\xe9' in position 4: ordinal not in range(128)
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 2, at top level
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 4: ordinal not in range(128)

Posted on
Sun Jan 23, 2022 7:46 pm
FlyingDiver online
User avatar
Posts: 7211
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Logging Error

Code: Select all
Track2 = indigo.variables[142028227].value #just played Track
list = u"\t\n{}".format(Track2)

with open('/tmp/StudioSongs.txt', 'a') as outfile:
    output = list.encode("utf8")
    outfile.write(output)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jan 23, 2022 7:48 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Logging Error

Thanks

That does it.

I just need to integrate it into the whole script, but I'm positive I can figure out how to manage that.

THANKS for your help!!


********


So the "list" line gave me a few problems, but thru trial and error, and a bit of intuition, I came up with this which works perfectly.
Code: Select all
list = u"\t\n{} \t{} \t{} \t{} \t{}".format(time, RunTime, Track2, Artist2, Album2)

Thanks again for your help.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests