python 如何将数据帧的结果插入到read_block函数中

python how to insert result of data frame into read_block function

提问人:Java 提问时间:11/9/2023 最后编辑:Java 更新时间:11/20/2023 访问量:369

问:

我正在尝试使用从 Azure Blob 存储生成的数据帧的结果,并将其应用于下一步(它以某种方式提取数据)。

我已经测试了两端(从Azure Blob存储生成数据并使用正则表达式提取数据(如果我单独测试,它可以工作)),但我现在的挑战是将两段代码放在一起。

下面是第一部分(从 Azure Blob 存储获取数据帧):

import re 
from io import StringIO
import pandas as pd
from azure.storage.blob import BlobClient


blob = BlobClient(account_url="https://test.blob.core.windows.net",
              container_name="xxxx",
              blob_name="Text.csv",
              credential="xxxx")

data = blob.download_blob()
df = pd.read_csv(data)

这是第二部分(仅从 csv 文件中提取部分部分):

def read_block(names, igidx=True):
    with open("Test.csv") as f:   ###<<<This is where I would like to modify<<<###              
        pat = r"(\w+),+$\n[^,]+.+?\n,+\n(.+?)(?=\n,{2,})"
        return pd.concat([
            pd.read_csv(StringIO(m.group(2)), skipinitialspace=True)
                .iloc[:, 1:].dropna(how="all") for m in re.finditer(
                    pat, f.read(), flags=re.M|re.S) if m.group(1) in names # optional
        ], keys=names, ignore_index=igidx)

df2 = read_block(names=["Admissions", "Readmissions"],igidx=False).droplevel(1).reset_index(names="Admission")   

因此,我试图做的是使用第一个代码中的 df 并应用于第二个代码的输入部分,其中它说“打开(”Test.csv“)为 f。

如何修改此代码的第二部分以从第一部分获取数据结果?

enter image description here

或者,如果这不起作用,有没有办法使用从 Azure 生成的文件路径 ID(数据),如下所示?

<azure.storage.blob._download.StorageStreamDownloader object at 0x00000xxxxxxx>

更新:

我修改了代码如下,现在我收到 concat 错误:

我不确定这是由于没有任何循环功能(因为我修改为删除“with open(”Test.csv“)作为f:)。

...

data = blob.download_blob()
df = pd.read_csv(data)
df1 = df.to_csv(index=False, header=False)

def read_block(names, igidx=True):    
    pat = r"(\w+),+$\n[^,]+.+?\n,+\n(.+?)(?=\n,{2,})"
    return pd.concat([
        pd.read_csv(StringIO(m.group(2)), skipinitialspace=True)
            .iloc[:, 1:].dropna(how="all") for m in re.finditer(
                pat, df1, flags=re.M|re.S) if m.group(1) in names 
    ], keys=names, ignore_index=igidx)

df2 = read_block(names=["Admissions", "Readmissions"], igidx=False).droplevel(1).reset_index(names="Admission")   
print(df2) 

enter image description here

新形象:

enter image description here

这是错误消息:enter image description here

这是最新代码(2023-11/13):

import re 
from io import StringIO
import pandas as pd
from azure.storage.blob import BlobClient
blob = 
BlobClient(account_url="https://xxxx.blob.core.windows.net",
              container_name="xxxx",
              blob_name="SampleSafe.csv",               
              credential="xxxx")

data = blob.download_blob(); 
df = pd.read_csv(data); 
df1 = df.to_csv(index=False)

def read_block(names, igidx=True):    
    pat = r"(\w+),+$\n[^,]+.+?\n,+\n(.+?)(?=\n,{2,})"
    return pd.concat([
        pd.read_csv(StringIO(m.group(2)), skipinitialspace=True)
            .iloc[:, 1:].dropna(how="all") for m in re.finditer(
                pat, data.readall(), flags=re.M|re.S)
               if m.group(1) in names], keys=names, ignore_index=igidx)

df2 = read_block(names=["Admissions", "Readmissions"], igidx=False).droplevel(1).reset_index(names="block")
print(df2) 

这是详细的错误消息(2023-11 年 11 月 13 日更新):

enter image description here

这是 df1 (11/18/2023):

