如何读取csv文件并在进行一些操作后打印数据?

时间:2015-07-06 07:04:11

标签: python python-3.x

我在python中读取csv文件时遇到问题,而打印时只会反复打印标题/列名称而不打印数据。

输出仅包含列名称。不打印行值。请帮我解决这个问题。

我的代码如下。

import csv
import sys
import datetime
import math
import re

f=open('VehicleMaster.csv','r')
try:
    reader=csv.DictReader(f,delimiter=',')
    num=' '
    regx= re.compile('[,-/]')
    for row in reader:
        dt1=row['Date_Registered']
        for xd in dt1.splitlines():
            d,m,y=regx.split(xd)
            dt2=('/'.join((d.zfill(2),m,'20'+y.zfill(2) if len(y)==2 else y)))
            row['Date_Registered']=dt2
            dt3= str(datetime.datetime.strptime(dt2,'%d/%b/%Y').date())
            #print(dt3)
            row['Date_Registered']=dt3![enter image description here][1]
            #print(row)
            print(num, '\t'.join(row))
            if num == ' ':
                    num=0
            num=num+1

输出: -

Workstream_from_AL  Manager work_steam  risk_score  Base    Count   Total_Downtime_Hours    Class   Vehicle_Description C111111 Total_Maint_Costs   Date_Registered smr Workstream  Latest_Mileage  Base_Location   Major_Repair_   Average_Downtime_Hours  Age_in_Months__17_Mar_15_   Status  Ob_Board_Power_ No_of_SMR_Jobs  Reg_Number  down_time   Cost_Centre mileage Workshop    age Date_of_Reading
1 Workstream_from_AL    Manager work_steam  risk_score  Base    Count   Total_Downtime_Hours    Class   Vehicle_Description C111111 Total_Maint_Costs   Date_Registered smr Workstream  Latest_Mileage  Base_Location   Major_Repair_   Average_Downtime_Hours  Age_in_Months__17_Mar_15_   Status  Ob_Board_Power_ No_of_SMR_Jobs  Reg_Number  down_time   Cost_Centre mileage Workshop    age Date_of_Reading
2 Workstream_from_AL    Manager work_steam  risk_score  Base    Count   Total_Downtime_Hours    Class   Vehicle_Description C111111 Total_Maint_Costs   Date_Registered smr Workstream  Latest_Mileage  Base_Location   Major_Repair_   Average_Downtime_Hours  Age_in_Months__17_Mar_15_   Status  Ob_Board_Power_ No_of_SMR_Jobs  Reg_Number  down_time   Cost_Centre mileage Workshop    age Date_of_Reading
3 Workstream_from_AL    Manager work_steam  risk_score  Base    Count   Total_Downtime_Hours    Class   Vehicle_Description C111111 Total_Maint_Costs   Date_Registered smr Workstream  Latest_Mileage  Base_Location   Major_Repair_   Average_Downtime_Hours  Age_in_Months__17_Mar_15_   Status  Ob_Board_Power_ No_of_SMR_Jobs  Reg_Number  down_time   Cost_Centre mileage Workshop    age Date_of_Reading

输入: - 计数Reg_Number状态Vehicle_Description Date_Registered Age_in_Months__17_Mar_15_ Latest_Mileage Date_of_Reading No_of_SMR_Jobs Total_Downtime_Hours Average_Downtime_Hours Total_Maint_Costs Major_Repair_ Manager Cost_Centre Ob_Board_Power_ Class Workshop Base_Location C111111 age mileage smr down_time work_steam risk_score Workstream Workstream_from_AL Base

1 PN57KTE电流 - 路上LEYLAND DAF LF45 160 Day 6.0 Der 20-May-08 81.92876712 195591 2-Feb-15 30 110.83 3.694333333 14269.36大型起重机维修£5,093.45工作否V79166 GORDON THOMPSON 40001072否HGV North East Truck& Van Ltd三通 - 修理1 0修理T恤

1 YP09LFX电流 - 在路上FORD TRANSIT 350 2.4 TDi Der / Man 10-Jun-09 69.23835616 62589 9-Mar-15 10 169.5 16.95 6166.99新发动机&注射器£4,287.57工作号V81240 BARRY WATSON 40001032是LCV红隼卡车服务有限公司北泰恩河 - 修理1 0.961643836 1.11E-05 3.548325662 3.941860465 1 9.451841074修理修理北泰恩河 1 YP09KZK电流 - 在路上FORD TRANSIT 350 2.4 TDi Der / Man 24-Jun-09 68.77808219 77171 12-Jan-15 12 40.25 3.354166667 5867.05新发动机£5,499.87工作否V78692 BARRY WATSON 40001032是LCV Howdon North Tyne - 修理1 0.955251142 1.11 E-05 3.375747986 0.78003876 1 6.111048999修复北泰恩河修理

1 YP09LCL电流 - 在路上FORD TRANSIT 350 2.4 TDi Der / Man 1-Jul-09 68.54794521 68703 19-Jan-15 10 101.25 10.125 8521.22新发动机£6,450.14工作否V85143 JOHN RICHARDSON 40001053是LCV S Jennings有限公司磨损 - 修理1 0.952054795 1.11E-05 4.902888377 2.354651163 1 9.209605446修理修理磨损

1 个答案:

答案 0 :(得分:0)

这是因为您使用csv.DictReader - 在对其项目进行迭代时,您的row变量将是dict类型的实例。 当你迭代dict(在join字符串方法中)时,你正在迭代它的键,这是行的名称,而你想迭代它的值。

因此,您应该将print(num, '\t'.join(row))替换为print(num, '\t'.join(row.values()))

注意:标准python词典是无序的。我不记得csv模块是否使用OrderedDict或类似的任何内容。