如题,详细来讲是,不但不能用split, dictionary和各种collection都不让用!
程序的要求是在一个txt格式的表格里,找到第九行最小的那个数字,并且通过这个数字,同时找到与数字同一列的,年份和四十四行的另一个数值。我的思路里,知道要用slicing,但是尝试写了一些之后发现因为没有剔除一行中的非数字字符所以运行不了。
min_num = 100000 start_index = 0 index_num = 0 min_index = 0 i = 0 def open_file(): '''Continously prompt for file until successfully opened.''' while True: in_file = input("input the filename:") try: in_file = open ("GDP.txt","r") return in_file except: print("Error. Please input again:") in_file = open_file() count = 1 for line in in_file: if count == 9: line9 = line print (len(line9)) print (line9[76:303:12]) while i < len (line9): if line9[i].isdigit() is False: checking_value = eval(line9[start_index:i]) if checking_value < min_num: min_num = checking_value min_index = index_num start_index = i + 1 index_num += 1 i += 1 print(min_num, min_index) count += 1 print (line9)
求助!我这样写方法对吗?有错误怎么修改?
cnaz
相关分类