【问题标题】:read the text file in azure storage blob line by line using python使用python逐行读取azure存储blob中的文本文件
【发布时间】:2019-09-21 22:27:39
【问题描述】:

我需要从 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)

【问题讨论】:

    标签: python-3.x azure-storage azure-blob-storage


    【解决方案1】:

    方法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)
    

    【讨论】:

    • 您好,如果答案有效,请帮忙标记为答案。谢谢。
    • 是的,它对我有用。 blob 中有大约 10000 个文本文件。我们可以获取分配给我检索的每一行的文件名吗?
    • 你的意思是有一些像a.txt,b.txt这样的文件,从a.txt或者b.txt获取每一行的时候,加上文件名(a.txt or b .txt) 到每一行?
    • 如果是我提到的情况,你可以直接在print()中添加blob名称,比如print(blob_name+":"+a1)
    猜你喜欢
    • 2021-08-26
    • 2021-04-01
    • 1970-01-01
    • 2021-05-09
    • 2012-06-16
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多