Please help because this flipping program is my ongoing nightmare!
I have several files that include some base64 encoded strings.Part of one file for examples reads as follows:
charset=utf-8;base64,I2JhY2tydW5uZXJfUV81c3R7aGVpZ2h0OjkzcHg7fWJhY2tydW5uZXJfUV81c3R7ZGlzcGxheTpibG9jayFpbXBvcnRhbnQ7fQ=="
They are always in the format "ANYTHINGbase64,STRING"It is html but I am treating it as one large string and using BeautifulSoup elsewhere. I am using a regex expression 'base' to extract the base64 string, then using base64 module to decode this as per my defined function "debase".
This seems to work ok up to a point: the output of b64encode for some reason adds unnecessary stuff:
b'#backrunner_Q_5st{height:93px;}backrunner_Q_5st{display:block!important;}' with the string the stuff in the middle.
I'm guessing this means in bytes; so I have tried getting my function to encode this as utf8 but basically I am out of my depth.
The end result that I want is for all "base64,STRING" in my html to be decoded and replaced with DECODEDSTRING.
Please help!
import os, sys, bs4, re, base64, codecsfrom bs4 import BeautifulSoupdef debase(instr): outstring = base64.b64decode(instr) outstring = codecs.utf_8_encode(str(outstring)) outstring.split("'")[1] return outstringbase = re.compile('base64,(.*?)"')for eachArg in sys.argv[1:]: a=open(eachArg,'r',encoding='utf8') presoup = a.read() b = re.findall(base, presoup) for value in b: re.sub('base64,.*?"', debase(value)) print(debase(value)) soup=BeautifulSoup(presoup, 'lxml') bname= str(eachArg).split('.')[0] a.close() [s.extract() for s in soup('script')] os.remove(eachArg) b=open(bname +'.html','w',encoding='utf8') b.write(soup.prettify()) b.close()
my issue is when I try to encode an image to base-64, it generate a long string (up to 200,000 lines) and it doesn't work in the decoding, it generate a nil image! even I try to resize the image to a smaller size but still it doesn't work!
Here is my image encoding code
let image = imageView.imagelet imageData:NSData = UIImagePNGRepresentation(image!)! as NSDatalet strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
The decoding code:
let processImage = user.value(forKey: "processImage") as! String // image from json if let dataDecoded:NSData = NSData(base64Encoded: processImage, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) { let image: UIImage = UIImage(data:dataDecoded as Data,scale:1.0)! print(image.size) self.myImage.image = image }
So I have an image cropper and its allowing the user to set the size of the image. The image returned after it is cropped is in this form:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtwAAALcCAYAAADUqWdpAAAgAElEQVR4Xux9...
However, when I try to decode this, i'm told that it is not in the correct format... What is going on here?
EDIT: I'm using var base64 = require('base-64'); base64.decode(string);
EDIT: Full return value: https://pastebin.com/vsHBq3pR
I am using Gmail API to read emails from Gmail account.
In the body I am replacing some chars which are needed as I read in the forums:
String codedBody = body.Replace("-", "+"); codedBody = codedBody.Replace("_", "/");
Problem is that when I try to convert it
byte[] data = Convert.FromBase64String(codedBody);
there is an exception which is firing with some emails:
System.FormatException: 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.'
The string which is coming from the request is:
"0J7QsdGP0LLQsDogSGVhbHRoY2FyZSBTZXJ2aWNlIFJlcHJlc2VudGF0aXZlIHdpdGggRHV0Y2gsIEdlcm1hbiANCiDQktCw0LbQvdC-ISDQnNC-0LvRjywg0L3QtSDQvtGC0LPQvtCy0LDRgNGP0LnRgtC1INC90LAg0YLQvtC30LggZW1haWwuICANCiAg0KLQvtC30LggZW1haWwg0LUg0LjQt9C_0YDQsNGC0LXQvSDQv9GA0LXQtyBqb2JzLmJnINC-0YIg0LjQvNC10YLQviDQvdCwINCa0YDQuNGB0YLQuNCw0L0g0JrRitC90LXQsiAg0JfQsCDQtNCwINGB0LUg0YHQstGK0YDQttC10YLQtSDRgSDQutCw0L3QtNC40LTQsNGC0LAg0YfRgNC10LcgZW1haWwg0LjQt9C_0L7Qu9C30LLQsNC50YLQtToga3Jpc3RpYW5fdG9uaUBhYnYuYmcgIA0KICDQodGK0L7QsdGJ0LXQvdC40LUg0L7RgiDQutCw0L3QtNC40LTQsNGC0LA6ICANCiAg0LHQu9Cw0LHQu9Cw0LHQu9Cw0LHQu9CwDQoNCg0KDQoNCg0KICA=PEhUTUw-PEJPRFk-DQrQntCx0Y_QstCwOiBIZWFsdGhjYXJlIFNlcnZpY2UgUmVwcmVzZW50YXRpdmUgd2l0aCBEdXRjaCwgR2VybWFuPGRpdj48YnI-PGRpdj7QktCw0LbQvdC-ISDQnNC-0LvRjywg0L3QtSDQvtGC0LPQvtCy0LDRgNGP0LnRgtC1INC90LAg0YLQvtC30LggZW1haWwuPC9kaXY-PGRpdj48YnI-PC9kaXY-PGRpdj7QotC-0LfQuCBlbWFpbCDQtSDQuNC30L_RgNCw0YLQtdC9INC_0YDQtdC3IGpvYnMuYmcg0L7RgiDQuNC80LXRgtC-INC90LAg0JrRgNC40YHRgtC40LDQvSDQmtGK0L3QtdCyPC9kaXY-PGRpdj7Ql9CwINC00LAg0YHQtSDRgdCy0YrRgNC20LXRgtC1INGBINC60LDQvdC00LjQtNCw0YLQsCDRh9GA0LXQtyBlbWFpbCDQuNC30L_QvtC70LfQstCw0LnRgtC1OiBrcmlzdGlhbl90b25pQGFidi5iZzwvZGl2PjxkaXY-PGJyPjwvZGl2PjxkaXY-0KHRitC-0LHRidC10L3QuNC1INC-0YIg0LrQsNC90LTQuNC00LDRgtCwOjwvZGl2PjxkaXY-PGJyPjwvZGl2PjxkaXY-0LHQu9Cw0LHQu9Cw0LHQu9Cw0LHQu9CwPGJyPjxicj48YnI-PGJyPjxicj48YnI-PC9kaXY-PC9kaXY-PC9CT0RZPjwvSFRNTD4NCg=="
What is causing this problem?
Encoding is new to me in Python, and I am trying to understand it. Apologies if this has been asked and answered already.
I am trying to encode a Python list and decode it.When I am trying to encode a list directly, I am hitting an error like below.
>>> my_list = [1, 2, 3]>>> encoded_list = base64.b64encode(my_list)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 54, in b64encode encoded = binascii.b2a_base64(s)[:-1]TypeError: b2a_base64() argument 1 must be string or buffer, not list
To fix it, I converted the list object to a string and passed it to the encode function and I was able to successfully encode it.
>>> encoded_list = base64.b64encode(str(my_list))>>> encoded_list'WzEsIDIsIDNd'
When I try to decode it, I get a decoded string like below.
>>> decoded_list = base64.b64decode(encoded_list)>>> decoded_list'[1, 2, 3]'>>> type(decoded_list)<type 'str'>
But my original intention was to encode and decode a list and not convert the list to a string and then string to list.
Pretty sure this is not the right way to encode objects like dict or a list. If that's the case, Can someone please enlighten me on how to encode/decode non string objects in Python?
Thanks very much.
Domácí - Mapa stránek - Soukromí - Links - Copyright © 2018 Cortex IT Ltd : Kontakt : admin @ cortexit.co.uk
Please note that by viewing our site you agree to our use of cookies (see Soukromí for details). You will only see this message once.