python - 尝试设置新的最大值时出错
问题描述
我现在的程序必须从我已经开始工作的 CSV 文本文件中获取数据,但是当我尝试将最大值设置为距离 [计数器] 时,我收到一个错误,其中指出:
类型错误:“str”和“float”的实例之间不支持“>”
我理解这一点,但我正在努力让它发挥作用。
forename = [""] * 100
surname = [""] * 100
distance = [0.0] * 100
# Gets the members details from the file
#strip the file by /n to get seperate lines
# split it by commas to get each value in each line
def get_members_info():
counter = 0
with open("members.txt",'r') as readfile:
line = readfile.readline().rstrip('/n')
while line:
items = line.split(",")
forename[counter] = items[0]
surname[counter] = items[1]
distance[counter] = items[2]
line = readfile.readline().rstrip('/n')
counter +=1
return forename, surname, distance
#
def print_max_distance(forename, surname, distance):
maximum = 0.0
print (distance[0])
for counter in range (1, len(distance)):
if distance[counter] > maximum:
maxs = distance[counter]
print (maxs)
print (maxs)
#
get_members_info()
print_max_distance(forename, surname, distance)
解决方案
line
是一个字符串,所以当你拆分它时,你会得到一个字符串列表。然后,您尝试将这些字符串与失败的浮点数进行比较。
如果您可以保证距离始终是一个数字,那么您可以简单地替换distance[counter] = items[2]
为distance[counter] = float(items[2])
推荐阅读
- angular - 无法在 Teamcity 构建中使用业力运行单元测试。错误:“设置“PUPPETEER_SKIP_DOWNLOAD”环境变量以跳过下载。
- javascript - 插件开发人员在 Wordpress 中发送 AJAX 请求的最佳实践是什么
- entity-framework-core - 我的第二个上下文尝试进行第一个上下文所做的所有更改
- javascript - NextJs getStaticPaths 未呈现适当的页面,显示 404 错误
- java - 编写一个Java程序,将句子中每个单词的首字母大写
- ethereum - 如何增加 GassLimit(以太坊 PoA)?
- image - 在android和ios xamarin表单中单击或点击图像时如何显示数字(1,2,3)
- linux - 在 Centos8 中找不到 virtio_pci.ko(或 virtio-pci.ko)
- c# - 使用 C# 向 Postgresql 数据库批量插入或更新数据
- xaml - 日历日期选择器下一个按钮 UWP 是否有任何刷新事件?