base64 Exporting converted strings as text files does not know how to match values when reimported

Asked 2 years ago, Updated 2 years ago, 444 views

If you write to a text file in utf-8 format with the following code and import it again, the base64 data will be kept as string type, and the data of the original variable will not match.

base64.b64encode(string variable.encode())

The following situations

Imported text state = b'abc' (*string type)
base64.b64encode(string variable.encode()) = b'abc' (*bytes type)

I want to check if the imported text matches the original word in the if statement, but I can't decode it because the imported text keeps the base64 text in a string state.

Error:

'str' object has no attribute'decode'

If you try to match the state immediately after importing the encoding variable, you cannot convert string from byte type (of course)
 

How can I match it?
Also, I don't know how to interpret the following error. Could you please let me know?

Error code 'utf-8' codec can't decode byte 0xcd in position 3: invalid continuation byte

python python3 base64

2022-10-22 09:14

3 Answers

After much searching, we have solved the problem by the following methods.
Also, write down the code for reproduction up to the unresolved parts.

#text.py
# 書き Because the directory of the export file is set directly below the C drive (provisional),
#   Prerequisites for Executing Anaconda Prompts with Administrator Privileges When Running Py Files
import base64

# Text to export
inputTxt = 'aiueotext'
print(inputTxt, type(inputTxt))
# =>aiueotext<class 'str'>

exportText=base64.b64encode(inputTxt.encode())#...■1

print(exportText, type(exportText))
#=>b'YWl1ZW90ZXh0'<class'bytes'>

# beginning of writing
txtfl=open('C:\\textfile_utf8.txt', 'w')
txtfl.write(str(exportText))
txtfl.close()

# loading
readfl=open('C:\\textfile_utf8.txt', 'r')
txt = readfl.read()
readfl.close()

print(txt, type(txt))
#=>b'YWl1ZW90ZXh0'<class'str'>
# Because the type is different between bytes and str, the value could not be determined to be the same.


# This is the reproduction code.


# solution

# I wanted to match txt with inputTxt.
# I managed to encode the data on the read file side with the following description.
#base64.b64decode(txt[2:-1].encode()) .decode('utf-8')

txt = base64.b64decode(txt[2:-1].encode()) .decode('utf-8')#...■2

print(txt, type(txt))
# =>aiueotext<class 'str'>

Note:
I was not satisfied with the process of excluding some variables when decoding, so I looked it up and found out that
I was able to do the following:

#Corrected
#...■1
exportText=base64.b64encode(inputTxt.encode()) .decode('utf-8')

#...■2
txt = base64.b64decode(txt.encode()) .decode('utf-8')


2022-10-22 09:14

b'YWl1ZW90ZXh0' data of length 12

txtfl=open('C:\\textfile_utf8.txt', 'w')
txtfl.write(str(exportText))
txtfl.close()

Did you have any doubts about increasing to 15 bytes when you started writing in ?

Didn't you have any doubts about the b' part in the file?

Did you really think that was part of the base64 encoded data?

Did you see the contents of the file in the first place?

To export data of type bytes, open the file in text mode and write it with something unnecessary (b') to represent the literal in Python.

I don't seem to understand the bytes and str types yet, so I think it would be good to touch them, but I'll leave it behind and write down the code that would have been better if it had been like this.

import base64

# text to be exported
inputTxt = 'aiueotext'
print(inputTxt, type(inputTxt))
# =>aiueotext<class 'str'>

# Data of type bytes obtained by decoding in UTF-8 is encoded in base 64.
exportText=base64.b64encode(inputTxt.encode())

print(exportText, type(exportText))
#=>b'YWl1ZW90ZXh0'<class'bytes'>

# Export in binary mode
binfl=open('textfile_ascii.txt', 'wb')
binfl.write(exportText)
binfl.close()

# Read in binary mode
readfl=open('textfile_ascii.txt', 'rb')
txt_as_bytes=readfl.read()
readfl.close()

print(txt_as_bytes, type(txt_as_bytes))
# =>b'YWl1ZW90ZXh0'<class'bytes'>

# Data of type bytes decoded in base64 is decoded in UTF-8 to obtain type str.
txt=base64.b64decode(txt_as_bytes).decode()

print(txt, type(txt))
# =>aiueotext<class'str'>

By the way

https://docs.python.org/ja/3/library/base64.html#base64.b64decode

Decode the base64 encoded bytes-like object or ASCII string s and return the decoded bytes.

So if you want to load it,

# The contents of the file are definitely within ascii's scope, so you can specify the encoding.
# Whether it's UTF-8 or ascii or Shift-JIS, the result doesn't change.
readfl=open('textfile_ascii.txt', 'r')
txt_as_str=readfl.read()
readfl.close()

print(txt_as_str, type(txt_as_str))
# =>YWl1ZW90ZXh0<class'str'>

# Data of type bytes decoded in base64 is decoded in UTF-8 to obtain type str.
txt=base64.b64decode(txt_as_bytes).decode()

print(txt, type(txt))
# =>aiueotext<class'str'>

But it's good.


2022-10-22 09:14

As you pointed out in the comments, there were some difficulties in how to ask questions, but the person who asked me provided the code in response to the comments, so I personally had the opportunity to learn more about Python's Base64 processing.

Therefore, when I checked the Japanese string to find out how to add the questioner, it seemed that there was no problem with my environment (MacOS 12.6, Terminal (utf-8), Python 3.10.8).I will transfer the code and output for your reference.

import base64

input_str=('Encode the string Base64'
             US>'Write to text file\n'
             'Load text file'
             ' Base64 decode to get string\n')
print(f'input_str:\n{input_str}')

write_str=base64.b64encode(input_str.encode()) .decode()

with open('tmp_base64.txt', 'w') asf:
    f.write(write_str)

with open('tmp_base64.txt', 'r') asf:
    read_str=f.read()

return_str=base64.b64decode(read_str).decode()
print(f'return_str:\n{return_str}')

print(f'input_str==return_str:\n{input_str==return_str}')
 input_str:
Write a string to a text file with Base64 encoding
Read text file and decode Base64 to get string

return_str:
Write a string to a text file with Base64 encoding
Read text file and decode Base64 to get string

input_str==return_str:
True


2022-10-22 09:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.