Home > Enterprise >  python hard-coded multi-char delimiter vs passed multi-char delimiter
python hard-coded multi-char delimiter vs passed multi-char delimiter

Time:01-31

I have a python script which is using csv DictReader to read a csv with unicode delimiter '\x1f'.

I am running the script by calling a bash shell script which is passing the delimiter as follows:

python python_script.py '\x1f' import.csv

however, I am getting following error: TypeError: "delimiter" must be a 1-character string

but when I hard-code the delimiter into the python script like this:

reader = csv.DictReader(import.csv, delimiter='\x1f') it works, while

reader = csv.DictReader(import.csv, delimiter=sys.argv[1]) gives above 1-character string error mentioned above.

How can I pass the multi-byte delimiter from shell script above without hard-coding the delimiter in the python script?

CodePudding user response:

If you are using bash:

python python_script.py $'\x1f' import.csv

Shells don't interpret escape sequences, including \x.., so you need to use bash's ANSI-C escaping syntax.

Other shells have similar mechanisms, but there's not a lot of consistency. You can use "$(printf '\37')" on any Posix shell.

  • Related