[英]Extract value from specified row and column in CSV file using Python. Cannot use CSV module or pandas module
I have been provided with a.csv file, which has data on covid19.我收到了一个.csv 文件,其中包含有关 covid19 的数据。 It is in the form of:
它的形式是:
district | country | date1 | date2 | date3 |etc
victoria | australia |1 case | 3 cases |7 cases | etc
It is a fairly large file, with 263 rows of countries/districts, and 150 columns of dates.这是一个相当大的文件,有 263 行国家/地区和 150 列日期。
The program needs to be able to take in an input district
, country
, and date
and print out the number of COVID cases
in that location as of that date.该程序需要能够输入
district
、 country
和date
,并打印出该地点截至该日期的 COVID cases
数。 (print the value of a specified row and column of a CSV file) (打印CSV文件的指定行和列的值)
We have been instructed not to use the CSV module or the pandas module.我们被指示不要使用 CSV 模块或 pandas 模块。 I am having trouble understanding where to start.
我无法理解从哪里开始。 I will add my attempted solutions to this question as I go along.
我将在 go 中添加我尝试的解决方案来解决这个问题。 Not looking for a complete solution,but any ideas that I could try would be appreciated.
不寻找完整的解决方案,但我可以尝试的任何想法将不胜感激。
This is what I finally did to solve it.这就是我最终为解决它所做的。 It works perfectly.
它完美地工作。 for reference the data file I am using is: https://portland-my.sharepoint.com/:x:/g/personal/msharma8-c_ad_cityu_edu_hk/ES7eUlPURzxOqTmRLmcxVEMBtemkKQzLcKD6U6SlbX2-_Q?e=tc5aJF
for reference the data file I am using is: https://portland-my.sharepoint.com/:x:/g/personal/msharma8-c_ad_cityu_edu_hk/ES7eUlPURzxOqTmRLmcxVEMBtemkKQzLcKD6U6SlbX2-_Q?e=tc5aJF
# for the purpose of this answer I preset the country, province, and date
country = 'Australia'
province = 'New South Wales'
date = '3/10/2020'
with open('covid19.csv', 'r') as f:
final_list = []
list0 = f.readline().split(',')
for line in f:
if line.split(',')[0] == province:
final_list = line.split(',')
dict1 = dict(zip(list0,final_list))
print dict1[date]
I will use the same logic to finish the solution.我将使用相同的逻辑来完成解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.