简体   繁体   English

使用python读取CSV文件时打印特定元素

[英]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: 我使用以下方法读取了此CSV文件:

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. 我正在尝试打印CSV文件的元素。

So by printing row[1][1] I was expecting "X000000000" but instead I get "1" which is the second character in "11" . 因此,通过打印row[1][1]我期望的是"X000000000"但是我得到的是"1" ,它是"11"的第二个字符。

Can you please tell me how to spit out how to extract elements? 您能告诉我如何吐出如何提取元素吗?

You are using row[1][1] but it should be row[1]. 您正在使用row [1] [1],但应为row [1]。

Try printing row in python shell you will able to analysis thing easily. 尝试在python shell中打印行,您将能够轻松地分析内容。 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])

您正在显示row ,而不是rows ,因此您正在显示每行第二个元素中的字符。

>>> 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

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. for row in ALLRows:语句中使用for row in ALLRows: ,您已经在所有行中进行迭代。 That means row now contains: 这意味着row现在包含:

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 如果要遍历THAT的各个项目,则可以执行以下操作

# 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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM