2
4

2 に答える 2

3

You want to avoid using echo in this case; write your input directly to the stdin pipe of the Popen() object instead.

Do make sure your environment is set to the correct locale so that grep knows to parse the input as UTF-8:

env = dict(os.environ)
env['LC_ALL'] = 'en_US.UTF-8'
p = subprocess.Popen(['grep', '-E', mainconf["MSG_FORMAT"][msgtype,fieldname]], stdin=subprocess.PIPE, env=env)
p.communicate(datarecord[fieldname])
if p.returncode:
     return fieldname
于 2013-03-24T15:13:52.507 に答える
0

Just to add to Martijn Pieters' answer, the solution he has suggested will fail in the case of an empty input string (unlike the original function, grep will fail to match an empty string even if the regexp would have permitted it). Therefore a complete implementation of the original function would be:

if (msgtype,fieldname) in mainconf["MSG_FORMAT"]:
        if not datarecord[fieldname]:
            if not regex.search(mainconf["MSG_FORMAT"][msgtype,fieldname],datarecord[fieldname],regex.UNICODE):
                return fieldname
        else:               
            curenv = os.environ
            curenv['LC_ALL']="en_US.UTF-8"
            check = subprocess.Popen(['grep','-E', mainconf["MSG_FORMAT"][msgtype,fieldname]], stdin=subprocess.PIPE, env=curenv, stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
            check.communicate (datarecord[fieldname]) 
            if check.returncode:
                return fieldname
return None

This works as regex matching works fine on an empty string.

于 2013-03-26T05:09:09.217 に答える