首页 > 解决方案 > 从 Azure Blob 存储打开文件并将它们附加到新文件中

问题描述

我似乎无法解决这个问题。

我在 Azure 容器中有几个名为container1. 例如 :

s1_cat.json
s2_cat.json
s3_dog.json
s1_dog.json
s2_dog.json

每个 json 的内容示例如下所示s1_cat.json

{"abc" : "def", "ghi" : 0}
{"123" : "456", "789" : 1}

s2_cat.json

{"klm" : "nop", "qrs" : 2}
{"2203" : "1994", "000" : 3}

很难处理正确的 json 格式。无论如何,我想根据关键字将它们附加catdog一个新文件中,然后将它们附加到一个不同的容器中,temp如下所示(as cat.json):

{"abc" : "def", "ghi" : 0}
{"123" : "456", "789" : 1}
{"klm" : "nop", "qrs" : 2}
{"2203" : "1994", "000" : 3}

我当前的代码:

try:

    container_name = 'container1'
    filepath = 'temp'
    account_name = 'xxx'
    account_key = 'xxx'

    blobService = BlockBlobService(account_name=account_name, account_key=account_key)
    appendblobservice = AppendBlobService(account_name=account_name, account_key=account_key)

    data = blobService.list_blobs(container_name, prefix='temp')

       
    for blob in data:

        if 'cat' in blob.name :

            filename = "cat.json"

            blobService.get_blob_to_path(container_name, blob_name=blob.name, file_path=filepath)

            #I stuck from here.....
            #read the json file
            cat = blobService.get_blob_to_text(container_name, blob.name)
            cat = cat.content.split('\n')
            cat = list(filter(None, cat )) #remove empty element in the list
            #display result
            print(cat)
            #stuck here....                



    
except Exception as ex:
    print('Unable to connect!')
    print('Exception:')
    print(ex)

我的问题是我不知道如何将第一个文件附加到cat第二个cat文件中。我只设法显示它们。我怎样才能做到这一点?

标签: pythonjsonazureazure-blob-storage

解决方案


尝试定义一个字符串,然后在每个循环中将每个内容添加到字符串中。代码如下:

#other code

mystring=""

for blob in data:

        if 'cat' in blob.name :

            filename = "cat.json"

            blobService.get_blob_to_path(container_name, blob_name=blob.name, file_path=filepath)

            #I stuck from here.....
            #read the json file
            cat = blobService.get_blob_to_text(container_name, blob.name)
            cat = cat.content.split('\n')
            cat = list(filter(None, cat )) #remove empty element in the list
            #display result
            print(cat)

            #here, append each content to the string
            mystring += cat + "\n"

            #stuck here....   

推荐阅读