简体   繁体   English

使用 Python 从 CSV 文件中的指定行和列中提取值。 无法使用 CSV 模块或 pandas 模块

[英]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.该程序需要能够输入districtcountrydate ,并打印出该地点截至该日期的 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.

相关问题 不使用python csv模块将csv行(从for循环内)写出到csv文件 - write csv row (from within for loop) out to csv file without using python csv module 使用熊猫或csv模块将值写入csv文件中的给定文件 - Writing value to given filed in csv file using pandas or csv module 使用csv或pandas模块将python csv转换为字典 - python csv to dictionary using csv or pandas module 使用 Python 的 CSV 模块覆盖 csv 文件中的特定行 - Overwriting a specific row in a csv file using Python's CSV module 从具有指定行名和列名的 csv 文件中提取特定数据 - extract specific data from a csv file with specified row and column names 使用Python csv模块覆盖csv文件中的特定列 - Overwrite a specific column in a csv file using Python csv module 我应该使用什么代码从 csv 文件中提取特定列(带有特定数据)到 python。 它可以是 pandas 或 numpy - What code should I use in extracting specific column (with specific data) from a csv file to python. It can be either pandas or numpy 使用 CSV 模块更新 CSV 文件中的值 - Updating a value in a CSV file using CSV module 如何使用csv模块将列中的空值替换为其上方行中的值? - How can I use csv module to replace empty value in column with the value from the row above it? CSV文件-具有Pandas和Python模块 - CSV file - Have Pandas and Python module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM