TGTGInsighttelegram intelligenceLIVE / telegram public index
← Python Заметки

TGINSIGHT SIMILAR POSTS

Најди сличен содржај

Изворен канал @pythonotes · Post #191 · 14 дек.

Как разделить строку с 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

Hashtags

Резултати

Пронајдени 8 слични објави

Пребарај: #allaboutbuses

当前筛选 #allaboutbuses清除筛选
SG MRT Updates

@sgmrt · Post #2230 · 22.01.2025 г., 03:44

Train services on the North-South, East-West, Circle & Thomson-East Coast Lines, and some bus services will be extended for commuters travelling late on the eve of Chinese New Year, 28 Jan (after midnight). See poster for more info.🧧#SMRT#AllAboutTrains#AllAboutBuseshttps://bit.ly/4g6JEzO - SMRT

SG MRT Updates

@sgmrt · Post #1947 · 31.05.2024 г., 02:09

Train services on the North-South, East-West, Circle & Thomson-East Coast Lines, and some bus services will be extended for commuters travelling late on the eve of Hari Raya Haji, 16 Jun 2024 (after midnight). Ref to the poster for more info. #SMRT#AllAboutTrains#AllAboutBuseshttps://bit.ly/4bWEkx9 - SMRT

SG MRT Updates

@sgmrt · Post #1902 · 04.04.2024 г., 02:29

Train services on North-South, East-West, Circle & Thomson-East Coast Lines, and some bus services will be extended for commuters travelling late on eve of Hari Raya Puasa, 9 Apr 2024 (after midnight). Ref to the poster for more info. #SMRT#AllAboutTrains#AllAboutBuseshttps://bit.ly/3vLtQRF - SMRT

SG MRT Updates

@sgmrt · Post #1864 · 11.12.2023 г., 09:05

Train services on the 🚆 North-South, East-West, Circle & Thomson-East Coast Lines, and some 🚍 bus services will be extended for commuters travelling late during the eve of Christmas and New Year. See the poster for more info. #SMRT#AllAboutTrains#AllAboutBuseshttps://bit.ly/3NjRnyE - SMRT

SG MRT Updates

@sgmrt · Post #1709 · 24.04.2023 г., 10:45

Train services on the 🚊 North-South, East-West, Circle & Thomson-East Coast Lines, and some 🚍 bus services will be extended for commuters travelling late on the eve of Labour Day, 30 April 2023. Refer to the poster for more info. 👀#SMRT#AllAboutTrains#AllAboutBuseshttps://bit.ly/41RQZwf - SMRT

SG MRT Updates

@sgmrt · Post #1583 · 05.01.2023 г., 05:16

Train services on the 🚆 North-South, East-West, Circle & Thomson-East Coast Lines, and some 🚍 bus services will be extended for commuters travelling late on the eve of Chinese New Year. 🧧 Refer to the poster for more info! 👀#SMRT#AllAboutTrains#AllAboutBuseshttps://bit.ly/3igIWI8 - SMRT

SG MRT Updates

@sgmrt · Post #1563 · 09.12.2022 г., 09:28

Train services on the 🚆 North-South, East-West, Circle & Thomson-East Coast Lines, and some 🚍 bus services will be extended for commuters travelling late during the eve of Christmas and New Year. Refer to the poster for more info!🥳#SMRT#AllAboutTrains#AllAboutBuseshttps://bit.ly/3uDtXe8 - SMRT