Home > Back-end >  How to convert different strings in python list to array in bash script
How to convert different strings in python list to array in bash script

Time:11-11

I'm having a python program in printlist.pyfile. I want to take the result of the printlist.py to array in bash script.

Here is python list output:

['Google USA (US Dollars, million)','Google UK (sf.nk)','Google India(rk,lp)']

I executed this command to assign the list to empty array arr:

mapfile -t arr < <(python3 /path/printlist.py | sed 's/[][]//g;s/, /\n/g')

When I print each string in the array I'm not getting a complete string.

for i in "${arr[@]}"
do
   echo "$i"
done

Eg: Google USA (US Dollars instead of Google USA (US Dollars, million)

How to convert the python list to an array so that it can print all variables perfectly. Thank You.

CodePudding user response:

In your python script, print your list like this, with null delimiters:

for i in mylist:
    print(i   "\000", end = "")

Then in bash, copy the output to an array like this:

mapfile -td '' myarr < <(python3 /path/printlist.py)

CodePudding user response:

First of all, please try to print the newline separated list as KamilCuk comments:

#!/usr/bin/python

l = ['Google USA (US Dollars, million)','Google UK (sf.nk)','Google India(rk,lp)']
for i in l:
    print(i)

Then feed the output to mapfile.
If you still have some specific reason that you need to separate it in bash, please try:

#!/bin/bash

line="['Google USA (US Dollars, million)','Google UK (sf.nk)','Google India(rk,lp)']"
# equivalent to line=$(python3 /path/printlist.py)

line=${line#[}                  # remove leading left square bracket
line=${line%]}                  # remove trailing right square bracket

pat="'([^']*)(. )"
while :; do
    if [[ $line =~ $pat ]]; then
        if [[ ${BASH_REMATCH[1]} != "," ]]; then
            arr =("${BASH_REMATCH[1]}")
        fi
        line=${BASH_REMATCH[2]} # update "line" to the remaining substring
    else
        break                   # no more matches
    fi
done

# see the result
for i in "${arr[@]}"; do
   echo "$i"
done

Output:

Google USA (US Dollars, million)
Google UK (sf.nk)
Google India(rk,lp)
  • The pattern ([^']*)(. ) matches 0 or more string of non-singlequote characters (captured as group1) followed by any characters (captured as group2).
  • Then the group1 captures substring surrounded by the single quotes or a comma separating the list.
  • the variable line is assigned to the remaining substring and the loop repeats till the end of the string.

I need to say the bash script above is not complete because it does not consider the case the string contains the single quote.

CodePudding user response:

This processes the python output to make it valid CSV.
Requires bash v5.1 for the loadable csv command.

# line=$(python printlist.py)
line="['Google USA (US Dollars, million)','Google UK (sf.nk)','Google India(rk,lp)']"

csv_string=${line#\[}                   # remove leading bracket
csv_string=${csv_string%\]}             # remove trailing bracket
csv_string=${csv_string//\"/\"\"}       # double any double quotes
csv_string=${csv_string//\'/\"}         # convert single to double quotes

BASH_LOADABLES_PATH=${BASH/bin/lib}
enable -f csv csv

csv -a elements "$csv_string"
declare -p elements

outputs

declare -a elements=([0]="Google USA (US Dollars, million)" [1]="Google UK (sf.nk)" [2]="Google India(rk,lp)")
  • Related