Python的base64解码和编码后无法正常工作。

huangapple go评论134阅读模式
英文:

python base64 decode and encode back not working

问题

The text you provided appears to be HTML-encoded JSON data. The characters like " are HTML entities for double quotes, which are used to encode JSON within an HTML context. To parse this JSON, you need to first decode these HTML entities back to their original characters. Here's the corrected JSON data:

{
  "mShadowFontMapHash":"9c99df30-87e2-2b53-df95-b7ef0000022d",
  "mTextParam":{
    "mAlignment":2,
    "mBackFillColor":4144959,
    "mBackFillOpacity":75,
    "mBackFillSize":0,
    "mBackFillVisible":false,
    "mDefaultRun":[],
    "mHeight":0,
    "mHindiDigits":false,
    "mIndic":false,
    "mIsMask":false,
    "mIsMaskInverted":false,
    "mIsVerticalText":false,
    "mLeading":0,
    "mLigatures":false,
    "mLineCapType":0,
    "mLineJoinType":0,
    "mMiterLimit":2.5,
    "mNumStrokes":1,
    "mRTL":false,
    "mShadowAngle":135,
    "mShadowBlur":0,
    "mShadowColor":0,
    "mShadowOffset":10,
    "mShadowOpacity":100,
    "mShadowSize":0,
    "mShadowVisible":false,
    "mStyleSheet":{
      "mAdditionalStrokeColor":[],
      "mAdditionalStrokeVisible":[],
      "mAdditionalStrokeWidth":[],
      "mBaselineOption":{"mParamValues":[[0,0]]},
      "mBaselineShift":{"mParamValues":[[0,0]]},
      "mCapsOption":{"mParamValues":[[0,0]]},
      "mFauxBold":{"mParamValues":[[0,false]]},
      "mFauxItalic":{"mParamValues":[[0,false]]},
      "mFillColor":{"mParamValues":[[0,16777215]]},
      "mFillOverStroke":{"mParamValues":[[0,true]]},
      "mFillVisible":{"mParamValues":[[0,true]]},
      "mFontName":{"mParamValues":[[0,"Assistant-Regular"],[1,"Assistant-Regular"],[2,"Assistant-Regular"],[3,"Assistant-Regular"],[4,"Assistant-Regular"],[5,"Assistant-Regular"],[6,"Assistant-Regular"],[7,"Assistant-Regular"],[8,"Assistant-Regular"],[9,"Assistant-Regular"],[10,"Assistant-Regular"],[11,"Assistant-Regular"],[12,"Assistant-Regular"],[13,"Assistant-Regular"],[14,"Assistant-Regular"],[15,"58724202-DD58-420B-A6F0-2FBBC812F7B5"]]},
      "mFontSize":{"mParamValues":[[0,100]]},
      "mKerning":{"mParamValues":[[0,0]]},
      "mStrokeColor":{"mParamValues":[[0,16777215]]},
      "mStrokeVisible":{"mParamValues":[[0,false]]},
      "mStrokeWidth":{"mParamValues":[[0,1]]},
      "mText":"Your Title Here",
      "mTracking":{"mParamValues":[[0,0]]},
      "mTsumi":{"mParamValues":[[0,0]]},
      "mUnderline":{"mParamValues":[[0,false]]}
    },
    "mTabWidth":400,
    "mVerticalAlignment":0,
    "mWidth":0
  },
  "mVersion":1
}

Now you should be able to work with this JSON data in your code.

英文:

