2013-04-30 135 views
1

我有一个文件,它看起来像如何使用Python替换此文本中的冒号?

1::12::33::1555 
1::412::1245::23444 

等。我需要摆脱最后一个参数,并用逗号替换冒号。我曾尝试:

myfile = open('words.txt', 'r') 
    content = myfile.read() 
    content = re.sub(r'(.+)::(.+)::(.+)::(.+)', "\1,\2,\3", content) 
    myfile = open('words.txt', 'w') 
    myfile.write(content) 
    # Close the file 
    myfile.close() 

但后面的参考是不行的,我刚刚结束了与逗号文件..

就是我希望实现的是:

1,12,33 
1,412,1245 
+1

请添加一些预期的输出。 – 2013-04-30 21:52:22

+2

'my_string.replace(“::”,“,”)'?不会工作? – 2013-04-30 21:52:44

+2

不是Perl,但仍然相关:http://xkcd.com/1171/ – squiguy 2013-04-30 21:56:58

回答

1

这将使你的字符串,你需要:

line = '1::412::1245::23444' 
line_list = line.split('::') 
new_line = ','.join(line_list[:-1]) 

print new_line 
>> 1,412,1245 
+0

谢谢!很棒! – Siddhartha 2013-04-30 22:12:32

6

反向引用只会插入一个原始字符串。

re.sub(r'(.+)::(.+)::(.+)::(.+)', r"\1,\2,\3", content) 

你也可以做到这一点使用纯字符串/列表

"\n".join([",".join(y.split('::')[:-1]) for y in content.split("\n")]) 
+0

您的意思是[原始字符串](http://docs.python.org/reference/lexical_analysis.html#string-literals)? – 2013-04-30 21:54:51

+0

@LevLevitsky感谢您的更正 – 2013-04-30 21:58:05

+0

谢谢,有没有什么办法可以用sub做呢?分裂也很好。 – Siddhartha 2013-04-30 22:14:23

1

你可以只使用简单的字符串函数?

line = '1::412::1245::23444' 
s = s.replace('::',',') 
# content stored in a list 
content = s.split(',')[:-1] 
+0

这样做并不解释这个文件被作为一个块读入并且有多行 – 2013-04-30 22:16:35

1

在Python 2.6:

with open('words.txt', 'r') as in_file: 
    with open('words_out.txt', 'w') as out_file: 
     for line in in_file: 
      new_line = ','.join(line.split('::')[:-1]) + ',' 
      out_file.write(new_line) 

在Python 2.7>

with open('words.txt', 'r') as in_file, open('words_out.txt', 'w') as out_file: 
    for line in in_file: 
     new_line = ','.join(line.split('::')[:-1]) + ',' 
     out_file.write(new_line) 
2

您可以使用CSV library像这样(为CSV嵌入CSV含蓄):

import StringIO 
import csv 

t = """1::12::33::1555 
1::412::1245::23444""" 

f = StringIO.StringIO(t) 
reader = csv.reader(f, delimiter=':') 
for row in reader: 
    print ",".join(row[0:-1:2]) 

此输出:

1,12,33 
1,412,1245 
+0

不错,我还没有听说过那个图书馆。 – Siddhartha 2013-04-30 22:12:10

+0

'“,”。join(row [0 :: 2])'。似乎不适合'csv'模块。 – 2013-04-30 22:39:55

+0

@StevenRumbalski,我实际上试图在这里一次做两件事,改变加入的字符并删除最后一个元素,所以'',“。join(row [0:-1:2])'可以工作 – 2013-04-30 22:45:32

0

它看起来并不像你真正需要的正则表达式这一点。我会做的是将::作为分隔符分割线,然后删除最后一项并重新插入逗号。

myfile = open('words.txt', 'r') 
content = myfile.read() 
numbers = [int(s) for s in content.split("::")]  #get a list of numbers from the string 
numbers = numbers[0:-1]        #drop last number 
content = "".join([str(n) + ",," for n in numbers]) #coalesce numbers back to string 
myfile = open('words.txt', 'w') 
myfile.write(content) 
myfile.close()