简体   繁体   English

如何将输入的一部分作为变量?

[英]How do I take part of an input as a variable?

Hello people of Stackoverflow, me again. 大家好,我是Stackoverflow。 I'm creating a basic Ubuntu terminal emulator as a little mini project in my journey to learn Python. 在学习Python的过程中,我将创建一个基本的Ubuntu终端模拟器作为一个小项目。

I need to take part of an input and turn it into a variable, so I can add it to a "command output". 我需要输入的一部分并将其转换为变量,因此可以将其添加到“命令输出”中。 Example: If someone puts "sudo add-apt-repository ppa:webupd8team/tor-browser", the code would be able to pull out "webupd8team", and use that as part of the output, and then pull tor-browser, and use that as part of the output. 示例:如果有人放置“ sudo add-apt-repository ppa:webupd8team / tor-browser”,则该代码将能够提取“ webupd8team”,并将其用作输出的一部分,然后提取tor-browser,然后将其用作输出的一部分。

I already have a few if/else filters checking for certain commands (Sudo for example) but I can't really go through every single possible option for some things. 我已经有一些if / else过滤器来检查某些命令(例如,Sudo),但是对于某些事情,我无法真正通过每个可能的选项。

if "sudo" in originalInput: #Checks to see if "sudo" is in the command input. 
password = input("[sudo] enter password for evan:") #Creating another input if sudo is found in input1


if password == "1qaz2wsx":

  if "cd" in originalInput:
    print("This is here for testing purposes") #Haven't started working on the CD command yet
    CommandPrompt()

  if "add-apt-repository ppa:" in originalInput:
    confirm = input("You are about to add this PPA to your system. Confirm?")
      if confirm == "1":  #Just here to pressing enter will activate ppa_install(), function made to mimic PPA installation
      return True

      else:  
        ppa_install()
        CommandPrompt()

I already have this thing set up for the PPA command, but I need to be able to make the code pull the things AFTER sudo/add-apt/whatever and use them in the output. 我已经为PPA命令设置了这个东西,但是我需要能够使代码在sudo / add-apt / whatever之后拉东西并在输出中使用它们。

It appears that you need either the split method, which splits a string at a given delimiter (space is the default), or the regex package. 看来您需要使用split方法(该方法将在给定的定界符(默认为空格)处分割字符串)或regex软件包。

In this case, the quick, low-tech way would be to split the string on spaces and pull out "ppa:webupd8team/tor-browser". 在这种情况下,一种快速,低技术的方法是在空格上分割字符串并拉出“ ppa:webupd8team / tor-browser”。 Divide that at the colon and slash by whatever string-processing means you choose (find, index, string slice), and there's your desired string. 用冒号和斜线除以您选择的任何字符串处理方式(查找,索引,字符串切片),即可得到所需的字符串。

See the documentation for details. 有关详细信息,请参见文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何分离输入的一部分并将其转换为变量? - How do I separate a part of an input and convert it into a variable? 如何从用户那里获取输入,将其存储在一个空变量中并打印该变量? - How do I take input from a user, store it in an empty variable and print the variable? 如何从 discord 获取用户输入并将其存储为变量以在方程式中使用? Discord.py - How do i take user input from discord and store it as a variable to use in an equation? Discord.py 如何获取可变数量的文件作为python脚本的输入? - How can I take a variable number of files as input for a python script? 如何让itemgetter从列表变量中获取输入? - How can I make itemgetter to take input from list variable? 如何在 python 中将多个文件作为输入并存储在变量中 - How can I take multiple file as input in python & store in a variable 如何获取价格并将其转换为我可以使用的变量 - How do I take the price and turn it into a variable I can use 如何在python中使用getopt将字符串作为输入 - How do I take a string as input using getopt in python 如何获取输入并将其保存到多个变量中 - How do I take input and save it into multiple variables 如何在不刷新 flask 页面的情况下获取用户输入? - how do I take user input without refreshing the page in flask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM