簡體   English   中英

Python:類型錯誤:字符串索引必須是 python 中的整數

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

您好我正在嘗試使用 python 將文件上傳到 blob 存儲中,但是這樣做時我收到以下錯誤

TypeError:字符串索引必須是整數

目前我的代碼看起來像這樣

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)

我試圖在我的環境中重現我成功使用 python 將文件上傳到 blob 存儲

請使用以下代碼將文件上傳到 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)

對於連接字符串,在存儲帳戶 -> 共享訪問簽名 -> 允許的資源類型中的“啟用容器” -> 單擊生成 sas 和連接字符串 -> 復制連接字符串

Output

在此處輸入圖像描述

最后,我的文件成功上傳到 azure blob 存儲中,如下所示。

在此處輸入圖像描述

注意:基於上述響應和 output 起草的 python 代碼,我沒有使用您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM