Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I remove hex values in a python string with regular expressions?
    text
    copied!<p>I have a cell array in matlab</p> <pre><code>columns = {'MagX', 'MagY', 'MagZ', ... 'AccelerationX', 'AccelerationX', 'AccelerationX', ... 'AngularRateX', 'AngularRateX', 'AngularRateX', ... 'Temperature'} </code></pre> <p>I use <a href="http://www.scipy.org/Cookbook/hdf5_in_Matlab" rel="nofollow">these scripts</a> which make use of matlab's hdf5write function to save the array in the hdf5 format.</p> <p>I then read in the the hdf5 file into python using pytables. The cell array comes in as a numpy array of strings. I convert to a list and this is the output:</p> <pre><code>&gt;&gt;&gt;columns ['MagX\x00\x00\x00\x08\x01\x008\xe6\x7f', 'MagY\x00\x7f\x00\x00\x00\xee\x0b9\xe6\x7f', 'MagZ\x00\x00\x00\x00\x001', 'AccelerationX', 'AccelerationY', 'AccelerationZ', 'AngularRateX', 'AngularRateY', 'AngularRateZ', 'Temperature'] </code></pre> <p>These hex values pop into the strings from somewhere and I'd like to remove them. They don't always appear on the first three items of the list and I need a nice way to deal with them or to find out why they are there in the first place.</p> <pre><code>&gt;&gt;&gt;print columns[0] Mag8� &gt;&gt;&gt;columns[0] 'MagX\x00\x00\x00\x08\x01\x008\xe6\x7f' &gt;&gt;&gt;repr(columns[0]) "'MagX\\x00\\x00\\x00\\x08\\x01\\x008\\xe6\\x7f'" &gt;&gt;&gt;print repr(columns[0]) 'MagX\x00\x00\x00\x08\x01\x008\xe6\x7f' </code></pre> <p>I've tried using a regular expression to remove the hex values but have little luck.</p> <pre><code>&gt;&gt;&gt;re.sub('(\w*)\\\\x.*', '\1', columns[0]) 'MagX\x00\x00\x00\x08\x01\x008\xe6\x7f' &gt;&gt;&gt;re.sub('(\w*)\\\\x.*', r'\1', columns[0]) 'MagX\x00\x00\x00\x08\x01\x008\xe6\x7f' &gt;&gt;&gt;re.sub(r'(\w*)\\x.*', '\1', columns[0]) 'MagX\x00\x00\x00\x08\x01\x008\xe6\x7f' &gt;&gt;&gt;re.sub('([A-Za-z]*)\x00', r'\1', columns[0]) 'MagX\x08\x018\xe6\x7f' &gt;&gt;&gt;re.sub('(\w*?)', '\1', columns[0]) '\x01M\x01a\x01g\x01X\x01\x00\x01\x00\x01\x00\x01\x08\x01\x01\x01\x00\x018\x01\xe6\x01\x7f\x01' </code></pre> <p>Any suggestions on how to deal with this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload