Reputation: 450
'[[[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n ...\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]]'
I have an string array like above. How can i remove all that " ' " (i mean i want it converts into just array type not String-array.)
I want an array looks like this:
[[[-2048, -2048,-2048, ..., -2048, -2048, -2048], [-2048, -2048, -2048, ..., -2048, -2048, -2048] [-2048 -2048 -2048 ... -2048 -2048 -2048] ... [-2048 -2048 -2048 ... -2048 -2048 -2048] [-2048 -2048 -2048 ... -2048 -2048 -2048] [-2048 -2048 -2048 ... -2048 -2048 -2048]][[-2048 -2048 -2048 ... -2048 -2048 -2048] [-2048 -2048 -2048 ... -2048 -2048 -2048] [-2048 -2048 -2048 ... -2048 -2048 -2048] ... [-2048 -2048 -2048 ... -2048 -2048 -2048] [-2048 -2048 -2048 ... -2048 -2048 -2048]]]
Upvotes: 1
Views: 488
Reputation: 522016
This may be a bit overkill, but a safe way to parse this is to define a custom parser using, e.g., pyparsing
:
from pyparsing import *
num_expr = Word('-' + nums, nums).setParseAction(lambda t: int(t[0]))
array_expr = nestedExpr('[', ']', num_expr)
d = '[[[-2048 -2048]\r\n [-2048 -2048]]]'
print(array_expr.parseString(d).asList()[0])
# [[[-2048, -2048], [-2048, -2048]]]
Upvotes: 1
Reputation:
Warning: eval() can be used to execute arbitrary Python code. You should never use eval() with untrusted strings. (See Security of Python's eval() on untrusted strings?)
eval('variable='+'your string here')
That function runs a string-type piece of code. You should be very careful about this practice. It is highly discouraged to code like that if avoidable. You may have high security and stability breaches if the string is not exactly what you expect. It is interesting as something that Python has, but I would tell you to work out a way around the problem in a different way. If you provide more information we may help you out.
Also I would tell you if you can to obtain that string in JSON format and then use Python's native JSON parser; that'd be much better practice.
Edit
I have just noticed that your string is not parseable for Python, even if you executed the code, because you don't have the appropriate commas as mentioned by other users in the comment box above. You would need to parse that and then call eval
, which is even more complicated and discouraged, though definitely possible.
Edit 2
A way to add a comma before each space and then execute the code as mentioned couuld easily be done by calling str.replace(" ", ", ")
Upvotes: 0
Reputation: 12005
Use re.sub
to remove the unnecessary \r\n
and add comma wherever necessary and then use ast.literal_eval
to convert the cleaned up string to list
>>> import ast
>>> import re
>>> s = '[[[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n ...\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]\r\n\r\n [[-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n ...\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]\r\n [-2048 -2048 -2048 ... -2048 -2048 -2048]]]'
>>> s = s.replace(' ...', '') # Not needed for your original string
>>> l = ast.literal_eval(re.sub(r'(\d?)(?:\r\n)*\s+', r'\1, ', s))
>>> print (l)
[[[-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048]], [[-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048]], [[-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048]], [[-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048]], [[-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048]], [[-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048], [-2048, -2048, -2048, -2048, -2048, -2048]]]
Upvotes: 0