On 4/28/21 5:57 PM, Bruce Labitt wrote:
> If someone could suggest how to do this, I'd appreciate it.  I've 
> scraped a table of fine thread metric screw parameters from a website.  
> I'm having some trouble with regex (re) separating the numbers.  Have 
> everything working save for this last bit.
> 
> Here is a sample string:
> 
> r1[1] = ' 17.98017.87417.65517.59917.43917.291'
> 
> I'm trying to separate the numbers.  It should read like this:
> 
> 17.980, 17.874, 17.655, 17.599, 17.439, 17.291
> 
> There's more than 200 lines of this, so it would be great to automate 
> it!  Each number has 3 digits of precision, so I want to add a comma and 
> a space after the third digit.
> 
> re.search('(\.)\d{3,3}', r1[1]) returns
> <re.Match object; span=(3, 7), match='.980'> so it found the first instance.
> 
> But, re.sub('(\.)\d{3,3}', '(\.)\d{3,3}, ', r1[1]) yields a KeyError: 
> '\\d' (Python3.8).  Get bad escape \d at position 4.
The second argument [the replacement string] to re.sub(pattern, repl, string) 
is not supposed to
just be a variation of the pattern-matching string that you passed as the first 
argument.

I think the best illustration that I can give here is to just fix this up for 
you:

        re.sub(r'(\.)(\d{3,3})', r'\1\2, ', r1[1])

-- 
Connect with me on the GNU social network! 
<https://status.hackerposse.com/rozzin>
Not on the network? Ask me for more info!
_______________________________________________
gnhlug-discuss mailing list
gnhlug-discuss@mail.gnhlug.org
http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/

Reply via email to