-2

I got a super weird problem: I got a list of strings which looks like this:

batch = ['{"SageMakerOutput":[{"label":"LABEL_8","score":0.9152628183364868},"inputs":"test"}',
 '{"SageMakerOutput":[{"label":"LABEL_8","score":0.9769203066825867},"inputs":"Alles OK"}',
 '{"SageMakerOutput":[{"label":"LABEL_8","score":0.9345938563346863},"inputs":"F"}']

In each entry of the list I want to remove the single quotes "'" but somehow I cannot remove it with .replace():

for line in batch:
    line = line.replace("'","")

I dont get it

soulwreckedyouth
  • 465
  • 1
  • 3
  • 12
  • 4
    Why not just read it as json? `import json; json.loads(line)`? – Sayse Sep 28 '21 at 15:15
  • You want to turn valid JSON string into invalid? – Olvin Roght Sep 28 '21 at 15:15
  • 1
    well, there's no single quotes in the string, but there's a lot of double quotes – Ghost Ops Sep 28 '21 at 15:15
  • 3
    `line = line.replace("'","")` isn't going to mutate `batch`. You are simply assigning a different string to the name `line` in the body of the loop, an assignment which doesn't effect the immutable string in `batch`, and which is lost after that pass through the loop. – John Coleman Sep 28 '21 at 15:17
  • 3
    @GhostOps, please, [don't suggest `eval`](https://stackoverflow.com/q/1832940/4046632) when there are proper tools to deal with this. – buran Sep 28 '21 at 15:19
  • 1
    It appears your string contents is json data, why not use json parser to parse it individually as you loop through the list – Chukwu Remijius Sep 28 '21 at 15:19
  • @buran but why? itd in-built and the OP is, seems like, just dealing with some simple dictionaries. so thats why i suggested that... – Ghost Ops Sep 28 '21 at 15:20
  • Check the link in my comment @GhostOp – buran Sep 28 '21 at 15:20
  • 1
    Each record is not json valid (missing `]`) – Corralien Sep 28 '21 at 15:22
  • 2
    Textbook XY problem. – ddejohn Sep 28 '21 at 15:23
  • 1
    Also https://stackoverflow.com/q/15197673/4046632 @GhostOps – buran Sep 28 '21 at 15:23
  • 3
    As @GhostOps mentioned, there are no `'` in your strings to replace. The single quotes are what encloses your string. Answer this: is `"` part of the string `"abc"`? How would you check if `"` were in that string? You'd have to enclose `"` with a quotation mark in order to treat it as a string, yes? Something like `'"' in "abc"`? Do you see now that the enclosing quotes are not actually part of the string? – ddejohn Sep 28 '21 at 15:24
  • @buran thanks for those suggestions man! learnt a lot! – Ghost Ops Sep 28 '21 at 15:25
  • Oh my bad @GhostOps I glossed right over that comment because I saw somebody telling you not to use `eval()`. – ddejohn Sep 28 '21 at 15:25
  • @ddejohn yeah, just got roasted in the comments for being a noob in python :') – Ghost Ops Sep 28 '21 at 15:27
  • lol, the `eval()` roast is a rite of passage for Pythoners – ddejohn Sep 28 '21 at 15:29
  • Maybe it makes sense to show you guys the bigger picture and why I am not reading the file in as a json: https://stackoverflow.com/questions/69362508/read-and-manipulate-sagemaker-json-output – soulwreckedyouth Sep 28 '21 at 15:29
  • @ddejohn thanks for the explanation but how do I get rid of it then? – soulwreckedyouth Sep 28 '21 at 15:32
  • There's nothing to get rid of. What problem are you really trying to solve that requires removing the (nonexistent) single quotes? – Barmar Sep 28 '21 at 15:34
  • 2
    I looked at your other post and am confused. Do you have dictionaries (as your other post suggests) or do you have strings? If you have strings *why not use* `json` to convert the json strings to dictionaries? – ddejohn Sep 28 '21 at 15:34
  • 1
    Try: `batch = list(map(json.loads, batch))`. I'm hoping the missing `]}` in each string is a copying error. – Barmar Sep 28 '21 at 15:35
  • 2
    Or it could be a bug in how you're creating the list from the original input data. You should fix that. – Barmar Sep 28 '21 at 15:36

1 Answers1

2

Alright, so judging by your comments and your other post, it seems like what you have are strings, and what you want are dictionaries. I've copied your data from the other post because your data isn't correct in this post (you're missing ] in this post).

Solution

import json

batch = ['{"SageMakerOutput":[{"label":"LABEL_8","score":0.9152628183364868}],"inputs":"test"}',
         '{"SageMakerOutput":[{"label":"LABEL_8","score":0.9769203066825867}],"inputs":"Alles OK"}']

batch = [json.loads(b) for b in batch]

Output:

[{'SageMakerOutput': [{'label': 'LABEL_8', 'score': 0.9152628183364868}],
  'inputs': 'test'},
 {'SageMakerOutput': [{'label': 'LABEL_8', 'score': 0.9769203066825867}],
  'inputs': 'Alles OK'}]

Explanation

Those objects in batch are what are known as JSON objects. They're just strings with a very specific structure. They are analogous to Python's dict type with some very minor differences and can very easily be converted to Python dict objects using Python's built-in json module, which automatically translates those minor differences between JSON and Python (e.g., booleans in JSON strings are true and false, but in Python they need to be True and False).

Notes

Advice to OP and future readers: this post is a classic case of the XY problem. In the future, try to be more clear about your end goal. Your question in this post isn't actually answerable because it's impossible to do what you were asking.

ddejohn
  • 8,775
  • 3
  • 17
  • 30