如何在不使用任何类型的导入的情况下筛选CSV文件并对值进行分组



我的项目要求我有一个名为def main(csvfile,country,type):

我将解释项目要求,以便了解如何开始这个项目。该项目要求我找出每个月每个国家新冠肺炎阳性病例数的最小值、最大值、标准差和相关性。在这种情况下,阳性病例=csvfile中的新病例。主函数将用于测试整个Python脚本,以检查测试用例。TYPE有一个复杂性:

type是输入参数,它提到需要哪种类型的分析。可能需要两个字符串输入中只有一个:"statistics"或"correlation"。如果第三次输入论点是"统计学",那么程序的目标是找到统计学单个国家的分析。否则,如果第三个输入参数是"correlation",则该程序的目的是找出两个国家的统计数据的相关性。

到目前为止,这是我写的代码:

def main(csvfile,country,type):
try:
Fileopen = open(csvfile,"r")                                                             #code opens the csv file 
Fileread = Fileopen.read()
Filelines = Fileread.split("n")                                                          #splits the list with a new line
listname = []

for line in Filelines:
Filevalues = line.split(",")                                                          #splits the  list with a comma 
listname.append(Filevalues)

for index in range (len(listname)):                                                       #loop checks and removes any empty lists from the csv
if listname[index] == ['']:
listname.pop(index)
except FileNotFoundError:
print('please check if the name of the file is correct')
quit()

因此,我很困惑如何开始过滤每个国家的csvfile和每个月的病例数,然后找到最小值/最大值等等。只需要一点指导。

csvfile看起来像

首先,清除数据的额外循环可以是一行简单的代码:

listname = [x for x in listname if x and x[0]]

同样的方式也可以应用于你的过滤器,比如说你希望搜索一个特定的国家";阿尔巴尼亚";。知道你的位置栏的索引是";2〃;(第三栏(你可以简单地这样做:

countryfilter = [x for x in listname if len(x) >= 2 and x[2] == "Albania"]
print(countryfilter)

以下是一些例子,可以对新病例进行统计分析:

new_cases = [int(x[4]) for x in countryfilter] 
new_cases.sort()
min_new_cases = new_cases[0]
max_new_cases = new_cases[-1]
average = sum(new_cases) / len(new_cases)

最新更新