从CSV fi读取行数据

2024-09-21 05:49:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试创建一个程序来读取CSV文件,更具体地说是行数据(横列中的数据)

示例数据.csv:

['Time', 'Date', 'Color  ', 'Name']
['1pm', '01-01-99', 'blue', 'jack']
['2pm', '02-02-99', 'green', 'kevin']
['3pm', '03-03-99', 'yellow', 'phil']
['4pm', '04-04-99', 'white', 'alice']
['5pm', '05-05-99', 'black', 'bob']

这是我的密码:

import csv  
with open('Sampledata.csv', 'r') as csvfile :
      regnumber = input("What is your regnumber?")
      reader = csv.reader(csvfile, delimiter=',')
for row in reader:
     print(row) # here lies the problem(python is reading columnal data (data going down) instead of row(lines across) data#

问题在于读取列(数据向下)。Python读取列。你知道吗

输出:

Date
01-01-99
02-02-99
03-03-99
04-04-99
05-05-99

Tags: 文件csv数据csvfile程序示例datadate
1条回答
网友
1楼 · 发布于 2024-09-21 05:49:39

这就是你要找的那个吗?你知道吗

import csv  #ignore my stupidity with the indentation and spaces#
with open('Sampledata.csv', 'r') as csvfile :
    regnumber = raw_input("Enter the time:")
    reader = csv.reader(csvfile)
    for row in reader:
        if(row[0]==regnumber):
            print ', '.join(row)
        else:
            continue

上面的代码逐行打印csv文件中的值。你知道吗

相关问题 更多 >

    热门问题