Division  FacilityName Census Admiss Readmiss  Discharges
          Test1         57    0      0         0
          Test3         2     0      0         1
          Test5         135   0      0         0
          Test6         9     0      0         0
          Test4         3     0      0         1
          Test2         76    0      0         0
          Blindsection  55    1      0         2
                
                
Admissions                  
Not Started: 12 Sent: 3 Completed: 3            
                
Division Community    ResiName  Date      DocStatus  LastUpdate
         TestStation  Jane Doe  9/12/2023 Sent       9/12/2023
         TestStation2 John Doe  9/12/2023 NotStarted    
         Alibaba      SuperMan  9/12/2023 NotStarted    
         Iceland      SuperWoma 9/12/2023 NotStarted    
                
                
Readmissions                    
Not Started: 1  Sent: 0 Completed: 1            
                
Division Community  ResidentName Date      DocStatus    Last Update
         StationK   PrettyWoman  9/12/2023 Not Started  
         MyGoodness UglyMan      7/21/2023 Completed    7/26/2023
                
                
Discharge                   
                
Division Community       ResidentName   Date        
         StationKingdom1 PrettyWoman2   8/22/2023       
         MyGoodness1     UglyMan1       4/8/2023        
         Landmark2       NiceGuys       9/12/2023       
         IcelandKingdom2 Mr.Heroshi2    7/14/2023       
         MoreKingdom2    KingKong       8/31/2023       

enter image description here这是使用以前的代码(11 月 18 日更新)的错误消息:enter image description here

这是错误消息(11 月 19 日更新): enter image description here

python pandas 正则表达式 azure-blob-storage

评论

2赞 Quang Hoang 11/9/2023
我不确定我是否理解这个问题。与什么有什么关系? 将尝试从本地存储中读取文件。dfread_blockwith open('Test.csv') as f:Test.csv
1赞 Mark Ransom 11/9/2023
而不是只是使用,看看它会把你带到哪里。f.read()data
1赞 rr_goyal 11/9/2023
尝试使用 的输出,注意:可能有额外的特殊字符,但其余的应该可以正常工作。df.to_csv(index=False, header=False)\r
2赞 Quang Hoang 11/9/2023
没有块,只是替换而不是.withdf1f.read()
1赞 rr_goyal 11/9/2023
如前所述,@QuangHoang,将 f.read() 替换为 df1 并删除 with 语句。的输出类似于 。df.to_csv(...)f.read()

答:

1赞 Timeless 11/12/2023 #1

我修改了代码......现在我收到 concat 错误

IIUC,这是因为正则表达式无法匹配所需的块。制作缓冲区时需要删除。或者干脆读取所有下载的 blob 并创建一个字符串以避免将输入 csv 读取为 DataFrame:header=Falsedf1 = df.to_csv(index=False)

data = blob.download_blob(max_concurrency=1, encoding="UTF-8")

def read_block(names, igidx=True):    
    pat = r"(\w+),+$\n[^,]+.+?\n,+\n(.+?)(?=\n,{2,})"
    return pd.concat([
        pd.read_csv(StringIO(m.group(2)), skipinitialspace=True)
            .iloc[:, 1:].dropna(how="all") for m in re.finditer(
                pat, data.readall(), flags=re.M|re.S)
               if m.group(1) in names], keys=names, ignore_index=igidx)

评论

1赞 Timeless 11/12/2023
似乎是由 .你曾经定义哪一行?您是否尝试过像原始代码一样传递?re.finditerdatadf1re.finditer
1赞 Timeless 11/12/2023
我的意思是你试过使用吗?在上一段代码中,您使用了 (这显然会在传递给 .data = BytesIO(); blob.download_blob().readinto(data)data = blob.download_blob()TypeErrorre.finditer
1赞 Timeless 11/12/2023
感谢您的调试。我想我现在明白了这个问题(见更新的答案)。
1赞 Timeless 11/13/2023
我觉得你没有告诉我们所有的细节。如果是这样,我就放弃了。如果没有,您可以将其作为文本包含在旅游帖子中吗?print(data.read().decode("utf-8"))
1赞 Timeless 11/13/2023
不,我更新了我的答案。你能检查一下吗?