2 に答える
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
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.