Error while trying to convert string into a dictionary (python) [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to convert the following into a dictionary
my_string='{"values": [["searchText", "aaaa bbbb"], ["url", "https://aaa.com/w/Try.do?one=yes&isFate=true&active=2&entry=aaaa+bbbb"], ["ip", "11.22.333.44, 12.12.3.111"], ["userId", "4569874"], ["id", 69875462145], ["sessionId", "25F8D032D8FGJ5ED023F56ZZ.TRYING207"], ["pageNumber", null], ["dateCreated", "2018-03-06 17:23:55"], ["page_id", 771790], ["dateModified", "2018-03-06 17:23:55"], ["device_id", 168]]}'
But when I do this:
ast.literal_eval(my_string)
It raises the following error:
ValueError: malformed node or string: <_ast.Name object at 0x000001F2C0536080>
Can you help me with this please ?
Thank you in advance
PS: I'm using python 3.6

The easiest way to load your data is to use json.loads function as was mentioned in comment to your question.
But if you strongly want to use ast.literal_eval function, the reason of ValueError is this part of my_string:
... ["pageNumber", null] ...
There is no null constant, class or type in Python. Change it to None.

Related

Date time index sets wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a Dataframe called file, but the column 'Time valid (UTC)' has its data values in the following way: '25-10-2017 03:30' which does not conform with datetime index. Now I tried to correct this date by using
file['Time valid (UTC)']=pd.to_datatime(file['Time valid (UTC)'],format='%d-%m-%Y %H:%M')
However, the following errors pops up
file['Time valid (UTC)']=pd.to_datatime(file['Time valid (UTC)'],format='%d-%m-%Y %H:%M') AttributeError: 'module' object has no attribute 'to_datatime'
Any idead about how to fix this or how to set the datetime correctly at once with pd.DatetimeIndex?
You have to correct the function name to pd.to_datetime()

Reinititalize 'str' as a type [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have used str as a variable. Now, I would like to convert an int into a string. For this, normally I would use str(10). What should I do in this case?
You can just delete the variable to get str() back:
othervarname = str
del str
Find and replace "str" with "sensibleNameForYourVariable", then use str(i) to convert integers to strings.
Use the code below if you had used str as a variable. This will solve your problem. The keyword str() internally uses __str__() function so better use that function only
str = 1
str.__str__()
Output
'1'
As in your case you should use.
Retrieve it from __builtins__.
str = "shadowed"
assert __builtins__.str(3) == "3"

Python bytearray.fromhex fails when string has "16" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
In Python 2.7, the function bytearray.fromhex(string) gives:
ValueError: non-hexadecimal number found in fromhex() arg at position x
when the string has '16' in it, like in for example:
0200FF001603000E30D03, 0200FF001603004401A03
It's like failing if it was decimal and was '84102' just for having '10', the base, in it.
How can I avoid that error?
There is no problem with 16 existed in the string, the problem is that you try to encode odd length strings - try any valid string with even length and you will see that it is works.

changing variable type in list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to take 4 numbers as string and split them into 4 elements. and convert each of them into integer and save it in a list. What am I doing wrong here?
mask = "250.250.0.0"
string = mask.split(".")
toInt = [int[i] for i in string]
print(toInt)
error message says type object is not subscriptable
use int(i) instead of int[i], int is not subscriptable
int is a builtin-class that provides no implementation for subscription in array-like fashion.
To convert to an integer you should use int like this:
toInt = [int(i) for i in string]

ValueError in string to datetime conversion [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to convert strings (which I have in a list) to datetimes.
I tried this:
import datetime
list = [
'12-October-2014-18:30',
'12-October-2014-19:30',
'12-October-2014-20:00',
'12-October-2014-20:30',
'13-October-2014-00:30',
]
for item in list:
item_time = datetime.datetime.strptime(item, "%m-%B-%Y-%H-%M")
print item_time
but I get this error:
ValueError: time data '12-October-2014-18:30' does not match format '%m-%B-%Y-%H-%M'
I dont see the Error, can somebody help please?
There are two things wrong.
for '%m-%B-%Y-%H-%M', your date should be: '12-October-2014-18-30'
In some you have 12th where it should just be 12 and in all of them you have 18:30 but need it to be 18-30

Categories

Resources