[英]Python strip only single specific characters from text/json
I'm currently trying to scrape data from a website and want to automatically save them.我目前正在尝试从网站上抓取数据并希望自动保存它们。 I want to format the data before so I can use them as cvs or similar.
我想先格式化数据,以便可以将它们用作 cvs 或类似的。 The json is:
json 是:
{"counts":{"default":"27","quick_mode1":"48","quick_mode2":"13","custom":"281","quick_mode3":"0","total":369}}
My code is:我的代码是:
x = '{"counts":{"default":"27","quick_mode1":"48","quick_mode2":"13","custom":"281","quick_mode3":"0","total":369}}'
y = json.loads(x)
print(y["total"])
But due to the {"counts":
on the beginning and the corresponding }
on the end I can't just use it as a normal json file because the formatting breaks and it just puts an error message out ( json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
), when I remove the characters manually it then works again.但是由于
{"counts":
在开头和相应的}
结尾,我不能将它用作普通的 json 文件,因为格式中断并且它只会发出错误消息( json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
),当我手动删除字符时,它会再次起作用。
How can I get rid of only those 2 parts?我怎样才能摆脱这两个部分?
I think that json
library should go along with this, but if it really is an issue you can't get rid of, you could remove all occurrences of {
and }
characters in the string you are are receiving with .replace()
function.我认为
json
库应该与此一起使用 go ,但如果这确实是您无法摆脱的问题,您可以使用.replace()
ZC1C1AB5068E68A47 删除您收到的字符串中所有出现的{
和}
字符。
This requires chaining for every character you want to remove and is rather not an optimal solution, but as far as the strings you are processing are not long and/or you are not concerned about efficiency this should do just fine.这需要为您要删除的每个字符进行链接,这不是最佳解决方案,但只要您正在处理的字符串不长和/或您不关心效率,这应该就可以了。
Example:例子:
my_var.replace('{', '').replace('}', '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.