pppp
pppp

Reputation: 677

iterate through csv character-by-character

I am currently doing an exercise building a python CSV parser. I need to be able to open a CSV file and read the contents, iterating through the CSV character-by-character. My python code is set up to iterate through strings however I've run into a errors doing this through the CSV.

Read method:

import csv
def parse(csv):
    #states
    is_token = False
    previous_character_is_escape = False
    no_quote_value = False

    csv_file = open(csv, 'r')
    iterator = (csv.reader(csv_file))

    for i in iterator:
        if is_token == False:
            if i == '"':
                print '\b' + i,
                is_token = True
                no_quote_value = False
            elif i == ',':
                print '\n',
            elif no_quote_value == True:
                print '\b' + i,
                is_token = True
            else:
                print '\b' + i,


        elif is_token == True:
            if i == '\\':
                print '\b' + i,
                previous_character_is_escape = True
            elif previous_character_is_escape == True and i == '"':
                print '\b' + i,
                previous_character_is_escape = False
            elif previous_character_is_escape == False and i == '"':
                print '\b' + i,
                is_token = False
                no_quote_value = True
            elif no_quote_value == True and i == ',':
                print '\n',
                is_token = False
            elif no_quote_value == False and i == ',':
                print '\b' + i,
            else:
                print '\b' + i,

parse('example.csv')

When I execute this I get the following error:

Traceback (most recent call last):
  File "main_test.py", line 47, in <module>
    parse('example.csv')
  File "main_test.py", line 10, in parse
    iterator = (csv.reader(csv_file))
AttributeError: 'str' object has no attribute 'reader'

Is there any way to open a CSV file and read the content character by character?

Upvotes: 1

Views: 224

Answers (2)

cblab
cblab

Reputation: 1935

A CSV file being basically a text file, I would say you should read it as a regular text file... In other words, if you want to build your own parser, you should not be using an existing parser...

with open(filename) as f:
    for l in f:
        for c in l:
            pass # your c-by-c processing here

Note that the regular column separator in "comma separated values" (CSV) format is a comma... But, sometimes (most of the time actually), it's a tab, a semi-colon, or some other chars... Take care about this while parsing... Regarding rows, "end-of-line character" (EOL) depends on the encoding of your file (standard on *nix is \n, on Mac is \r, on Windows is \r\n)

Upvotes: 1

Fingalzzz
Fingalzzz

Reputation: 109

That might because you name you parameter csv, which confuse the computer with the csv module, change another name and try it again.

Upvotes: 0

Related Questions