Python DictReader - Skipping rows with missing columns?

I have an Excel.CSV file that I am trying to read using DictReader.

Everything seems to be fine, except that it seems to skip rows, especially those that lack columns.

Our entry is as follows:

mail,givenName,sn,lorem,ipsum,dolor,telephoneNumber
ian.bay@blah.com,ian,bay,3424,8403,2535,+65(2)34523534545
mike.gibson@blah.com,mike,gibson,3424,8403,2535,+65(2)34523534545
ross.martin@blah.com,ross,martin,,,,+65(2)34523534545
david.connor@blah.com,david,connor,,,,+65(2)34523534545
chris.call@blah.com,chris,call,3424,8403,2535,+65(2)34523534545

Thus, some of the rows do not have lorem / ipsum / dolor columns, and for them it's just a comma.

We read it with:

def read_gd_dump(input_file="blah 20100423.csv"):
    gd_extract = csv.DictReader(open('blah 20100423.csv'), restval='missing', dialect='excel')
    return dict([(row['something'], row) for row in gd_extract])

And I checked that "something" (the key for our dict) is not one of the missing columns, I initially suspected that this was possible. This is one of the columns after that.

DictReader, , . restval -, , . , CSV Python (http://docs.python.org/library/csv.html), , , , - .

+3
2

- , list(gd_extract), :

[{'telephoneNumber': '+65(2)34523534545', 'ipsum': '8403', 'sn': 'bay', 'dolor': '2535', 'mail': 'ian.bay@blah.com', 'givenName': 'ian', 'lorem': '3424'}, {'telephoneNumber': '+65(2)34523534545', 'ipsum': '8403', 'sn': 'gibson', 'dolor': '2535', 'mail': 'mike.gibson@blah.com', 'givenName': 'mike', 'lorem': '3424'}, {'telephoneNumber': '+65(2)34523534545', 'ipsum': '', 'sn': 'martin', 'dolor': '', 'mail': 'ross.martin@blah.com', 'givenName': 'ross', 'lorem': ''}, {'telephoneNumber': '+65(2)34523534545', 'ipsum': '', 'sn': 'connor', 'dolor': '', 'mail': 'david.connor@blah.com', 'givenName': 'david', 'lorem': ''}, {'telephoneNumber': '+65(2)34523534545', 'ipsum': '8403', 'sn': 'call', 'dolor': '2535', 'mail': 'chris.call@blah.com', 'givenName': 'chris', 'lorem': '3424'}]

dicts, , ipsum .. , , .

something ( , ), , , " " - - csv reader, "" dict, . ?

+1

, Alex , csv "rb" "wb" ( Python 2.X). , . csv , BINARY.

, , , :
(1) (a) (b) a script (c) output -
(2) Python (3)

: Python 3.X : "" " csvfile - , newline=''. csv.reader, csv.writer, csv.DictReader csv.DictWriter.

0

All Articles