Skip to content
Advertisement

Python 3.10 read from playlist file, wrong first character of first line

File: main_nested.aimppl

#Name:Διάφορα εκκλησιαστικά
#Cursor:-1
#Flags:2047
#Group:/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/mp3/|1
#Track:1|/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/mp3/ΥΜΝΟΙ/ΑΓΙΟΙ ΑΓΓΕΛΟΙ.mp3||||ΑΓΙΟΙ ΑΓΓΕΛΟΙ|0|0|||0|0|0|
#Track:1|/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/mp3/ΥΜΝΟΙ/Αγνή Παρθένε Δέσποινα.mp3||||Αγνή Παρθένε Δέσποινα|0|0|||0|0|0|
#Track:1|/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/.aimppl/nested/ΑΝΑΣΤΑΣΙΜΑ.aimppl||||ΑΝΑΣΤΑΣΙΜΑ|0|0|||0|0|0|
#Track:1|/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/.aimppl/nested/ΥΜΝΟΙ.aimppl||||ΥΜΝΟΙ|0|0|||0|0|0|

File: read_playlist_file.py

import os
import sys

with open("main_nested.aimppl") as f:
    lines = f.readlines()
    for line in lines:
        print(line[0])

Output:

chris@chris-Inspiron-3847:~/Desktop$ python3 read_playlist_file.py 

#
#
#
#
#
#
#

Expected output:

chris@chris-Inspiron-3847:~/Desktop$ python3 read_playlist_file.py 
#
#
#
#
#
#
#
#

As you can see in the first line there is no # print.

This file was created from python3.10. Maybe you can’t reproduce this problem. Maybe it’s a utf-bom character issue.

This code works:

import io

f = io.open('main_nested.aimppl', 'rt', encoding='utf_8_sig')
lines = f.readlines()
for line in lines:
    print(line[0])

What’s the problem with the first code, and what it’s the appropriate solution?

Advertisement

Answer

“Maybe it’s a utf-bom character issue.” Yes, that’s it. You can check your file with e.g. hexdump -c on the command line. The first 3 bytes should be 0xEF, 0xBB, 0xBF if it’s a utf8-with-BOM file.

“what it’s the appropriate solution?” What you did is exactly right. encoding='utf_8_sig' is for utf8-with-BOM files.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement