Python 3.10从播放列表文件中读取,第一行的错误第一字符

发布于 2025-02-03 03:56:38 字数 1639 浏览 1 评论 0原文

文件: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|

文件:read_playlist_file.py.py

import os
import sys

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

输出:

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

#
#
#
#
#
#
#

预期输出:

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

您可以在第一行中看到,没有#print。

该文件是由Python3.10创建的。 也许您无法再现此问题。 也许这是一个UTF-BOM角色问题。

此代码有效:

import io

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

第一个代码的问题是什么,什么是适当的解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-02-10 03:56:38

“也许这是一个UTF-BOM角色问题。”是的,就是这样。您可以在命令行上使用EG HexDump -c检查您的文件。前3个字节应为0xef,0xbb,0xbf,如果是一个utf8-with-bom文件。

“这是什么合适的解决方案?”您所做的完全正确。 encoding ='utf_8_sig'用于utf8-with-bom文件。

"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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文