Python替换文本Fi中定义中的单词

2024-05-29 04:49:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个为cobol编写的旧informix数据库。所有字段都是用代码编写的,所以我的SQL查询看起来像。在

SELECT uu00012 FROM uu0001;

这很难读懂。在

我有一个文本文件,其字段定义如下

^{pr2}$

我想把代码换成更英文的名字。在上面运行一个python脚本,保存一个英文名称的文件。在

最好的办法是什么?在


Tags: 代码from脚本数据库sql定义名字select
3条回答

如果每一部分都是一个单独的单词,re.sub绝对是这里的方法:

#create a mapping of old vars to new vars.
with open('definitions') as f:
    d = dict( [x.split() for x in f] )

def my_replace(match):
    #if the match is in the dictionary, replace it, otherwise, return the match unchanged.
    return d.get( match.group(), match.group() )

with open('inquiry') as f:
    for line in f:
        print re.sub( r'\w+', my_replace, line ) 

从概念上讲

我可能会首先建立一个编码映射->英语(在内存或o

然后,对于地图中的每一个编码,扫描你的文件并用对应的英语代码替换。在

infile = open('filename.txt','r')
namelist = []
for each in infile.readlines():
    namelist.append((each.split(' ')[0],each.split(' ')[1]))

这将为您提供键、值对的列表

我不知道你想对结果做什么,尽管,你需要更明确

相关问题 更多 >

    热门问题