2012-02-03 88 views
-1

我有一個包含類似以下信息的文件:更換的每個實例「 - 」從文件

1/1/2010 
1-2-3-4 
1 
1/2/2010 
1-5-6-7 
111 
etc. 

我希望能夠寫這其中'-'每個實例都替換爲另一個文件','。我一直在使用str.split('-')嘗試並實現收到此錯誤
TypeError: must be string or read-only character buffer, not list
我也使用str.replace('-', 'i')嘗試後,我不能寫一個列表文件,但所有這一切給了我另外一個錯誤
TypeError: replace() takes at least 2 arguments (1 given)
任何人都可以點我在正確的方向?

我還要指出,我試圖首先使用str.split()的原因是因爲這最終都將被放入一個字典,像這樣

{0:[['1/1/2010], ['1', '2', '3', '4'], ['1']], 1: [['1/2/2010'], ['1', '5', '6', '7'], ['111']], etc.} 

只是一個更新... 我發現使用未提及的正則表達式執行此操作的簡單方法。

>>> def test(filename): 
import re 
input_file = open(filename, 'r') 
output_file = open('test.txt', "w") 
for line in input_file: 
    line = line.strip() 
    line = re.sub('-', ',', line) 
    output_file.write(line) 
    output_file.write('\n') 
input_file.close() 
output_file.close() 
+1

這是否被集成到另一個程序中?你需要爲這一步使用python嗎?如果不是爲什麼不是shell命令:cat file | 「tr」 - 「」,「 – Benedict 2012-02-03 18:43:06

回答

3
def replaceAll(infilepath, outfilepath): 
    infile = open(infilepath) 
    outfile = open(outfilepath, 'w') 
    for line in infile: 
     outfile.write(line.replace('-', ',')) 
    infile.close() 
    outfile.close() 

編輯:這裏是這樣做的稍微更Python的方式:

def replaceAll(infilepath, outfilepath): 
    with open(infilepath) as infile, open(outfilepath, 'w') as outfile: 
     for line in infile: 
      outfile.write(line.replace('-', ',')) 
+0

工作。感謝您提供一些比'replace(' - ',',')'更多的東西。 – Timmay 2012-02-03 22:35:48

0

嘗試使用此

replace("-",","); 
+0

也許是因爲我不知道足夠的Python,但是當我在這個文件中使用嘗試閱讀行時,它不起作用。即'line.replace(「 - 」,「,」)'拷貝到文件的所有內容是它正在讀取的文件的第一行。 – Timmay 2012-02-03 22:22:47

+0

從Python文檔中查看此頁面的最底部:http://docs.python.org/library/string.html – 2012-02-03 22:25:27

+0

'replace'會返回一個新的'str'。你調用它的那個沒有改變。這很不明顯,並導致醜陋的代碼,如'message = message.replace(「 - 」,「,」)'。在另一個說明中,在這個答案的最後的分號不屬於。 – ArtOfWarfare 2013-10-08 20:34:28

1

我建議你使用正則表達式來解析您的文件格式是否爲這種一致的,這裏有一個例子:

import os, re 

data = """1/1/2010 
1-2-3-4 
1 
1/2/2010 
1-5-6-7 
111""" 

regex = re.compile(os.linesep.join([r'(\d{1,2}/\d{1,2}/\d{4})', 
            r'(\d+-\d+-\d+-\d+)', 
            r'(\d+)'])) 
groups = {} 
n = 0 
for match in regex.finditer(data): 
    groups[n] = [[match.group(1)], match.group(2).split('-'), [match.group(3)]] 
    n += 1 

>>> groups 
{0: [['1/1/2010'], ['1', '2', '3', '4'], ['1']], 1: [['1/2/2010'], ['1', '5', '6', '7'], ['111']]} 

至於你的問題(如何用逗號替換連字符),假設您已將文件讀入字符串data,則可以使用以下行代替所有連字符:

data = data.replace('-', ',') 

你也可以分割字符串的所有'-'然後用','.join(),雖然使用str.replace()簡單:

data = ','.join(data.split('-')) 
+0

我只用我的數據樣本(9行)嘗試了上面的內容,並返回了{}'。 – Timmay 2012-02-03 18:56:14

+0

如果你的行有任何尾隨空格,或者文件有奇怪的行結尾,這可能不起作用,你能用你試過的行編輯你的問題嗎? – 2012-02-03 19:16:46

+0

我已經嘗試過它,但是當我輸入組時,它仍然給我提供'{}'。你在使用2.7.2嗎? – Timmay 2012-02-03 22:21:23

0

我創建了一個測試文件給你看。

>>> contents = open('test.txt', 'r').read() 
>>> contents 
'blah-blah\nsomething-\n' 

使用字符串替換方法與,取代的-出現:首先,在閱讀

>>> contents_replaced = contents.replace('-', ',') 
>>> contents_replaced 
'blah,blah\nsomething,\n' 

寫回了一個文件,並在閱讀它,以確保它已更新:

>>> open('test2.txt', 'w').write(contents_replaced) 
>>> open('test2.txt', 'r').read() 
'blah,blah\nsomething,\n'