Kiro
Kiro

Reputation: 5

Printing a specific element when reading CSV File using python

Assuming I have the below dataset

10,"XG16008168",9/12/2017 0:00:00,9/13/2017 0:00:00,2/23/2018 0:00:00,"Whatever","07210","25978","Main","Yes",3/9/2018 0:00:00,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,,0,0,0,0,0,0,0,0,0,1,,0,,0,,0,0,0,0,0,1,0,0,0,0,0,0,,0,
11,"X000000000",11/30/2017 0:00:00,9/25/2017 0:00:00,2/27/2018 0:00:00,"Whatever 004","07210","25978","Main","Yes",3/9/2018 0:00:00,,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,,0,0,0,0,0,0,0,0,0,1,"Missing valve number.",0,,0,,0,0,0,0,0,0,1,0,0,0,0,0,,0,

I read this CSV file using the following:

with open("AccessTable.txt", "r") as RTCData:
    with open("RTCOutput.csv", "w") as RTCOutput:
        ALLRows = csv.reader(RTCData, delimiter=',')

        for row in ALLRows:
            rows.append(row)

            print(row[1][1])

I am trying to print an element of the CSV file.

So by printing row[1][1] I was expecting "X000000000" but instead I get "1" which is the second character in "11".

Can you please tell me how to spit out how to extract elements?

Upvotes: 0

Views: 5443

Answers (4)

UtahJarhead
UtahJarhead

Reputation: 2217

Just a small bit of clean up for your future use as well as your answer. When you use your for row in ALLRows: statement, you are iterating through all of the rows already. That means row now contains:

11,"X000000000",11/30/2017 0:00:00,9/25/2017 0:00:00,2/27/2018 0:00:00,"Whatever 004","07210","25978","Main","Yes",3/9/2018 0:00:00,,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,,0,0,0,0,0,0,0,0,0,1,"Missing valve number.",0,,0,,0,0,0,0,0,0,1,0,0,0,0,0,,0,

If you want to iterate through the individual items of THAT you can then do the below

# Put both open statements on a single line.  A bit cleaner, but does the same thing.
with open("AccessTable.txt", "r") as RTCData, open("RTCOutput.csv", "w") as RTCOutput:
    ALLRows = csv.reader(RTCData, delimiter=',')

    # Make a huge array from the CSV file
    rows = [row for row in ALLRows]
    rows.append(row)

    # OR
    for row in ALLRows:
        # Iterate through all of the cells of this row
        for cell in row:
            print(cell)

        # And to access a single item of the row:
        part_number = row[1]

Good luck!

Upvotes: 0

jackotonye
jackotonye

Reputation: 3853

>>> import csv
>>> with open('eggs.csv', 'rb') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

Upvotes: 0

Lavanya Pant
Lavanya Pant

Reputation: 702

You are using row[1][1] but it should be row[1].

Try printing row in python shell you will able to analysis thing easily. Example index value of each item, row is a list of items.

with open("/home/lavanya/Desktop/AccessTable.txt", "r") as RTCData:

    with open("RTCOutput.csv", "w") as RTCOutput:

        ALLRows = csv.reader(RTCData, delimiter=',')

        for row in ALLRows:

            rows.append(row)
            print row
            print(row[1][1])

Upvotes: 1

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

You are displaying row, not rows, so you are displaying a character from the second element of each row.

Upvotes: 0

Related Questions