Python Forum
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord
#11
Do this now will it not break if some song have trouble and write all good ones.
try:
    # Now actually write the thing.
    f = open(OUTPUT_DIRECTORY + song['title'].replace('/','') + '.pro5','w')
    f.write ( to_write + FooterBlock )
    f.close()
except:
    pass
There is a problem if title has Unicode eg Egile è Degno,not songs lyrics text which can take almost any Unicode.
Then can try to fix it in code or if it just few songs fix in database as eg Egile e Degno will fix it.

Edit did look a little more a this.
Import and replace line comment out.
import codecs
 
# Now actually write the thing.
f = codecs.open(OUTPUT_DIRECTORY + song['title'].replace('/','') + '.pro5','w', 'utf-8')
#f = open(OUTPUT_DIRECTORY + song['title'].replace('/','') + '.pro5','w')
Now will songs with Unicode in title also work.
Reply
#12
(Apr-22-2023, 10:57 PM)snippsat Wrote:
(Apr-22-2023, 09:19 PM)ctrldan Wrote: the real songs.sqlite is with 1600 elements.
If you can post the file i can take a look.
I can be problem with one or more songs as error point to song Egile è Degno.
Look where the song is in database with eg DB Browser for SQLite.

Yes of course, this is the real data Songs.sqlite I know that Egli è Degno is the first element of the database, so I think that the problem is not for the song
Reply
#13
Hi, can we change part of the code with another one more up to date compatible with the last version of Python?
There are new methods to convert this stuff in a propresenter db?
Reply
#14
(Apr-23-2023, 07:54 AM)nickystark Wrote: can we change part of the code with another one more up to date compatible with the last version of Python?
You can try to convert python 2 code to python 3 with the 2to3 tool that comes with Python.
nickystark likes this post
Reply
#15
(Apr-23-2023, 07:54 AM)nickystark Wrote: Hi, can we change part of the code with another one more up to date compatible with the last version of Python?
There are new methods to convert this stuff in a propresenter db?
(Apr-23-2023, 08:32 AM)Gribouillis Wrote: You can try to convert python 2 code to python 3 with the 2to3 tool that comes with Python.
In this case it will be difficult,the problem is not the syntax as i tried to change to run in Python 3.10.
The problem is that it need a older sqilte version that's in Python 2 to read songs.sqlite.
Could try to move over sqilte module,but i don't want to mess with that.

Here is the fixed version,as i run in Python 2.7.
C:\Python27
λ python song.py
OpenLP to Pro-Presenter 5 converter.

Loading Database:
  songs.sqlite

  Cool.  1644 songs loaded.

And writing the new files to
  C:/songs/

Finished.
So one problem or maybe not is that it now write over song title if it has same name.
As example adon olam and adon olam same title name,with a small change in the lyric maybe some fix up.
So it write 1243 songs.

The tricky part.
# Now actually write the thing.
f = codecs.open(OUTPUT_DIRECTORY + song['title'].replace('/','').replace('?', '').replace('"', '') + '.pro5', 'w', encoding='utf_8', errors='replace')
#f = open(OUTPUT_DIRECTORY + song['title'].replace('/','') + '.pro5','w')
f.write(to_write + FooterBlock )
f.close()

Reply
#16
(Apr-23-2023, 10:15 AM)snippsat Wrote:
(Apr-23-2023, 07:54 AM)nickystark Wrote: Hi, can we change part of the code with another one more up to date compatible with the last version of Python?
There are new methods to convert this stuff in a propresenter db?
(Apr-23-2023, 08:32 AM)Gribouillis Wrote: You can try to convert python 2 code to python 3 with the 2to3 tool that comes with Python.
In this case it will be difficult,the problem is not the syntax as i tried to change to run in Python 3.10.
The problem is that it need a older sqilte version that's in Python 2 to read songs.sqlite.
Could try to move over sqilte module,but i don't want to mess with that.

Here is the fixed version,as i run in Python 2.7.
C:\Python27
λ python song.py
OpenLP to Pro-Presenter 5 converter.

