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

有帮助吗?

解决方案

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

f = r"""awk -F"\t" '{print$5" "$6" "$7}' file.txt | sort | uniq -c"""
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top