Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:使用正则表达式定义边界_Python_Replace - Fatal编程技术网

Python:使用正则表达式定义边界

Python:使用正则表达式定义边界,python,replace,Python,Replace,我试图替换字符串中使用边界从参数读取的单词。但对于参数值,我无法实现它。下面是代码的详细信息 paramfile=str(Path('C:\\Users\\as15188\\Desktop\\Python\\Param'))+'\\'+'DataStandardization.txt' with open(paramfile, 'rb') as config_file: configs.load(config_file) filename=str(configs.get((sys

我试图替换字符串中使用边界从参数读取的单词。但对于参数值,我无法实现它。下面是代码的详细信息

paramfile=str(Path('C:\\Users\\as15188\\Desktop\\Python\\Param'))+'\\'+'DataStandardization.txt'
 
 with open(paramfile, 'rb') as config_file:
  configs.load(config_file)
 
 filename=str(configs.get((sys.argv[1])+"SourceFile").data)
 tokenfilename=str(configs.get((sys.argv[1])+"TokenFile").data)
 outputfilename=str(configs.get((sys.argv[1])+"OutputFile").data)
 AccountNameColumnNumber=int(configs.get((sys.argv[1])+"AccountNameColumnNumber").data)
 AccountNameColumnNumber -=1
 NextVal=AccountNameColumnNumber+1

  with open(tokenfilename, 'r') as rep_words:
   rep_list = []
   for rep_line in rep_words:
    rep_list.append(tuple(rep_line.strip().split(',')))

  with open(filename, 'r') as old_text:
   with open(outputfilename, 'w') as new_text:
   
    for read_line in old_text:
     new_line = read_line

     Token=new_line.split('|')[3]
     BeforeAttri='|'.join(new_line.split('|')[0:3])
     AfterAttri='|'.join(new_line.split('|')[4:])
 
     for old_word, new_word in rep_list:
      repStr=re.sub(r '\b' old_word '\b', new_word, Token)
      CompValue=BeforeAttri+'|'+repStr+'|'+AfterAttri
    
     new_text.write(CompValue)

以下是参数值


    old_word = Hosp  
    new_word = Hospital   
    Token  = Manor Drug Medical Hosp

Complete Record in file
SourceID|SourceName|AccountName|AddressLine1|AddressLine2|City|State|ZIP5|ZIP4|PhoneNumber|DeltaFlag|MD5  
1|ABC|Manor Drug Medical And Pharma|5795 N 1st St||Fresno|CA|93710|6203|5594314142|A|e6f9b166284831094453ab0a64f855b3

有人能帮助实现同样的目标吗
样本记录-

请张贴实际代码;由于缺少引号,此代码有语法错误。发布的代码不是有效的python代码。你好,托马斯,我已经用准确的代码更新了帖子