Python Forum

Full Version: This project a little over my head, would love some help.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have the following data:

CONST robtarget robttarget1:=[[-42277.480909368,-4997.36320197,2332.380745999],[0.347787091,-0.799426288,0.217080241,0.439133144],[0,0,0,0],[-35700.001525879,180.000020668,2200.000095367,0,9E9,9E9]];
CONST robtarget robttarget2:=[[-41999.578125,-4703.04296875,2485.273193359],[0.382563147,-0.862604063,0.215021279,0.251645621],[0,0,0,0],[-35700.001525879,180.000020668,2200.000095367,0,9E9,9E9]];
CONST robtarget robttarget3:=[[-41823.44921875,-4460.583984375,2579.253662109],[0.178574824,-0.942134265,0.267469035,0.09462755],[0,0,0,0],[-35700.001525879,180.000020668,2200.000095367,0,9E9,9E9]];

Each line is a robot target for an ABB robot.
I need to find these specific values towards the end of each line:

-35700.001525879,180.000020668,2200.000095367,0,9E9,9E9]];
-35700.001525879,180.000020668,2200.000095367,0,9E9,9E9]];
-35700.001525879,180.000020668,2200.000095367,0,9E9,9E9]];

With the left values, -35700.00, -35700.00, -35700, I apply absolute value > then replace.
With the right values, 180.00, 180.00, 180.00, I subtract 180 > then replace.

so far all I have is loop through each line of a file, and output the type. So I know each line is a string.

I don't know the next step. If you could help point towards the next step that would be awesome!

#!/usr/bin/env python

import re

axis = open("robots.txt", "r")

for line in axis:
        print(type(line))
not entirely sure what u wanted but below is just a string extraction and conversion to list part

for line in axis:
    stringlist = re.compile('.*(\[\[.*\]\]).*').search(line)
    stringtolist = eval(stringlist.group(1))
    ...
u can then access each list (stringtolist) and perform math operation as usual
@ka06059 - eval? really? please, don't do it...
@michoff - in the example you round the numbers to 2 decimal places? is that ok?
line = 'CONST robtarget robttarget1:=[[-42277.480909368,-4997.36320197,2332.380745999],[0.347787091,-0.799426288,0.217080241,0.439133144],[0,0,0,0],[-35700.001525879,180.000020668,2200.000095367,0,9E9,9E9]];'

def process_line(line):
    line_to_list = line.split(',')
    item11, item12 = line_to_list[11:13]
    n = len(item12.split('.')[-1])
    templ = '{{:.{}f}}'.format(n)
    if item11.startswith('[-'):
        line_to_list[11] = '[{}'.format(item11[2:])
    line_to_list[12] =  templ.format(float(item12)-180)
    return ','.join(line_to_list)

print(process_line(line))