This -
cg4AAAAAAAB7ACIAbQBTAGgAYQBkAG8AdwBGAG8AbgB0AE0AYQBwAEgAYQBzAGgAIgA6ACIAOQBjADkAOQBkAGYAMwAwAC0AOAA3AGUAMgAtADIAYgA1ADMALQBkAGYAOQA1AC0AYgA3AGUAZgAwADAAMAAwADAAMgAyAGQAIgAsACIAbQBUAGUAeAB0AFAAYQByAGEAbQAiADoAewAiAG0AQQBsAGkAZwBuAG0AZQBuAHQAIgA6ADIALAAiAG0AQgBhAGMAawBGAGkAbABsAEMAbwBsAG8AcgAiADoANAAxADQANAA5ADUAOQAsACIAbQBCAGEAYwBrAEYAaQBsAGwATwBwAGEAYwBpAHQAeQAiADoANwA1ACwAIgBtAEIAYQBjAGsARgBpAGwAbABTAGkAegBlACIAOgAwACwAIgBtAEIAYQBjAGsARgBpAGwAbABWAGkAcwBpAGIAbABlACIAOgBmAGEAbABzAGUALAAiAG0ARABlAGYAYQB1AGwAdABSAHUAbgAiADoAWwBdACwAIgBtAEgAZQBpAGcAaAB0ACIAOgAwACwAIgBtAEgAaQBuAGQAaQBEAGkAZwBpAHQAcwAiADoAZgBhAGwAcwBlACwAIgBtAEkAbgBkAGkAYwAiADoAZgBhAGwAcwBlACwAIgBtAEkAcwBNAGEAcwBrACIAOgBmAGEAbABzAGUALAAiAG0ASQBzAE0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IAbgBpAG4AZwAiADoAewAiAG0AUABhAHIAYQBtAFYAYQBsAHUAZQBzACIAOgBbAFsAMAAsADAAXQBdAH0ALAAiAG0AUwB0AHIAbwBrAGUAQwBvAGwAbwByACIAOgB7ACIAbQBQAGEAcgBhAG0AVgBhAGwAdQBlAHMAIgA6AFsAWwAwACwAMQA2ADcANwA3ADIAMQA1AF0AXQB9ACwAIgBtAFMAdAByAG8AawBlAFYAaQBzAGkAYgBsAGUAIgA6AHsAIgBtAFAAYQByAGEAbQBWAGEAbAB1AGUAcwAiADoAWwBbADAALABmAGEAbABzAGUAXQBdAH0ALAAiAG0AUwB0AHIAbwBrAGUAVwBpAGQAdABoACIAOgB7ACIAbQBQAGEAcgBhAG0AVgBhAGwAdQBlAHMAIgA6AFsAWwAwACwAMQBdAF0AfQAsACIAbQBUAGUAeAB0ACIAOgAiAFkAbwB1AHIAIABUAGkAdABsAGUAIABIAGUAcgBlACIALAAiAG0AVAByAGEAYwBrAGkAbgBnACIAOgB7ACIAbQBQAGEAcgBhAG0AVgBhAGwAdQBlAHMAIgA6AFsAWwAwACwAMABdAF0AfQAsACIAbQBUAHMAdQBtAGkAIgA6AHsAIgBtAFAAYQByAGEAbQBWAGEAbAB1AGUAcwAiADoAWwBbADAALAAwAF0AXQB9ACwAIgBtAFUAbgBkAGUAcgBsAGkAbgBlACIAOgB7ACIAbQBQAGEAcgBhAG0AVgBhAGwAdQBlAHMAIgA6AFsAWwAwACwAZgBhAGwAcwBlAF0AXQB9AH0ALAAiAG0AVABhAGIAVwBpAGQAdABoACIAOgA0ADAAMAAsACIAbQBWAGUAcgB0AGkAYwBhAGwAQQBsAGkAZwBuAG0AZQBuAHQAIgA6ADAALAAiAG0AVwBpAGQAdABoACIAOgAwAH0ALAAiAG0AVgBlAHIAcwBpAG8AbgAiADoAMQB9AA==

decoded in python - b = base64.b64decode(a).decode()
and the result is -
r{"mShadowFontMapHash":"9c99df30-87e2-2b53-df95-b7ef0000022d","mTextParam":{"mAlignment":2,"mBackFillColor":4144959,"mBackFillOpacity":75,"mBackFillSize":0,"mBackFillVisible":false,"mDefaultRun":[],"mHeight":0,"mHindiDigits":false,"mIndic":false,"mIsMask":false,"mIsMaskInverted":false,"mIsVerticalText":false,"mLeading":0,"mLigatures":false,"mLineCapType":0,"mLineJoinType":0,"mMiterLimit":2.5,"mNumStrokes":1,"mRTL":false,"mShadowAngle":135,"mShadowBlur":0,"mShadowColor":0,"mShadowOffset":10,"mShadowOpacity":100,"mShadowSize":0,"mShadowVisible":false,"mStyleSheet":{"mAdditionalStrokeColor":[],"mAdditionalStrokeVisible":[],"mAdditionalStrokeWidth":[],"mBaselineOption":{"mParamValues":[[0,0]]},"mBaselineShift":{"mParamValues":[[0,0]]},"mCapsOption":{"mParamValues":[[0,0]]},"mFauxBold":{"mParamValues":[[0,false]]},"mFauxItalic":{"mParamValues":[[0,false]]},"mFillColor":{"mParamValues":[[0,16777215]]},"mFillOverStroke":{"mParamValues":[[0,true]]},"mFillVisible":{"mParamValues":[[0,true]]},"mFontName":{"mParamValues":[[0,"Assistant-Regular"],[1,"Assistant-Regular"],[2,"Assistant-Regular"],[3,"Assistant-Regular"],[4,"Assistant-Regular"],[5,"Assistant-Regular"],[6,"Assistant-Regular"],[7,"Assistant-Regular"],[8,"Assistant-Regular"],[9,"Assistant-Regular"],[10,"Assistant-Regular"],[11,"Assistant-Regular"],[12,"Assistant-Regular"],[13,"Assistant-Regular"],[14,"Assistant-Regular"],[15,"58724202-DD58-420B-A6F0-2FBBC812F7B5"]]},"mFontSize":{"mParamValues":[[0,100]]},"mKerning":{"mParamValues":[[0,0]]},"mStrokeColor":{"mParamValues":[[0,16777215]]},"mStrokeVisible":{"mParamValues":[[0,false]]},"mStrokeWidth":{"mParamValues":[[0,1]]},"mText":"Your Title Here","mTracking":{"mParamValues":[[0,0]]},"mTsumi":{"mParamValues":[[0,0]]},"mUnderline":{"mParamValues":[[0,false]]}},"mTabWidth":400,"mVerticalAlignment":0,"mWidth":0},"mVersion":1}

I am not able to read this as json. there seems to be some hidden characters in this because when i slice it as string for first 10 characters - b[:10] result is just this r{.
and if i just edit it as string and encode back, its not working.

答案1

得分: 0

以下是翻译好的部分:

"这些数据一团糟。前导的 r 不应该存在,而且里面有大量的0字节,这在JSON中都是无效的。要解决这个问题,你可以过滤掉这些无效的字节,并移除前导错误的字节:"

import json
import base64

inp = '在这里放入你的数据'
data = json.loads(bytes([b for b in list(base64.b64decode(inp)) if b != 0])[2:])
英文:

That data is an absolute mess. The leading r must not be there and there are tons of 0-bytes in there which are all invalid in json. To solve this you can filter out those invalid bytes and remove the leading wrong ones:

import json
import base64
inp = 'your data here'
data = json.loads(bytes([b for b in list(base64.b64decode(inp)) if b != 0])[2:])

答案2

得分: -2

我试过去掉第一个字符,然后将其保存为 JSON,我认为它运行正常。

import json

with open('text.txt', 'r') as f:
    x = json.load(f)

print(x)
英文:

I tried removing the first character and then save it as json and I think it works fine.

import json
with open('text.txt', 'r') as f:
x = json.load(f)
print(x)

huangapple
  • 本文由 发表于 2023年5月7日 20:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定