我需要从 Windows XP 上的文本文件中删除数字。我是 python 的新手,刚安装它用于数据清理。
我把测试文件存放在C:\folder1\test1.txt
test1.txt 上的上下文只有 1 行:
This must not b3 delet3d, but the number at the end yes 134411
我想创建一个包含 result1.txt 的文件
This must not b3 delet3d, but the number at the end yes
到目前为止,这是我尝试过的
import os
fin = os.open('C:\folder1\test1.txt','r')
我收到以下错误:
TypeError: an integer is required.
我不确定它期望的是什么整数。
能否请您告诉我如何进行编程以获得我想要的结果。非常感谢您的帮助。
最佳答案
您正在 os
模块中使用 open()
,它采用数字文件模式。您需要内置的 open()
函数。此外,字符串中的反斜杠在 Python 中具有特殊含义;如果您真的是反斜杠,则需要将它们加倍。尝试:
fin = open('C:\\folder1\\test1.txt','r')
关于python:删除文件中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7351939/