2011-09-08 106 views
4

我需要从Windows XP上的文本文件中删除数字。我是python的新手,刚刚安装它进行数据清理。python:删除文件中的数字

我已经保存在C测试文件:\文件夹1 \ 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. 

我不知道它是什么期待整数。

您能否让我知道如何去编程以获得我想要的结果。非常感谢你的帮助。

+0

我编辑你的问题有点让它看起来更好,但我认为双引号实际上并不是文本文件。你能证实情况是这样吗? –

+0

这个问题从何而来?看起来你并不是互联网上第一个询问它的人:http://www.google.ca/search?q=%22This+must+not+b3+delet3d%2C+but+the+n+++ at + the + end + yes + 134411%22&oq =%22 This + must + not + b3 + delet3d%2C + but + the + number + at + the + end + yes + 134411%22 –

+0

有趣的发现,@DavidWolever。我们都成为巨魔饲养员吗? – StevenV

回答

0
f = open(r'C:\folder1\test1.txt','r') 
8

你的os模块,这需要一个数字文件的方式使用open()。你需要改为内置的open()函数。此外,字符串中的反斜杠在Python中具有特殊含义;如果你真的是反斜杠,你需要把它们加倍。尝试:

fin = open('C:\\folder1\\test1.txt','r') 
+0

您也可以使用:fin = open(r'C:\ folder1 \ test1.txt','r')http: //docs.python.org/reference/lexical_analysis.html#string-literals – six8

+1

或者只是使用正斜杠;) – Voo