0

I have created a string:

userId = print('{\n    "userId": "1111111111abc",\n    "Message": null,\n    "Time": null"\n}')

Output:

{
    "userId": "1111111111abc",
    "Message": null,
    "Time": null"
}

Now I want to "work" with this string. I want to access/extract the value of UserId - 1111111111abc. But when I start to remove the first and last n characters it says:

userId[3:]

TypeError: 'NoneType' object is not subscriptable

Why I need this? I will get 100+ JSON style strings/texts(?) and will have to extract UserId from it.


UPDATED

Pardon, but I can not find a solution to my question here. If someone can go through my example and then extract the desired ID I would be happy. If it is possible of course.

1
  • Please make a clear explanation, the input, and the output that you are expecting for. Commented Nov 11, 2020 at 5:08

3 Answers 3

0

In python print() function returns None. Although it PRINTS some value, it doesn't return anything.

And you are trying to slice the returned value which is None.

Hence the error TypeError: 'NoneType' object is not subscriptable.

To verify, try printing userId as print(userId).

What you should try instead to access the "userId" is convert your string to json format and access it as you did above.

Sign up to request clarification or add additional context in comments.

Comments

0

That is because print function doesn't actually return the stuff you print, it always returns None. You would have to assign the values to an actual variable, but even if you were to do that with this example string, you couldn't read it with slicing as you're trying to do, since it is simply a string. The best option would be to create a dictionary with "userId", "Message" and "Time" as keys or a jsonified string.

Comments

0

If you assign print function's return to a variable it must be a nothing. If you print the userId in the next line, print(type(userId)) you will get <class 'NoneType'>.

Firstly, you have to assign the input value to a variable, the access it. Though I'm not clear with the question, it might help you out.

Code : as list

userId = ['\n    "userId": "1111111111abc"','\n    "Message": null','\n    "Time": null"\n']
access_userId = userId[0][userId[0].find("\"") : ]
print(access_userId)

Output

"userId": "1111111111abc"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.