推荐学习书目
› 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 · Jan 21, 2024 · 5371 views
  •   You need to sign in to view this topic
    This topic created in 1033 days ago, the information mentioned may be changed or developed.

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

    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 replies  •  2024-01-23 21:13:27 +08:00
    yiguanxianyu
    yiguanxianyu
    Jan 21, 2024
    1
    可以把有问题的几行单独提出来 debug
    WoofZJ
    WoofZJ
    Jan 21, 2024
    2
    strip 不是这样用的,它你传”.mp3"实际上会把结尾有这四个字符之一就一直清除到不含,The-Joshua-Generation-grows-up.mp3 变成了 The-Joshua-Generation-grows-u
    watry
    watry
    Jan 21, 2024
    3
    line.strip('.mp3') 删除的不是'.mp3'字符串,而是其中的任意字符,所以会把'p.mp3'一起去掉
    WoofZJ
    WoofZJ
    Jan 21, 2024
    4
    字符串有个 removesuffix 方法,应该用这个 line.strip('.mp3')=>line.removesuffix('.mp3')
    Persimmon08
    Persimmon08
    OP
    Jan 21, 2024
    5
    @WoofZJ 谢谢大佬指点,万分感谢
    Persimmon08
    Persimmon08
    OP
    Jan 21, 2024
    6
    @Persimmon08 谢谢大佬,感谢指教
    MiketsuSmasher
    MiketsuSmasher
    Jan 21, 2024
    7
    楼市其他人已经解释得很清楚了,移除后缀不应该用 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
    Persimmon08
    OP
    Jan 21, 2024
    8
    @yiguanxianyu 学艺不精,让大家见笑了
    Persimmon08
    Persimmon08
    OP
    Jan 21, 2024
    9
    @MiketsuSmasher 谢谢指点,我去好好看看文档
    NoOneNoBody
    NoOneNoBody
    Jan 21, 2024
    10
    1. name_url_lines downloaded_files 转为 set 做交集或差集更方便
    2. 去除末端应该用 rstript 而不是 stript ,避免前端也匹配的不严谨情况
    3. 去除末端一段字符串应该用正则,stripe/rstript 的参数是字符集,相当于{'.', 'm', 'p', '3'}无序任一,而不是有序的'.mp3'字符串,楼上已经说清楚了。而且,如果是 windows 系统,因为文件名大小写不区分,为了同时匹配大小写的情况,更应该用正则;其他系统则视乎需求
    Persimmon08
    Persimmon08
    OP
    Jan 21, 2024
    11
    @NoOneNoBody 非常感谢,又学到了很多东西
    yushenglin
    yushenglin
    Jan 22, 2024
    12
    好久没写 python 了,以前常用 os.path.splitext 来提前文件名和后缀,挺简单的
    l1xnan
    l1xnan
    Jan 22, 2024
    13
    路径处理还是上 pathlib 库省事
    PTLin
    PTLin
    Jan 22, 2024
    14
    https://www.v2ex.com/t/971492#reply12 此时此刻恰如彼时彼刻
    Persimmon08
    Persimmon08
    OP
    Jan 22, 2024
    15
    @yushenglin 果断选择这种方式
    Persimmon08
    Persimmon08
    OP
    Jan 22, 2024
    16
    @l1xnan 又学到了一种新方法,知道了一个新的库,大家懂得好多啊
    julyclyde
    julyclyde
    Jan 22, 2024
    17
    @PTLin 971492 居然才 137 天之前吗?我觉得已经过了好久了……
    Persimmon08
    Persimmon08
    OP
    Jan 22, 2024
    18
    @PTLin 学习路上不孤单,哈哈
    CHchenkeyi
    CHchenkeyi
    Jan 22, 2024
    19
    好吧,我之前都是用 rstrip('.') 去切割,看来还麻烦了
    Persimmon08
    Persimmon08
    OP
    Jan 23, 2024
    20
    @CHchenkeyi 也是一个不错的办法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   986 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 22:02 · PVG 06:02 · LAX 14:02 · JFK 17:02
    ♥ Do have faith in what you're doing.