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.

John Smith
  • 71
  • 1
  • 8

3 Answers3

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.

Underoos
  • 4,708
  • 8
  • 42
  • 85
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.

Nastor
  • 638
  • 4
  • 15
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"
KittoMi
  • 411
  • 5
  • 19