使用python逐行读取azure存储blob中的文本文件 [英] read the text file in azure storage blob line by line using python

查看:43
本文介绍了使用python逐行读取azure存储blob中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从BLOB存储中逐行读取文本文件,并执行一些操作并获取数据帧的特定行。我试过用各种方法逐行阅读文件。当数据在本地存储中时,有没有办法从BLOB LINE-LINE中读取文本文件,并像readLines()一样执行操作和输出特定的行?

candidate_resume = 'candidateresumetext'
block_blob_service = BlockBlobService(account_name='nam', account_key='key')
generator2 = block_blob_service.list_blobs(candidate_resume)
#for blob in generator2:
   #print(blob.name)
for blob in generator2:
    blob2 = block_blob_service.get_blob_to_text(candidate_resume,blob.name)
    #print(blob2)

    #blob_url=block_blob_service.make_blob_url(candidate_resume, blob.name)
    #print(blob_url)

    #blob3 = block_blob_service.get_blob_to_stream(candidate_resume,blob.name,range)
    blob3 = blob2.split('.')
    with open(blob.name,encoding = 'utf-8') as file:
        lines = file.readlines()
        for line in blob3:      
            if any(p in years_list for p in line ):
                if any(p in months_list for p in line):    
                    print(line)

推荐答案

方法get_blob_to_text是正确的,您可以按照下面的示例代码操作(如果不能满足您的需要,可以做一些修改)。您无法使用with open() as file,因为那里没有实际文件。

#read the content of the blob(assume it's a .txt file)
str1 = block_blob_service.get_blob_to_text(container_name,blob_name)

#split the string str1 with newline.
arr1 = str1.content.splitlines()

#read the one line each time.
for a1 in arr1:
    print(a1)

这篇关于使用python逐行读取azure存储blob中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