How to pass a python variables to shell script in azure databricks notebookbles.?

百般思念 提交于 2021-01-23 04:57:59

问题


How to pass a python variables from %python cmd to shell script %sh,in azure databricks notebook..?

notebook example


回答1:


Per my experience, there are two workaround ways to pass a Python variable to Bash script for your current scenario.

Here is my sample codes using Python3 in notebook.

  1. To pass little data via environment variable in the same shell session of Azure Databricks Notebook, as below.

    %python
    import os
    l = ['A', 'B', 'C', 'D']
    os.environ['LIST'] = ' '.join(l)
    print(os.getenv('LIST'))
    
    %%bash
    for i in $LIST
    do
      echo $i
    done
    

It works as the figure below.

  1. To pass large data via file in the current path of Azure Databricks Notebook, as below.

    %python
    with open('varL.txt', 'w') as f:
      for elem in l:
        f.write(elem+'\n')
      f.close()  
    
    %%bash
    pwd
    ls -lh varL.txt
    echo '=======show content=========='
    cat varL.txt
    echo '=====result of script========'
    for i in $(cat varL.txt)
    do
      echo $i
    done
    

It works as the figure below.



来源:https://stackoverflow.com/questions/54662605/how-to-pass-a-python-variables-to-shell-script-in-azure-databricks-notebookbles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!