当尝试将文件夹中的CSVS上载到blob时,它会在第一个CSV:中抛出
异常:无效的base64编码字符串:数据字符数(85(不能超过4 的倍数1
谢谢!
import os
import io
import glob from base64
import b64decode, b64encode
from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient
path = "D:/Git projects/csvs"
extension = 'csv'
os.chdir(path) #gets files that are from TODAY only
result = glob.glob("*"+ str(today) + ".{}".format(extension))
try:
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
for csv_file in result: # Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container=, blob=blob_name) # Upload the created file
with open(csv_file, "rb") as data:
#data = data.read().decode("utf-8")
#data = data.encode("utf-8")
blob_client.upload_blob(data, overwrite = True)
except Exception as ex:
print('Exception:')
print(ex)`
OUTPUT: Exception: Invalid base64-encoded string: number of data characters (85) cannot be 1 more than a multiple of 4
您可以检查要上传的csv数据的编码:
import chardet
with open(csv_file,"rb") as data
chardet.detect(data)
chardet的输出将类似于:
{'encoding': 'EUC-JP', 'confidence': 0.99}
检测编码的库是:https://github.com/chardet/chardet。然后你可以转换编码并尝试再次上传