从 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的不同容器的新文件中,如下所示(如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文件。我只设法显示它们。我怎样才能做到这一点?

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

#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....   

最新更新