【问题标题】:Printing data from the csv file从 csv 文件打印数据
【发布时间】:2021-06-02 06:45:39
【问题描述】:

我想问如何从 CSV 文件中打印 2012 年的牙医人数。

import csv
csv_file = open('project.csv' , 'r')
data_file  = csv.reader(csv_file)
def opt_a():
   next(data_file)
   for row in data_file:
       i = row[1:]
       print(i)
opt_a()

这是我尝试过的,但我只能按行打印。

数据样本:

year,Private Dental Specialists,Private General Dental Practitioners,Public Dental Specialists,Public General Dental Practitioners
2008,116,864,47,268
2009,180,863,74,246
2010,185,874,87,267
2011,199,961,77,241
2012,203,1012,86,271
2013,219,1192,88,308
2014,216,1219,96,348
2015,215,1326,102,347
2016,219,1425,106,380
2017,232,1516,112,365
2018,189,1579,113,412
2019,237,1644,130,379

【问题讨论】:

  • 欢迎来到 SO。您能否编辑您的问题并告知您期望的输出是什么?也许还为您选择的流程/语言添加一个标签(似乎是 Python ?)

标签: python-3.x list csv


【解决方案1】:

在您的 for 循环中,只需检查您的行的第一个值是否为 2012 :

import csv
csv_file = open('project.csv' , 'r')
data_file  = csv.reader(csv_file)
def opt_a():
   next(data_file)
   for row in data_file:
       if row[0] == "2012":
           i = row[1:]
           print(i)
opt_a()

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2013-03-06
    • 2016-04-23
    相关资源
    最近更新 更多