【问题标题】:How can we run a standard Python script in Azure and save files to a Data Lake?我们如何在 Azure 中运行标准 Python 脚本并将文件保存到数据湖?
【发布时间】:2023-01-31 07:58:48
【问题描述】:

我有一个 Python 脚本,可以在我的笔记本电脑上完美运行。我正在尝试将它移动到 Azure,并在那里运行它。理想情况下,我想做一些基本的屏幕抓取、一些基本的转换,然后将数据文件保存在数据湖或存储资源管理器中(数据湖可能更好)。因此,我设置了“创建自动化帐户”和“运行方式帐户”。现在,我尝试在 Azure“Runbook”中运行代码(点击“开始”按钮),但收到此错误消息。

Failed
Traceback (most recent call last):  File "C:\Temp\3fgngmon.o45\7e326422-ff39-4a2c-93f9-4afafd46205c", line 2, in <module>    from bs4 import BeautifulSoupModuleNotFoundError: No module named 'bs4'

这是我的示例代码。

import requests
from bs4 import BeautifulSoup
from urllib.parse import unquote
import csv
import io

all_links = [
    "/vsoch/hospital-chargemaster/tree/0.0.2/data/ochsner-clinic-foundation",
    "/vsoch/hospital-chargemaster/tree/0.0.2/data/ohio-state-university-hospital",
    "/vsoch/hospital-chargemaster/tree/0.0.2/data/orlando-health",
    "/vsoch/hospital-chargemaster/tree/0.0.2/data/st.-joseph%E2%80%99s-hospital-(tampa)",
]

for item in all_links:
    item = item.replace('tree/', '')
    
    try:
        file_name = unquote(item.split('/')[-1])
        DOWNLOAD_URL = f'https://raw.githubusercontent.com{item}/data-latest.tsv'
        r_tsv = requests.get(DOWNLOAD_URL)
        
        if r_tsv.status_code == 404:
            print(f"Not found - {DOWNLOAD_URL}")
        else:
            print(f"Downloaded - {DOWNLOAD_URL}")
            data = list(csv.reader(io.StringIO(r_tsv.text), delimiter='\t'))
            DOWNLOAD_PATH = fr'C:\Users\ryans\Desktop\hospital_data\{file_name}.csv'
            
            with open(DOWNLOAD_PATH, 'w', newline='') as f_output:
                csv_output = csv.writer(f_output)
                csv_output.writerows(data)
    except Exception as e: 
        print(e)

不知何故,我想我需要进行 pip 安装。不知道该怎么做。此外,我需要将保存路径更改为 Data Lake(或 Storage Explorer)。也不知道该怎么做。我怎样才能启动并运行它?

【问题讨论】:

    标签: azure azure-devops azure-functions azure-web-app-service


    【解决方案1】:

    你需要导入美丽的汤打包到您的自动化帐户中。

    在您的自动化帐户中,选择Python包在下面共享资源.点击+ 添加 Python 包.

    【讨论】:

    • 工作完美!感谢您分享这一见解!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2022-08-21
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多