简体   繁体   English

如何从 python 中的输入值中分别获取数字和字母?

[英]How can I get the number and the letter separately from input value in python?

I want to build a converter from Celsius (C) to Fahrenheit (F) in python.我想在 python 中构建一个从摄氏度 (C) 到华氏度 (F) 的转换器。 How can I get the number and the letter (ex. 36C: C as string and 36 as an int) separately the user has to input in an input() built-in function and then save them in two different variables.如何分别获取数字和字母(例如 36C:C 作为字符串,36 作为 int),用户必须输入 input() 内置函数,然后将它们保存在两个不同的变量中。

I tried to save 36C(example) as a list, but it didn't work, because 36C is a string.我试图将 36C(example) 保存为列表,但没有成功,因为 36C 是一个字符串。 I could save it as an int.我可以将它保存为一个整数。 But I need 36 and C separately但是我分别需要 36 和 C

You could use a regex with something like:您可以使用正则表达式,例如:

import re

# ask input
inp = input()

# get digits, optional space(s), unit
m = re.match('(\d+)\s*([CF])', inp)

# if the match failed, input is invalid
if not m:
    print('invalid input')
# else get the 2 parts
else:
    value, unit = m.groups()
    value = int(value)
    print(value, unit)

Split the inputs uisng re and then unpack list - re拆分输入 uisng re然后解压列表 - re

import re
tem = input()
sp = re.split('(\d+)',tem)
sp = list(filter(None, sp))#remove empty elemnt
temp, degree_cel = sp[0], sp[1]
print(temp)
print(degree_cel)

output #输出 #

36
C

Regex is of course a very valid way to parse the user's input.正则表达式当然是解析用户输入的一种非常有效的方法。 If you wanted to parse it without using that library, we could also just assume the last charater of the user input is the measure:如果您想在不使用该库的情况下解析它,我们也可以假设用户输入的最后一个字符是度量:

Note, strings in python are already a sort of list like thing so probably no need to convert to one explicitely注意,python 中的字符串已经是一种类似列表的东西,所以可能不需要明确地转换为一个

## ---------------------
## get some input from the user
## ---------------------
temp_as_string = input().strip()
if not temp_as_string:
    raise ValueError("no input given")
## ---------------------

## ---------------------
## let's assume the last character is the units
## ---------------------
units = temp_as_string[-1].lower()
if units not in ["c", "f"]:
    raise ValueError("bad units")
## ---------------------

## ---------------------
## everything before the units is then the measure
## ---------------------
measure = temp_as_string[:-1]
if not measure:
    raise ValueError("bad measure")
measure = int(measure)
## ---------------------

print(f"{measure} degrees {units}")

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

相关问题 如何通过切片在字典 python 中分别获取键和值 - how can i get the key and value separately in dictionary python by slicing 如何在 python 中通过 re 从字符串中获取数字和字母? - How can I fetch number and letter from string by re in python? 我如何才能从拟合Python中分别画出高斯? - How can i draw separately the gaussians from fitting in python? 如何让python显示输入的第一个字母 - How to get python to display the first letter from input 我如何从输入中给出一个数字并在 python 中制作一个三角形 - how can i give a number from input and make a triangle in python 如何从文本文件中获取每一行并将其拆分,以便我可以在 python 中单独使用它们 - How do I get each line from a text file and split it so that I can use them all separately in python Python Webscraping:如何从输入标签中获取 value 属性中的字符串? - Python Webscraping: How can I get the string in the value attribute from the input tag? python,输入数字,输出字母 - python , input number, output letter python tkinter 读取用户输入文本 label 每个字母单独 - python tkinter read user input text label each letter separately Python 如何从 .whl 文件中获取版本号 - Python How can I get the version number from a .whl file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM