File: main_nested.aimppl
JavaScript
x
9
1
#Name:Διάφορα εκκλησιαστικά
2
#Cursor:-1
3
#Flags:2047
4
#Group:/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/mp3/|1
5
#Track:1|/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/mp3/ΥΜΝΟΙ/ΑΓΙΟΙ ΑΓΓΕΛΟΙ.mp3||||ΑΓΙΟΙ ΑΓΓΕΛΟΙ|0|0|||0|0|0|
6
#Track:1|/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/mp3/ΥΜΝΟΙ/Αγνή Παρθένε Δέσποινα.mp3||||Αγνή Παρθένε Δέσποινα|0|0|||0|0|0|
7
#Track:1|/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/.aimppl/nested/ΑΝΑΣΤΑΣΙΜΑ.aimppl||||ΑΝΑΣΤΑΣΙΜΑ|0|0|||0|0|0|
8
#Track:1|/home/chris/Documents/papinhio-player/src/python+/menu-1/playlists/test-playlists/.aimppl/nested/ΥΜΝΟΙ.aimppl||||ΥΜΝΟΙ|0|0|||0|0|0|
9
File: read_playlist_file.py
JavaScript
1
8
1
import os
2
import sys
3
4
with open("main_nested.aimppl") as f:
5
lines = f.readlines()
6
for line in lines:
7
print(line[0])
8
Output:
JavaScript
1
10
10
1
chris@chris-Inspiron-3847:~/Desktop$ python3 read_playlist_file.py
2
3
#
4
#
5
#
6
#
7
#
8
#
9
#
10
Expected output:
JavaScript
1
10
10
1
chris@chris-Inspiron-3847:~/Desktop$ python3 read_playlist_file.py
2
#
3
#
4
#
5
#
6
#
7
#
8
#
9
#
10
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:
JavaScript
1
7
1
import io
2
3
f = io.open('main_nested.aimppl', 'rt', encoding='utf_8_sig')
4
lines = f.readlines()
5
for line in lines:
6
print(line[0])
7
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.