0
import csv
from csv import reader
with open("C:\Python37-32\Date_create_count.csv","r") as new:
    data = reader(new)
    for i in data:
        print(i)

provides the output as follows

C:\Users\ybs\PycharmProjects\Pyprgs\venv\Scripts\python.exe C:/Python_prgs/count_by_date_csv.py
['target_release\tid\tstate\trel_state\towner\ts\tp\ttype\tsubtype\t|title\tdate_create\tkeywords']
['et.3\t1053801\tWONT_FIX\tWONT_FIX\tuttams\t4\tB\tSW_SC\tscc\t|[SCC][Longevity]Backup Workflow failing with Token Expired Error\t20161206\tSC42_Triaged_Out', 'source=TestPlanned', 'target=5.0']
['et.3\t1088667\tWONT_FIX\tWONT_FIX\tsgundi\t4\tB\tSW_SC\tcommon_gui\t|External beta 3.0: Resource view selection has to be persisted\t20170530\tSC42_Triaged_Out', 'source=RFE', 'chunk=NA']

When I try to print only tid and tdate_create it gives error

    for i in data:
        print(i['tid'])

\Users\ybs\PycharmProjects\Pyprgs\venv\Scripts\python.exe C:/Python_prgs/count_by_date_csv.py
Traceback (most recent call last):
  File "C:/Python_prgs/count_by_date_csv.py", line 6, in <module>
    print(i['tid'])
TypeError: list indices must be integers or slices, not str

Process finished with exit code 1

How I split the string and get tid and tdate_create fields from the csv file

['target_release\tid\tstate\trel_state\towner\ts\tp\ttype\tsubtype\t|title\tdate_create\tkeywords']
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
yogesh BS
  • 11
  • 1

2 Answers2

1

Comma is the default delimiter. If you want tab you have to specify:

from csv import reader
with open(r"C:\Python37-32\Date_create_count.csv", "r", newline='') as new:
    data = reader(new, delimiter='\t')
    for i in data:
        print(i)

You may also want to use DictReader so you can access columns by name:

from csv import DictReader
with open(r"C:\Python37-32\Date_create_count.csv", "r", newline='') as new:
    data = DictReader(new, delimiter='\t')
    for i in data:
        print(i['id'],i['date_create'])
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0
import pandas as pd

df = pd.read_csv('C:\Python37-32\Date_create_count.csv')
print(df[['tid', 'tdate_']])

This uses pandas to read the csv into a dataframe, which is similar to an excel spreadsheet.

The print statement allows you to return the requested columns. There's other ways to do this too:

Selecting multiple columns in a pandas dataframe

Brndn
  • 676
  • 1
  • 7
  • 21