2011-09-08 34 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