zhi_huo 发表于 2022-8-14 10:39

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

本帖最后由 zhi_huo 于 2022-8-14 14:15 编辑

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

情况:从本论坛的悬赏中看到要求:从txt中删除指定长度的字符。
      例如:从:shfsdk2434jfhsdf            E389EBF0A6AA1CD9C9F976EEB4350F20                            23423                            BF9477540C8AA9C6B3131B2723B3C17B
删除长度为32的字符,像BF9477540C8AA9C6B3131B2723B3C17B
于是想用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)

具体错误代码:
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

兄弟使用sublime编辑器 + 正则表达式不必代码快

zhi_huo 发表于 2022-8-14 10:56

grekevin 发表于 2022-8-14 10:43
contents.remove('content')
content 引号去掉
去掉也不行
['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

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)

zhi_huo 发表于 2022-8-14 11:21

grekevin 发表于 2022-8-14 11:18
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
in_file = 'x.txt'
out_file = 'out.txt'



大佬,能把我看看悬赏贴吗?
https://www.52pojie.cn/thread-1675156-1-1.html
页: [1] 2 3
查看完整版本: 【python】从txt中删除指定长度的字符,报错查因【已解决】