python regex help

This is the place for queries that don't fit in any of the other categories.

python regex help

Postby jdinze » Thu Apr 10, 2014 1:49 pm

Code: Select all
anis_chars = re.compile('\\x1b\[7mlines [0-9]*-[0-9]* \\x1b\[27m\\x1b\[K\\r\\x1b\[K')
def strip_ansi(with_ansi):
   return ansi_chars.sub('', with_ansi)
   
strip_ansi (ssh.before)

ssh.before is some output from a pxssh obj.
the output returns some garbage ansi characters that i need to remove in order to proper parse the output.

sample output
Code: Select all
parts = \x1b[7mlines 24-46 \x1b[27m\x1b[K\r\x1b[KCPU 9
parts = \x1b[7mlines 25-47 \x1b[27m\x1b[K\r\x1b[K Utilization: 57%
parts = Peak Utilization Last Hour: 100% at 2014/04/09 07:07:12
parts = Avg. Utilization Last Hour: 67%
parts =
parts = \x1b[7mlines 48-70 \x1b[27m\x1b[K\r\x1b[K
parts = \x1b[7mlines 49-71 \x1b[27m\x1b[K\r\x1b[KCPU 14
parts = Utilization: 46%

Im using RHEL 5.8 and Python 2.7. the script works just fine when i run it as myself or even after using sudo bash. when i run it as a cron job, the regex doesnt match and i get output as if i didnt use the strip_ansi() function.

Ive done some research and learned that cron uses a different local, which plays a part in the way it handles some characters. so i tried running the cron job like this:
Code: Select all
*/1 **** export LANG=en_US.UTF-8; /pathtomyscript/myscript.py
but no luck. i did confirm that
Code: Select all
 "export LANG=en_US.UTF-8"
actually sets the LANG variable for cron. i also checked for output errors and this script has none.

Is there a different way i can write the regex to consider the fact that cron uses a different locale? I'm also open up to other ideas, as this has been plaguing me for three days.

thanks.
Last edited by Mekire on Thu Apr 10, 2014 2:14 pm, edited 1 time in total.
Reason: First post lock. Code tags added.
jdinze
 
Posts: 1
Joined: Thu Apr 10, 2014 1:40 pm

Re: python regex help

Postby micseydel » Mon Apr 21, 2014 11:25 pm

Could you provide a very simple example of the strings that occur with cron, and what you want out of them? I think the reason you haven't gotten an answer to this is that there is a lot of extraneous information. If you reduce it to a simple regex question, you'll probably get an answer rather quickly.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1113
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests