Python 根据字典的内容替换文本的里的字符串 20
有个字典默认为a_dict={'apple':'1','tree':'2','123456':'3'}文本文件里面为apple123456123456tree想要的结果是...
有个字典默认为a_dict = {'apple':'1','tree':'2','123456':'3'}
文本文件里面为apple 123456
123456 tree
想要的结果是1 3
3 2
我的代码是这样的,但是好像不成功
>>> a_dict = {'apple':'1','tree':'2','123456':'3'}
>>> input_file = open(r'd:\test_body.txt')
>>> output_file = open(r'd:\test_body1.txt','w')
>>> for i in input_file.readlines():
for k in a_dict:
output_file.write(i.replace(k,a_dict[k]))
>>> output_file.close()
>>> input_file.close()
所以想问问怎样才能在原文件或者新建文件上根据字典的内容替换文本里的字符串?
两种写法分别是啥。另外我这个错在那里? 展开
文本文件里面为apple 123456
123456 tree
想要的结果是1 3
3 2
我的代码是这样的,但是好像不成功
>>> a_dict = {'apple':'1','tree':'2','123456':'3'}
>>> input_file = open(r'd:\test_body.txt')
>>> output_file = open(r'd:\test_body1.txt','w')
>>> for i in input_file.readlines():
for k in a_dict:
output_file.write(i.replace(k,a_dict[k]))
>>> output_file.close()
>>> input_file.close()
所以想问问怎样才能在原文件或者新建文件上根据字典的内容替换文本里的字符串?
两种写法分别是啥。另外我这个错在那里? 展开
3个回答
推荐于2018-03-04
展开全部
我觉得你好想想多了。即然全文替换。哪么你只需要遍历你的dict
a_dict = {'apple':'1','tree':'2','123456':'3'}
input_file = open(r'd:\test_body.txt',"r").read()();
for key,value in a_dict.items():
input_file=input_file.replace(key,value);
这样子不行吗?
a_dict = {'apple':'1','tree':'2','123456':'3'}
input_file = open(r'd:\test_body.txt',"r").read()();
for key,value in a_dict.items():
input_file=input_file.replace(key,value);
这样子不行吗?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
# encoding: utf-8
import re
patt = re.compile(r'\S+')
a_dict = {'apple':'1','tree':'2','123456':'3'}
content = '''# 第三行信息非严格匹配,不替换
apple 123456
123456 tree
appletree 12345678'''
a_dictkset = set(a_dict.keys())
def findkeys(ln):
return a_dictkset & set(patt.findall(ln))
def repln(ln, ks):
for k in ks:
ln = ln.replace(k, a_dict.get(k, k))
return ln
for ln in content.splitlines():
ln = repln(ln, findkeys(ln))
print ln
>python -u "rptest.py"
# 第三行信息非严格匹配,不替换
1 3
3 2
appletree 12345678
>Exit code: 0 Time: 0.051
import re
patt = re.compile(r'\S+')
a_dict = {'apple':'1','tree':'2','123456':'3'}
content = '''# 第三行信息非严格匹配,不替换
apple 123456
123456 tree
appletree 12345678'''
a_dictkset = set(a_dict.keys())
def findkeys(ln):
return a_dictkset & set(patt.findall(ln))
def repln(ln, ks):
for k in ks:
ln = ln.replace(k, a_dict.get(k, k))
return ln
for ln in content.splitlines():
ln = repln(ln, findkeys(ln))
print ln
>python -u "rptest.py"
# 第三行信息非严格匹配,不替换
1 3
3 2
appletree 12345678
>Exit code: 0 Time: 0.051
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
>>> "%(pwd)s" % params
'secret'
>>> "%(pwd)s is not a good password for %(uid)s" % params
'secret is not a good password for sa'
>>> "%(database)s of mind, %(database)s of body" % params
'master of mind, master of body'
多看书
>>> "%(pwd)s" % params
'secret'
>>> "%(pwd)s is not a good password for %(uid)s" % params
'secret is not a good password for sa'
>>> "%(database)s of mind, %(database)s of body" % params
'master of mind, master of body'
多看书
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询