Question

I am attempting to feed a string to the terminal from python but I can't get my string formatted correctly.

c="awk -F\"\t\" '{print$5\" \"$6\" \"$7}' file.txt | sort | uniq -c"

when you look at this in the shell you get:

>>> f="awk -F\"\t\" '{print$5\" \"$6\" \"$7}' file.txt | sort | uniq -c"
>>> f
'awk -F"\t" \'{print$5" "$6" "$7}\' file.txt | sort | uniq -c'

Why are there \'s before the single quotes? How do I get rid of them? I have tried """string""" and '''string'''

In a previous program i had this:

>>> t="awk '{print $4}' file.txt | sort | uniq"
>>> t
"awk '{print $4}' file.txt | sort | uniq"

I'm not sure what the difference is

Was it helpful?

Solution

The solution is to use a raw triple-quoted string.

f = r"""awk -F"\t" '{print$5" "$6" "$7}' file.txt | sort | uniq -c"""
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top