简体   繁体   English

Python:类型错误:字符串索引必须是 python 中的整数

[英]Python: TypeError: string indices must be integers in python

Hi I am trying to Upload a file into the blob storage using python, but while doing so i am getting the following error您好我正在尝试使用 python 将文件上传到 blob 存储中,但是这样做时我收到以下错误

TypeError: string indices must be integers TypeError:字符串索引必须是整数

Currently My code looks like this目前我的代码看起来像这样

import os
import yaml
from azure.storage.blob import ContainerClient

def load_config():
    dir_root = os.path.dirname(os.path.abspath(__file__))
    with open(dir_root + "/config.yaml","r") as yamlfile:
        return yaml.load(yamlfile, Loader=yaml.FullLoader)

def get_files(dir):
    with os.scandir(dir) as entries:
            for entry in entries:
                if entry.is_file() and not entry.name.startswith('.'):
                    yield entry

config =load_config()
csvs =get_files(config["source_folder"]+"/csvs")
print(*csvs)

I have tried to reproduce in my environment I uploaded a file into the blob storage using python successfully我试图在我的环境中重现我成功使用 python 将文件上传到 blob 存储

Please use this below code to upload a file into the blob storage请使用以下代码将文件上传到 blob 存储

    from azure.storage.blob import BlobServiceClient
    import json
    twilioMetadata ={1:'dhoni',2:'rohit',3:'virat',4:'raina',5:'yuvaraj'}
    Body = json.dumps(twilioMetadata)
    print(Body)
    blob_service_client  = BlobServiceClient.from_connection_string("<connection string>")
    blob_client = blob_service_client.get_blob_client("container1",blob="upload2.json")
    blob_client.upload_blob(Body)

For connection string, in storage account -> shared access signature -> "Enable the container" in allowed resource types -> click Generate sas and connection string -> copy connection string对于连接字符串,在存储帐户 -> 共享访问签名 -> 允许的资源类型中的“启用容器” -> 单击生成 sas 和连接字符串 -> 复制连接字符串

Output : Output

在此处输入图像描述

Finally, my files are uploaded in an azure blob storage successfully as below.最后,我的文件成功上传到 azure blob 存储中,如下所示。

在此处输入图像描述

Note: Based on python code drafted above response and output I have not used your code.注意:基于上述响应和 output 起草的 python 代码,我没有使用您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM