0

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?

4
  • Are you reading the text file or a csv file ?? with open("AccessTable.txt", "r") as RTCData: Commented Oct 7, 2018 at 20:06
  • Thanks for your reply. I renamed the "AccessTable.txt" to "AccessTable.csv" but still getting the same output Commented Oct 7, 2018 at 20:10
  • I am trying to read a txt file generated by an Access database. Commented Oct 7, 2018 at 20:10
  • In my understanding you are victim of a typo. It's print(rows[1][1]) (note rows, not row). Another problem is that your code is seemingly misaligned, the print(rows[1][1]) shouldn't be outside of the with blocks, when all file i/o has been completed? Commented Oct 7, 2018 at 21:17

4 Answers 4

1

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])
Sign up to request clarification or add additional context in comments.

3 Comments

@kerlosSaad, As you can see this is a list, You can access these element by there index. pt = ['223', 'QG17001687', '8/31/2017 0:00:00', '9/1/2017 0:00:00', '4/2/2018 0:00:00', 'Qso Construction Department 600', '26509', '33123', 'Main', 'No', '', '', '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', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '', '0', '']. print pt[0]
row[1] would print the whole row instead I want to print one of the elements of that row.
No, row[1] will select the second element of the list. Debug your code...
0

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

1 Comment

so how to display the whole character not its element? Thanks!
0
>>> 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

1 Comment

then how would you print the first "Spam" only ?
0

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!

2 Comments

thank you that worked. C++ is my first language. Just started with python. so I appreciate the help!
WAY easier than c++! You just lose gobs of performance with python. I LOVE it, but I don't need gobs of speed most times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.