Как разделить строку с shell-командой на отдельные аргументы в виде списка?
Если сделать просто сплит по пробелу то получим то что надо, кроме случаев со вставками текста с пробелами. Например так:
>>> '-arg "I Am Groot"'.split(' ')
['-arg', '"I', 'Am', 'Groot"']
Чтобы учитывать текст в кавычках как единый аргумент можно воспользоваться функцией shlex.split()
Кто читает мой канал давно, уже в курсе.
А что делать, если нужно обратное действие? Объединить аргументы из списка в строку и при этом добавить кавычки в аргумент с пробелами.
Конечно, если вы используете subprocess то он сам всё разрулит. Но если вам нужна именно команда одной строкой, то можно воспользоваться готовой функцией в том же subprocess.
>>> from subprocess import list2cmdline
>>> list2cmdline(['-arg', 'I Am Groot'])
'-arg "I Am Groot"'
Он также позаботится об экранировании уже имеющихся кавычек
>>> list2cmdline(['-arg', 'I Am "Groot"'])
'-arg "I Am \"Groot\""'
А вот так он может "схлопнуть" в команду JSON
>>> list2cmdline(['--json', json.dumps({'key': 'value'})])
'--json "{\"key\": \"value\"}"'
_______________
Возможно кто-то спросит, а зачем соединять аргументы в строку если subprocess сам это сделает а os.system не наш путь?
Мне как-то потребовалось отправлять команду на удалённое выполнение и в API поддерживалось указание команды только строкой. Так что всякое бывает)
#libs#basic
#RIF/USDT analysis :
Following a significant decline of -83% from its all-time high, the price has found support at the $0.0600 level. Subsequently, it has bounced back and successfully broken above the trendline, indicating a confirmation of anticipated bullish momentum. It is expected that the price will continue this upward trajectory and test the previous swing high.
TF : 1W
Entry : $0.1117
Target : $0.3377
SL : $0.0766
#RIF/USDT analysis :
#RIF is trading above the support zone and the 200 EMA. The price is anticipated to rebound from this level and proceed to rise, testing the previous swing high.
TF : 15min
Entry : $0.0977
Target : $0.1007
SL : $0.0956
#RIF yielded an impressive 3,010% return in a few days ⭐
If you used 200x leverage on this signal, you would've made a mind-blowing profit of 12,040%🤩
$50💰 would return $750💵
$50 💰 would return $1,500
💲
$50💰 would return $6,000💰
When you understand the market like we do, every move becomes predictable With 5+ years of trading experience, we spot these moves before they happen 💯
If you want daily signals like this, DM me to join our private group! 👇
@Kevindexter✅
#RIF
I'm expecting roughly this kind of movement on this asset, either with streaming start ups behind our magnets from above.
📈Sign up on Bybit to trade with me