Loading Database:
  songs.sqlite

  Cool.  1644 songs loaded.

And writing the new files to
  C:/songs/

Finished.
So one problem or maybe not is that it now write over song title if it has same name.
As example adon olam and adon olam same title name,with a small change in the lyric maybe some fix up.
So it write 1243 songs.

The tricky part.
# Now actually write the thing.
f = codecs.open(OUTPUT_DIRECTORY + song['title'].replace('/','').replace('?', '').replace('"', '') + '.pro5', 'w', encoding='utf_8', errors='replace')
#f = open(OUTPUT_DIRECTORY + song['title'].replace('/','') + '.pro5','w')
f.write(to_write + FooterBlock )
f.close()


Thank you.. better 1242 songs respect 0.. thank you for your help
Reply
#17
(Apr-23-2023, 11:51 AM)ctrldan Wrote: # Now actually write the thing.f = codecs.open(OUTPUT_DIRECTORY + song['title'].replace('/','').replace('?', '').replace('"', '') + '.pro5', 'w', encoding='utf_8', errors='replace')#f = open(OUTPUT_DIRECTORY + song['title'].replace('/','') + '.pro5','w')f.write(to_write + FooterBlock )f.close()

I tried and it works! But the verses are inverted.. For example if in a song there are 3 verses, the first verse is verse 3, is possibile to invert the current order with the correct order?
Reply
#18
(Apr-23-2023, 12:06 PM)ctrldan Wrote: I tried and it works! But the verses are inverted.. For example if in a song there are 3 verses, the first verse is verse 3, is possibile to invert the current order with the correct order?
Maybe not so simple if look at this code,as author has comment in funny syntax Rolleyes
So i added a reversed(),that should do it.
# Prepare Verses to write: (funny python syntax...)
to_write += ''.join(
    reversed([VerseBlock(Verbose_names(v['type']) + ' ' + v['label'],
                v['type'],
                v['text'],
                color = CHORUS_COLOR if v['type'] == 'c'\
                                     else VERSE_COLORS[min(MAX_VERSE_COLORS-1,int(v['label']))])
    for v in ParseLyric(song['lyrics'])]))
Reply
#19
(Apr-23-2023, 01:41 PM)snippsat Wrote: Maybe not so simple if look at this code,as author has comment in funny syntax Rolleyes
So i added a reversed(),that should do it.

I continue to write on this thread.. I used your fixing and it worked very well, I tried to implemente the code, as I can, with the results that now there are songs with the alternative title (and for this more songs converted), I want to try to rename the songs with the same title in this way
      
i=1  
while xml_attr(song['title']) in database_song:
            try:
                title=song['title']+" "+ str(i)              #for print the duplicate
                id=song['id']
                updateSqliteTable(id, title)
                print(id)
                if xml_attr(song['title']) in database_song:
                    TypeError
            except:
                i+=1
database_song+=[xml_attr(song['title'])]
                
