V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Persimmon08
V2EX  ›  Python

求助!找不到 Python 代码 bug

  •  
  •   Persimmon08 · 96 天前 · 1713 次点击
    这是一个创建于 96 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码功能很简单,如下所示

    1. 从名为 nameURL.txt 的文件中读取以英文逗号分隔的每一行,每行包含文件名和对应的链接。

    2. 从名为 remote_filename.txt 的文件中读取已下载的文件名列表。

    3. 遍历 nameURL.txt 中的每一行,检查文件名是否在已下载文件列表中。

    4. 如果文件名不在已下载文件列表中,则将该文件名和对应的链接写入名为 undownload_mp3.txt 的文件。

    download_checker.py

    # 读取 nameURL.txt 中的文件名和链接
    with open('nameURL.txt', 'r', encoding='utf-8') as file:
        name_url_lines = file.readlines()
    
    # 读取 remote_filename.txt 中已下载的文件名(去除后缀)
    with open('remote_filename.txt', 'r', encoding='utf-8') as file:
        downloaded_files = [line.strip('.mp3') for line in file.read().splitlines()]
    
    # 遍历每一行,检查文件名是否在已下载文件列表中,如果不在则写入 undownload_mp3.txt
    with open('undownload_mp3.txt', 'w', encoding='utf-8') as file:
        for line in name_url_lines:
            filename, url = line.split(',')
            if filename not in downloaded_files:
                file.write(f'{filename.strip()},{url.strip()}\n')
    
    print('未下载的文件名和链接已写入 undownload_mp3.txt 文件。')
    

    测试遇到的问题,undownload_mp3.txt中竟然存在少量remote_filename.txt中的存储的已下载的文件名,例如 第一行数据中的文件名The-Joshua-Generation-grows-up

    实在找不到问题出在哪里,特来请教大家,谢谢

    nameURL.txt链接: https://github.com/deyunwanxin/rawData/blob/main/nameURL.txt

    remote_filename.txt链接: https://github.com/deyunwanxin/rawData/blob/main/remote_filename.txt

    undownload_mp3.txt链接: https://github.com/deyunwanxin/rawData/blob/main/undownload_mp3.txt

    20 条回复    2024-01-23 21:13:27 +08:00
    yiguanxianyu
        1
    yiguanxianyu  
       96 天前
    可以把有问题的几行单独提出来 debug
    WoofZJ
        2
    WoofZJ  
       96 天前
    strip 不是这样用的,它你传”.mp3"实际上会把结尾有这四个字符之一就一直清除到不含,The-Joshua-Generation-grows-up.mp3 变成了 The-Joshua-Generation-grows-u
    watry
        3
    watry  
       96 天前
    line.strip('.mp3') 删除的不是'.mp3'字符串,而是其中的任意字符,所以会把'p.mp3'一起去掉
    WoofZJ
        4
    WoofZJ  
       96 天前
    字符串有个 removesuffix 方法,应该用这个 line.strip('.mp3')=>line.removesuffix('.mp3')
    Persimmon08
        5
    Persimmon08  
    OP
       96 天前
    @WoofZJ 谢谢大佬指点,万分感谢
    Persimmon08
        6
    Persimmon08  
    OP
       96 天前
    @Persimmon08 谢谢大佬,感谢指教
    MiketsuSmasher
        7
    MiketsuSmasher  
       96 天前
    楼市其他人已经解释得很清楚了,移除后缀不应该用 line.strip('.mp3'),看看官方文档,这个方法做的跟你想的根本不是一回事: https://docs.python.org/zh-cn/3/library/stdtypes.html#str.strip

    要移除后缀,应该改成 line.removesuffix('.mp3'): https://docs.python.org/zh-cn/3/library/stdtypes.html#str.removesuffix
    Persimmon08
        8
    Persimmon08  
    OP
       96 天前
    @yiguanxianyu 学艺不精,让大家见笑了
    Persimmon08
        9
    Persimmon08  
    OP
       96 天前
    @MiketsuSmasher 谢谢指点,我去好好看看文档
    NoOneNoBody
        10
    NoOneNoBody  
       96 天前
    1. name_url_lines downloaded_files 转为 set 做交集或差集更方便
    2. 去除末端应该用 rstript 而不是 stript ,避免前端也匹配的不严谨情况
    3. 去除末端一段字符串应该用正则,stripe/rstript 的参数是字符集,相当于{'.', 'm', 'p', '3'}无序任一,而不是有序的'.mp3'字符串,楼上已经说清楚了。而且,如果是 windows 系统,因为文件名大小写不区分,为了同时匹配大小写的情况,更应该用正则;其他系统则视乎需求
    Persimmon08
        11
    Persimmon08  
    OP
       96 天前
    @NoOneNoBody 非常感谢,又学到了很多东西
    yushenglin
        12
    yushenglin  
       96 天前
    好久没写 python 了,以前常用 os.path.splitext 来提前文件名和后缀,挺简单的
    l1xnan
        13
    l1xnan  
       96 天前
    路径处理还是上 pathlib 库省事
    PTLin
        14
    PTLin  
       96 天前
    https://www.v2ex.com/t/971492#reply12 此时此刻恰如彼时彼刻
    Persimmon08
        15
    Persimmon08  
    OP
       96 天前
    @yushenglin 果断选择这种方式
    Persimmon08
        16
    Persimmon08  
    OP
       96 天前
    @l1xnan 又学到了一种新方法,知道了一个新的库,大家懂得好多啊
    julyclyde
        17
    julyclyde  
       96 天前
    @PTLin 971492 居然才 137 天之前吗?我觉得已经过了好久了……
    Persimmon08
        18
    Persimmon08  
    OP
       96 天前
    @PTLin 学习路上不孤单,哈哈
    CHchenkeyi
        19
    CHchenkeyi  
       95 天前
    好吧,我之前都是用 rstrip('.') 去切割,看来还麻烦了
    Persimmon08
        20
    Persimmon08  
    OP
       94 天前
    @CHchenkeyi 也是一个不错的办法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2885 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:43 · PVG 15:43 · LAX 00:43 · JFK 03:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.