python打开包含整数的文件
python新手,有人能告诉我为什么下面的代码会抛出错误吗?并建议我修复python打开包含整数的文件,python,python-3.x,Python,Python 3.x,python新手,有人能告诉我为什么下面的代码会抛出错误吗?并建议我修复 def cel_to_fahr(c): if c < -273.15: return "physical matter can reach only till -273.15" else: f = c * 9/5 + 32 return f temp = open("tempr.txt") tempr = temp.read() tempr
def cel_to_fahr(c):
if c < -273.15:
return "physical matter can reach only till -273.15"
else:
f = c * 9/5 + 32
return f
temp = open("tempr.txt")
tempr = temp.read()
tempr = tempr.splitlines()
for t in tempr:
print(cel_to_fahr(t))
错误:
Traceback (most recent call last):
File "cel_to_fahr.py", line 41, in <module>
print(cel_to_fahr(t))
File "cel_to_fahr.py", line 29, in cel_to_fahr
if c < -273.15:
TypeError: '<' not supported between instances of 'str' and 'float'
回溯(最近一次呼叫最后一次):
文件“cel_to_fahr.py”,第41行,在
印刷品(cel_至fahr(t))
文件“cel_to_fahr.py”,第29行,在cel_to_fahr中
如果c<-273.15:
TypeError:“如果要将字符串与浮点进行比较,请尝试以下操作:
def cel_to_fahr(c):
if c < -273.15:
return "physical matter can reach only till -273.15"
else:
f = c * 9/5 + 32
return f
temp = open("tempr.txt")
tempr = temp.read()
tempr = tempr.splitlines()
for t in tempr:
print(cel_to_fahr(float(t)))
def cel_至_fahr(c):
如果c<-273.15:
return“物理物质只能到达-273.15”
其他:
f=c*9/5+32
返回f
temp=open(“tempr.txt”)
tempr=temp.read()
tempr=tempr.splitlines()
对于温度中的t:
打印(cel_to_fahr(float(t)))
假设文件中只有float
从文件中读取的数据将作为字符串读取,直到您强制转换它们为止
如果不确定数据的格式,可以使用try/catch逻辑。但是对于您的一个简单问题,即文件中的所有数据都是已知的,这就太过分了。这是非常基本的python。您可以阅读更多关于编写和读取文件的内容。Python中的一个常见做法是与。。。打开这将确保您在之后关闭该文件。考虑这个例子(工作)与…打开它还将创建该文件
with open('tempr.txt','w') as f:
f.write('10\n-20\n-289\n100')
def cel_to_fahr(c):
if c < -273.15:
return "physical matter can reach only till -273.15"
else:
return c * 9/5 + 32
with open('tempr.txt') as f:
tempr = [float(i) for i in f.read().split('\n')]
for t in tempr:
print(cel_to_fahr(t))
以open('tempr.txt','w')作为f的:
f、 写入('10\n-20\n-289\n100')
def cel_至_fahr(c):
如果c<-273.15:
return“物理物质只能到达-273.15”
其他:
返回c*9/5+32
将open('tempr.txt')作为f:
tempr=[f.read().split('\n')中i的浮点(i)]
对于温度中的t:
印刷品(cel_至fahr(t))
已经给出了很好的答案,如果不小心使用了字符串,下面的将转换为float。另请参见第行中的注释/提示:
# use type hinting to indicate what type of args should be used
def cel_to_fahr(c: float) -> float:
# use doc string to describe your function
"""
usage: a = cel_to_fahr(x)
x = degrees celcius (pos/neg) as float
a = return degrees fahrenheit (pos/neg) as float
Will raise ValueError when x < -273.15
"""
# you can change to float if it is not float or int to begin with
c = c if isinstance(c, (float, int)) else float(c)
# check if c is indeed 0 K (-273.15 C) or up
if c >= -273.15:
return c * 9/5 + 32
# Errors should never pass silently.
# Unless explicitly silenced.
raise ValueError('Physical matter can reach only till -273.15')
#使用类型提示来指示应该使用哪种类型的参数
def cel_至_fahr(c:浮动)->浮动:
#使用文档字符串描述您的函数
"""
用法:a=cel_至_fahr(x)
x=浮点数时的摄氏度(正/负)
a=作为浮动的返回华氏度(正/负)
当x<-273.15时,将引发值错误
"""
#如果开头不是float或int,则可以更改为float
c=c,如果i持续(c,(float,int)),否则浮点(c)
#检查c是否确实为0 K(-273.15 c)或更高
如果c>=-273.15:
返回c*9/5+32
#错误永远不应该悄无声息地过去。
#除非明确沉默。
raise VALUE ERROR('物理物质只能达到-273.15')
@gkiranreddy根据错误消息,您似乎只应用了第一个浮点转换,而不是第二个。实际上,我建议您在最初调用函数时执行类似于cel\u to_fahr(float(t))
的操作。我尝试了您的建议,得到了以下错误:文件“cel\u to_fahr.py”,第33行返回f^IndentationError:意外indent@Luc我刚刚读了您评论的最后一行“假设您的文件中只有浮点”-我只是在传递整数正值和负值。偏移错误意味着您在复制/粘贴时遗漏了一个空格。整数包含在float中,所以应该很好:)为什么要一次读取所有文件,然后一次转换所有文件?只需逐行转换值。感谢您的回答,是的,这是非常基本的python。正如问题中提到的,我对python是新手,事实上我对编码也是新手。再次感谢你的回答,它奏效了。我会读更多关于文件的内容。@gkiranreddy别担心。我们都在这里学习。我希望你能通过开放学习。。。它真的很有用!祝你好运
# use type hinting to indicate what type of args should be used
def cel_to_fahr(c: float) -> float:
# use doc string to describe your function
"""
usage: a = cel_to_fahr(x)
x = degrees celcius (pos/neg) as float
a = return degrees fahrenheit (pos/neg) as float
Will raise ValueError when x < -273.15
"""
# you can change to float if it is not float or int to begin with
c = c if isinstance(c, (float, int)) else float(c)
# check if c is indeed 0 K (-273.15 C) or up
if c >= -273.15:
return c * 9/5 + 32
# Errors should never pass silently.
# Unless explicitly silenced.
raise ValueError('Physical matter can reach only till -273.15')