Python search string for two numbers, assign to variables - python

I have the following string a part of a file with lines with the same format.
CONST robtarget robttarget1:=[[-42277.480909368,-4997.36320197,2332.380745999],[0.347787091,-0.799426288,0.217080241,0.439133144],[0,0,0,0],[-35700.0,180.0,2200.000095367,0,9E9,9E9]];
I need to access two specific numbers and preform math on them. -35700.0 and 180.0.
I am struggling with getting those specific values and assign them to variables.

Just get rid of the part of the lines that are not the value and use eval to get the rest as a python variable.
The following line will break your string at the '=' character using split (so you will get 2 strings).
It will then get the second part of your string (the one which starts after the '='), remove the final ';' character and use eval to interpret the whole thing:
result = eval(s.split('=')[1][:-1])
Now you'll get a list of lists that you can extract your numbers from easily

You can split the string by the "=" and then use the ast module to convert the string list to a python list object and the use list indexing to fetch the required value
EX:
import ast
A = "CONST robtarget robttarget1:=[[-42277.480909368,-4997.36320197,2332.380745999],[0.347787091,-0.799426288,0.217080241,0.439133144],[0,0,0,0],[-35700.0,180.0,2200.000095367,0,9E9,9E9]];"
A = A.split("=")[1].replace(";", "") #Remove any non python string.
A = ast.literal_eval(A) #Convert string object to list object
print A[-1][0:2] # I have used slice to fetch the required value
Output:
[-35700.0, 180.0]

Related

Why Simple String's Character has So many array Dimensions?

I am currently working on a python's String and List.
When I assign string in variable str="string" and try to access it first character by str[0] it works perfectly and give "s".
But, when I try to find character str[0][0][0][0][0][0] it again gives "s". But when I give str[0][1] it gives an error:
IndexError: string index out of range
Its Correct. My Question is Why Simple String Character has So many array Dimensions? and it did not given any error and print 0 character of string when str[0][0][0][0][0][0]. What is Data Structure of String?
My Code is
str="string"
print((str[0][0][0][0][0][0][0][0])) # Working, but my Question is Why Working
print((str[1][0][0][0][0])) # Working
print((str[2][0][0][0][0])) # Working
print((str[3][0][0][0][0])) # Working
list=["0","p",0]
print(list[0][0][0]) # Working
My Output is:
s
t
r
i
0
Why shouldn't it work?
Indexing a string returns a one element string which is again indexable and returns the same value:
>>> 's'[0]
's'
since it consists of one element, you can continue indexing the zero-element [0] as much as you want.
This is explained in the standard type hierarchy section of the Python Reference manual:
Strings
A string is a sequence of values that represent Unicode code points. All the code points in the range U+0000 - U+10FFFF can be represented in a string. Python doesn’t have a char type; instead, every code point in the string is represented as a string object with length 1.
(Emphasis mine)
Side-note: Don't use names such as str, you mask the built-in str.
In Python a string is a sequence of characters, but characters are 1-char strings.
So if you access 'foobar'[0], you obtain 'f'. Since f is however a string, we can access the first character of that string. Since 'f'[0] is 'f'. As a result if you access a strings s with s[i][0][0][0], you thus keep accessing the first character of the string s.
If you write s[i][1] however, this will error, since s[i] is a one-character string, and thus you can not obtain the second character, since there is no such character.
The string itself is not multidimensional, you simply obtain a new string and call the index of that new string. You can add as many [0]s as you like.
The problem is not in Python, it is due to the fact that you assume there is a char type in Python (based on the title of this question).
A string in Python is an array of essentially single element strings. s[0] simply returns the string 's', not a character. s[0]...[0] can be thought of as an infinite recursion that keeps getting the same single element string, infinitely many times.
You can go as deep as you want: (in this case, in order to do it more than 997 times you will need to modify Python`s default allowed recursion depth)
def string_dive(s, count=0):
if count < 997:
count += 1
return string_dive(s[0], count)
else:
return s
print(string_dive('string'))
# 's'

How to strip letters out of a string and compare values?

I have just learned Python for this project I am working on and I am having trouble comparing two values - I am using the Python xlwt and xlrd libraries and pulling values of cells from the documents. The problem is some of the values are in the format 'NP_000000000', 'IPI00000000.0', and '000000000' so I need to check which format the value is in and then strip the characters and decimal points off if necessary before comparing them.
I have tried using S1[:3] to get the value without alphabet characters, but I get a 'float is not subscriptable' error
Then I tried doing re.sub(r'[^\d.]+, '', S1) but I get a Typerror: expected a string or buffer
I figured since the value of the cell that is being returned via sheet.cell( x, y).value would be a string since it is alphanumeric, but it seems like it must be returned as a float
What is the best way to format these values and then compare them?
You are trying to get the numbers from the strings in the format shown? Like to get 2344 from NP_2344? If yes then use this
float(str(S1)[3:])
to get what you want. You can change float to int.
It sounds like the API you're using is returning different types depending on the content of the cells. You have two options.
You can convert everything to a string and then do what you're currently doing:
s = str(S1)
...
You can check the types of the input and act appropriately:
if isinstance(S1, basestring):
# this is a string, strip off the prefix
elif isinstance(S1, float):
# this is a float, just use it

Parse XML File with Python and get the letter 'u' in every list element

I have a XML file with some elements like this:
<RMS>[14.470156174, 14.470156174, 14.485567944, 14.496014765]</RMS>
I want to get a list with all the elements
So i tried some Regex with the following code:
string = dom.getElementsByTagName('RMS')[0].toxml()
string2 = re.findall("[\-]*[0-9]*\.[0-9]*", string)
Now, when I want to print the list, it looks like this:
[u'14.470156174', u'14.470156174', u'14.485567944', u'14.496014765']
What's going on with the 'u'?
Are there any ideas how to solve the problem?
Thanks for helping.
Strings that start with a u are unicode string literals. Since XML contains unicode data, the XML parser returns your data in the correct type, which is the python unicode() type.
You do not need to remove them, you do not have a problem. You may want to read up on Unicode and Python in the Python Unicode HOWTO but there is no problem here.
Since these are numbers, you can convert the unicode values straight to float instances.
There is no need to use regex here. In fact, your regex may not work for some floats such as 1.4e1.
Since you are using minidom you could do this:
import xml.dom.minidom as minidom
import ast
content = "<RMS>[14.470156174, 14.470156174, 14.485567944, 14.496014765]</RMS> "
dom = minidom.parseString(content)
text = dom.getElementsByTagName('RMS')[0].childNodes[0].wholeText
If you
print(text)
you get
[14.470156174, 14.470156174, 14.485567944, 14.496014765]
but if you
print(repr(text))
you get
u'[14.470156174, 14.470156174, 14.485567944, 14.496014765]'
The u indicates that text is a unicode object, not a str object. Similarly, your code produces a list of unicode objects. When you print a list, Python prints the repr of each of the elements inside the list. This is why you see
[u'14.470156174', u'14.470156174', u'14.485567944', u'14.496014765']
Now upon rereading your question, I see you want a list of the elements in text. Since they are numbers, I assume you want a list of floats. In that case, you could use ast.literal_eval:
values = ast.literal_eval(text)
print(values)
yields
[14.470156174, 14.470156174, 14.485567944, 14.496014765]
where values is a list of floats.

python trying to remove single apostrophe

I'm using a program called CityEngine which has a python element to it.
The problem: I've just called a function on an object and returns me a list of numbers, xyz. I split the xyz into their own names. I also call a function to retrieve a different attribute related to this object to replace the previously retrieved y value.
Now, when I print the y value, it contains numerical characters only apart from decimal place.
When I incorporate the y value into a new list, it's value has single apostrophe around it.
For example, print(y) returns 5.0000000
If I place it like this position[x,y,z] I get a print(position) of [0, '5.000000' , 0]. The program can't read the single apostrophes so ignored the value completely.
I've tried .remove("'","") and .strip() and nothing.
Any help would be appreciated.
Thanks.
That looks more as if the function were not returning a number but a string. So, in order to deal with it, you’ll have to convert the string using either int() or float().
In general, if you do a print(l) on some list of items, each item will be printed with the output of it’s __repr__ method. Convention has it that the __repr__ method of string wraps the string with single apostrophes, whereas numbers do not get wrapped. This is to remove potential ambiguity. Hence, a print(l) which returned
[0, '5.00000', 0.1]
would be a list containing an int, a str and a float.
Convert it to float ... It is a string, so you need to do string to float conversion

Python, string (consisting of variable and strings, concatenated) used as new variable name?

I've been searching on this but am coming up a little short on exactly how to do specifically what i am trying to do.. I want to concatentate a string (I guess it would be a string in this case as it has a variable and string) such as below, where I need to use a variable consisting of a string to call a listname that has an index (from another variable).. I simplified my code below to just show the relevant parts its part of a macro that is replacing values:
toreplacetype = 'type'
toreplace_indx = 5
replacement_string = 'list'+toreplacetype[toreplace_indx]
so... I am trying to make the string on the last line equal to the actual variable name:
replacement_string = listtype[5]
Any advice on how to do this is appreciated
EDIT:
To explain further, this is for a macro that is sort of a template system where I am indicating things in a python script that I want to replace with specific values so I am using regex to do this. So, when I match something, I want to be able to replace it from a specific value within a list, but, for example, in the template I have {{type}}, so I extract this, but then I need to manipulate it as above so that I can use the extracted value "type" to call a specific value from within a list (such as from a list called "listtype") (there is more than 1 list so I need to find the one called "listtype" so I just want to concatenate as above to get this, based on the value I extracted using regex
This is not recommended. Use a dict instead.
vars['list%s' % toreplacetype][5] = ...
Hrm...
globals()['list%s'% toreplacetype][toreplace_indx]
replacement_string = 'list'+toreplacetype+'['+str(toreplace_indx)+']'
will yield listtype[5] when you print it.
You need to basically break it into 5 parts: 1 string variable, 3 strings and an int casted to a string.
I think this is what you are asking?

Categories

Resources