在Python中,我如何解析数字字符串,如"545.2222"
相应的浮点值,545.2222
?或者将字符串解析为"31"
整数,31
?
我只是想知道如何将一个浮点数 解析str
为a float
,并且(单独)将一个int 解析str
为一个int
.
我希望我的Python函数分割一个句子(输入)并将每个单词存储在一个列表中.我当前的代码拆分了句子,但没有将单词存储为列表.我怎么做?
def split_line(text):
# split the text
words = text.split()
# for each word in the line:
for word in words:
# print the word
print(words)
Run Code Online (Sandbox Code Playgroud) import sys
print(sys.platform)
print(2**100)
raw_input()
Run Code Online (Sandbox Code Playgroud)
我正在使用Python 3.1并且无法raw_input
"冻结"dos弹出窗口.我正在阅读的书是Python 2.5,我使用的是Python 3.1
我该怎么做才能解决这个问题?
我正在编写一个必须接受用户输入的程序.
#note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
age = int(input("Please enter your age: "))
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")
Run Code Online (Sandbox Code Playgroud)
如果用户输入合理数据,这将按预期工作.
C:\Python\Projects> canyouvote.py
Please enter your age: 23
You are able to vote in the United States!
Run Code Online (Sandbox Code Playgroud)
但如果他们犯了错误,那就崩溃了:
C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Traceback (most recent call last):
File "canyouvote.py", line 1, in …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个读取文件的程序,如果文件的第一行不是空白,则会读取接下来的四行.在这些行上执行计算,然后读取下一行.如果该行不为空,则继续.但是,我收到此错误:
ValueError: invalid literal for int() with base 10: ''.
它正在读取第一行但不能将其转换为整数.
我该怎么做才能解决这个问题?
代码:
file_to_read = raw_input("Enter file name of tests (empty string to end program):")
try:
infile = open(file_to_read, 'r')
while file_to_read != " ":
file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
file_to_write = file_to_write + ".csv"
outfile = open(file_to_write, "w")
readings = (infile.readline())
print readings
while readings != 0:
global count
readings = int(readings)
minimum = (infile.readline())
maximum = (infile.readline())
Run Code Online (Sandbox Code Playgroud) 如何将函数应用于变量输入列表?例如,filter
函数返回true值,但不返回函数的实际输出.
from string import upper
mylis=['this is test', 'another test']
filter(upper, mylis)
['this is test', 'another test']
Run Code Online (Sandbox Code Playgroud)
预期的产出是:
['THIS IS TEST', 'ANOTHER TEST']
Run Code Online (Sandbox Code Playgroud)
我知道upper
是内置的.这只是一个例子.
这是一个简单的问题,我不想在这里问它,但我似乎无法在其他任何地方找到答案:是否有可能在一行Python中从用户那里获得多个值?
例如,在CI中可以做这样的事情:scanf("%d %d", &var1, &var2)
.但是,我无法弄清楚Python的等价物是什么.我认为它只是类似的东西var1, var2 = input("Enter two numbers here: ")
,但这不起作用,我不抱怨,因为如果它确实没有多大意义.
有没有人知道一个优雅和简洁的好方法?
如何将空格分隔的整数输入转换为整数列表?
输入示例:
list1 = list(input("Enter the unfriendly numbers: "))
Run Code Online (Sandbox Code Playgroud)
转换示例:
['1', '2', '3', '4', '5'] to [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud) 嗨,我是python的新手,想要在数组中输入.关于数组没有很好地描述python doc.另外我觉得我对python中的for循环有一些打嗝.
我在python中提供了我想要的C代码片段:
C代码:
int i;
printf("Enter how many elements you want: ");
scanf("%d", &n);
printf("Enter the numbers in the array: ");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
Run Code Online (Sandbox Code Playgroud) 所以我raw_input
作为一些列表的输入.
x= raw_input()
Run Code Online (Sandbox Code Playgroud)
我输入的地方1 2 3 4
如果我只输入数字,我将如何将其转换为整数列表?
python ×10
list ×4
input ×2
integer ×2
python-3.x ×2
function ×1
loops ×1
parsing ×1
python-2.7 ×1
raw-input ×1
split ×1
user-input ×1
validation ×1
variables ×1