吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 976|回复: 22
收起左侧

[求助] 【python】从txt中删除指定长度的字符,报错查因【已解决】

[复制链接]
zhi_huo 发表于 2022-8-14 10:39
本帖最后由 zhi_huo 于 2022-8-14 14:15 编辑

问题:报错查因。 ValueError:list.remove(x): x not in list

情况:从本论坛的悬赏中看到要求:从txt中删除指定长度的字符。

      例如:从:shfsdk2434jfhsdf
              E389EBF0A6AA1CD9C9F976EEB4350F20               
              23423               
              BF9477540C8AA9C6B3131B2723B3C17B

删除长度为32的字符,像BF9477540C8AA9C6B3131B2723B3C17B

于是想用python来解决,写了下列代码,学艺不精,请大佬们指点:

[Python] 纯文本查看 复制代码
filename = 'x.txt'

with open(filename, 'r') as file_object:
    contents = file_object.readlines()
    print(contents)
    
    for content in contents:
        content = content.strip('\n')
        print(content)
        if len(content)==32:
            contents.remove('content')

    print(contents)


具体错误代码:

[PowerShell] 纯文本查看 复制代码
PS C:\Users\ZhiHu\Documents\python_application>  c:; cd 'c:\Users\ZhiHu\Documents\python_application'; & 'C:\Users\ZhiHu\AppData\Local\Programs\Python\Python310\python.exe' 'c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '55704' '--' 'c:\Users\ZhiHu\Documents\python_application\txt_delete.py' 
['shfsdk2434jfhsdf\n', 'E389EBF0A6AA1CD9C9F976EEB4350F20\n', '23423\n', 'BF9477540C8AA9C6B3131B2723B3C17B']
shfsdk2434jfhsdf
E389EBF0A6AA1CD9C9F976EEB4350F20
Traceback (most recent call last):
  File "C:\Users\ZhiHu\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\ZhiHu\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main    
    run()
  File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file    runpy.run_path(target, run_name="__main__")
  File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\ZhiHu\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "c:\Users\ZhiHu\Documents\python_application\txt_delete.py", line 11, in <module>
    contents.remove('content')
ValueError: list.remove(x): x not in list

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

grekevin 发表于 2022-8-14 10:43
contents.remove('content')  
content 引号去掉
grekevin 发表于 2022-8-14 10:48
 楼主| zhi_huo 发表于 2022-8-14 10:56
grekevin 发表于 2022-8-14 10:43
contents.remove('content')  
content 引号去掉

去掉也不行
[PowerShell] 纯文本查看 复制代码
['shfsdk2434jfhsdf\n', 'E389EBF0A6AA1CD9C9F976EEB4350F20\n', '23423\n', 'BF9477540C8AA9C6B3131B2723B3C17B']
shfsdk2434jfhsdf
E389EBF0A6AA1CD9C9F976EEB4350F20
Traceback (most recent call last):
  File "C:\Users\ZhiHu\Documents\python_application\txt_delete.py", line 11, in <module>
    contents.remove(content)
ValueError: list.remove(x): x not in list
 楼主| zhi_huo 发表于 2022-8-14 10:57
grekevin 发表于 2022-8-14 10:48
兄弟使用sublime编辑器 + 正则表达式不必代码快

确实快,我想问一下python能不能处理这种问题?
grekevin 发表于 2022-8-14 11:05
zhi_huo 发表于 2022-8-14 10:57
确实快,我想问一下python能不能处理这种问题?

可以
content = content.strip('\n') 这句你把\n去掉了,但是列表内的\n还在,去掉\n的元素肯定不在列表内
知道怎么改了吧
 楼主| zhi_huo 发表于 2022-8-14 11:16
本帖最后由 zhi_huo 于 2022-8-14 11:18 编辑
grekevin 发表于 2022-8-14 11:05
可以
content = content.strip('\n') 这句你把\n去掉了,但是列表内的\n还在,去掉\n的元素肯定不在列表 ...

原来是这里出了问题。

但是我想不出来怎么改。

我不知道怎么去掉\n后,把content写入到contents

grekevin 发表于 2022-8-14 11:18
[Python] 纯文本查看 复制代码
in_file = 'x.txt'
out_file = 'out.txt'


with open(out_file, 'w') as fo:
    with open(in_file, 'r') as fi:
        for line in fi:
            if len(line.strip())==32:
                fo.write(line)

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
zhi_huo + 1 + 1 谢谢@Thanks!

查看全部评分

 楼主| zhi_huo 发表于 2022-8-14 11:21
grekevin 发表于 2022-8-14 11:18
[mw_shl_code=python,true]in_file = 'x.txt'
out_file = 'out.txt'

OK,明白了。如果需要去掉32字符的话,
改成   if len(line.strip())  != 32:
                fo.write(line)
 楼主| zhi_huo 发表于 2022-8-14 11:22
grekevin 发表于 2022-8-14 11:18
[mw_shl_code=python,true]in_file = 'x.txt'
out_file = 'out.txt'

大佬,能把我看看悬赏贴吗?
https://www.52pojie.cn/thread-1675156-1-1.html
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 07:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表