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

Était-ce utile?

La 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"""
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top