0

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.

4
  • 1
    Write a python script to convert your script output to a newline separated list. Then use mapfile. But anyway, consider using a common data format from your printlist.py pyhon program in the first place, like JSON, or output newline or zero separated list from it. Commented Nov 10, 2021 at 12:39
  • @KamilCuk. I did that but got the same output as earlier. Commented Nov 10, 2021 at 12:44
  • 1
    I did that Well, what does "that" refer to exactly? Did you wrote a separate python script to convert your script output to a newline separated script? Did you modify printlist.py to output data in JSON format or as a newline separated list? What format is used by printlist.py script to output the data? Commented Nov 10, 2021 at 12:48
  • @KamilCuk I add a loop to print each string to new line but still it didn't work Commented Nov 10, 2021 at 14:11

3 Answers 3

2

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

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

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

mapfile -td '' myarr < <(python3 /path/printlist.py)
Sign up to request clarification or add additional context in comments.

2 Comments

It's printing very good but when I'm passing again to the .env file it's printing** 'Google USA (US Dollars,' 'million)'** can you please add a little code so that even when we pass it to the .env file it should look "Google USA (US Dollars, million)"
@swarna You can add double quotes by changing the print function in python to print('"' + i + '"' + "\000", end = "").
2

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.

Comments

1

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)")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.