(I insert this code inside for song in songs:
where updateSqliteTable(id, title) is:
def updateSqliteTable(id, title):
            try:
                sqliteConnection = sql.connect(OPENLP_DATABASE)
                cursor = sqliteConnection.cursor()
                print("Connected to SQLite")

                sql_update_query = """Update OPENLP_DATABASE set title = ? where id = ?"""
                data = (title, id)
                cursor.execute(sql_update_query, data)
                sqliteConnection.commit()
                print("Record Updated successfully")
                cursor.close()

            except sql.Error as error:
                print("Failed to update sqlite table", error)
            finally:
                if sqliteConnection:
                    sqliteConnection.close()
                    print("The sqlite connection is closed")
I think I asked the program the wrong things (I found this code on the web), I don't know how to modify the query well.
I don't know if is clear what I want to do... If there is a duplicated songs ('SongA' for example), the duplicated songs will be called: SongA 1, if there are 3 SongA, the last one will be called SongA 2.
Thank you for your help, I don't know if is necessary to open a new thread for this question... let me know
Reply
#20
(Apr-23-2023, 10:22 PM)ctrldan Wrote: I think I asked the program the wrong things (I found this code on the web), I don't know how to modify the query well.
I don't know if is clear what I want to do... If there is a duplicated songs ('SongA' for example), the duplicated songs will be called: SongA 1, if there are 3 SongA, the last one will be called SongA 2.
So this is maybe the simplest way to do it,add a unique number to song['title'] then it would write all song.
The code you found is old and also written in way that make it hard too read,so if not well verse in Python(also old stuff) this a difficult task.
Here is the change.
for index, song in enumerate(songs, start=1): 
    song_authors = get_song_authornames(song['id'])
    # Find the copyright year (this would be briefer in perl...)
    get_year = re.match(__re_year, xml_attr(song['copyright']))
    if get_year != None:
        copyright_year = get_year.group()
        copyright = song['copyright'][4:].strip()
    else:
        copyright_year = ''
        copyright = ''

    # Prepare Header Block to write:
    to_write = ( HeaderBlock(Name          = xml_attr(song['title']),
                         CCLILicenceNumber = xml_attr(song['ccli_number']),
                         Notes             = xml_attr(song['comments']),
                         CCLICopyRightInfo = xml_attr(copyright_year),
                         Publisher         = xml_attr(copyright),
                         Authors           = xml_attr(song_authors)) )

    # Prepare Verses to write: (funny python syntax...)
    to_write += ''.join(
        reversed([VerseBlock(Verbose_names(v['type']) + ' ' + v['label'],
                    v['type'],
                    v['text'],
                    color = CHORUS_COLOR if v['type'] == 'c'\
                                         else VERSE_COLORS[min(MAX_VERSE_COLORS-1,int(v['label']))])
        for v in ParseLyric(song['lyrics'])]))

    try:
        # Now actually write the thing.
        f = codecs.open(OUTPUT_DIRECTORY + song['title'].replace('/','').replace('?', '').replace('"', '') + str(index) + '.pro5', 'w', encoding='utf_8', errors='replace')
        #f = open(OUTPUT_DIRECTORY + song['title'].replace('/','') + '.pro5','w')
        f.write(to_write + FooterBlock )
        f.close()
    except:
        print ('Oh dear. Something went wrong with writing "' + song['title'] + '".\nSorry.\n\n' +
               'Maybe it\'s a file-write permissions issue?\n' +
               'You could try changing where this script is writing to, it\'s the line\n' +
               '  OUTPUT_DIRECTORY="' + OUTPUT_DIRECTORY + '"\n' +
               'that will need to be changed.' )
        try:
            f.close()
        except:
            pass
        exit();
(Apr-23-2023, 10:22 PM)ctrldan Wrote: Thank you for your help, I don't know if is necessary to open a new thread for this question... let me know
No new thread as has to see all done to help,as this old code usually no one would help as Python 2 has been dead💀 for over 3 years.
If could fix the sqlite read problem,then could maybe rewrite to Python 3.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encode/decode to show correct country letters in a CTk combobox janeik 2 726 Sep-02-2023, 09:46 AM
Last Post: janeik
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 16: invalid cont Melcu54 3 5,027 Mar-26-2023, 12:12 PM
Last Post: Gribouillis
  [SOLVED] [Debian] UnicodeEncodeError: 'ascii' codec Winfried 1 1,036 Nov-16-2022, 11:41 AM
Last Post: Winfried
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 19,059 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 2,495 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,741 Apr-29-2022, 12:36 PM
Last Post: Armandito
  UnicodeEncodeError caused by print when program runs from Popen SheeppOSU 5 2,939 Jan-13-2022, 08:11 AM
Last Post: SheeppOSU
  [UnicodeEncodeError from smtplib] yoohooos 0 3,394 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  ASCII-Codec in Python3 [SOLVED] AlphaInc 4 6,173 Jul-07-2021, 07:05 PM
Last Post: AlphaInc
  [solved] unexpected character after line continuation character paul18fr 4 3,431 Jun-22-2021, 03:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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