[英]How to replace the colons in this text using Python?
I have a file which looks like 我有一个看起来像的文件
1::12::33::1555
1::412::1245::23444
and so on. 等等。 I need to get rid of the last argument, and replace the colons with commas. 我需要摆脱最后一个参数,并用逗号替换冒号。 I have tried: 我努力了:
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()
but the back reference doesn't work, and I just end up with a file with commas.. 但是后面的引用不起作用,而我最终得到一个带逗号的文件。
What I'm hoping to achieve is: 我希望实现的是:
1,12,33
1,412,1245
Backreferences will only be interpolated with a raw string. 向后引用将仅使用原始字符串进行插值。
re.sub(r'(.+)::(.+)::(.+)::(.+)', r"\1,\2,\3", content)
You could also do this using purely strings/lists 您也可以使用纯字符串/列表来执行此操作
"\n".join([",".join(y.split('::')[:-1]) for y in content.split("\n")])
You could use the CSV library like so (embedding the CSV for simplicity): 您可以像这样使用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])
This outputs: 输出:
1,12,33
1,412,1245
Can you just use simple string functions? 您可以只使用简单的字符串函数吗?
line = '1::412::1245::23444'
s = s.replace('::',',')
# content stored in a list
content = s.split(',')[:-1]
In Python 2.6: 在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)
In Python 2.7 > 在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)
This would give you the string you need: 这将为您提供所需的字符串:
line = '1::412::1245::23444'
line_list = line.split('::')
new_line = ','.join(line_list[:-1])
print new_line
>> 1,412,1245
It doesn't look like you truly need regex for this. 看起来您并不是真的需要正则表达式。 What I would do is split the line using ::
as a delimiter, then drop the last item and re-insert commas. 我要做的是使用::
作为分隔符来分割行,然后删除最后一项并重新插入逗号。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.