0

My csv File sample :

'Date','Category','Ability'
'21,2,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'

My current code:

with open(Filepath,'r') as f :
 user_read=csv.reader(f)
 dict_date=[line['Date'] for line in user_read]
print(dict_date)

Error :

TypeError : list indices must be integer or slices,not str

My expected Output :

[21,2,5,4,5,6]

Any ideas.. So my Category and Ability has seperate Dictionary

eshirvana
  • 23,227
  • 3
  • 22
  • 38
  • `csv.reader` gives you a _list_, which you index using integers, e.g. `line[0]` gives the first element. If you want to reference your element using the column name, use `csv.DictReader` – Pranav Hosangadi Jun 13 '22 at 17:15
  • Also note that your list comprehension is going to give you a list of lists. Since you seem to want a flattened output, see https://stackoverflow.com/q/952914/843953 – Pranav Hosangadi Jun 13 '22 at 17:16

4 Answers4

2

You're accessing the rows as dicts, which is available through csv.DictReader, and also you're not setting the quotechar for the dialect you're using which therefore defaults to ".

The following does both and works in the way you want.

import csv
from io import StringIO

# I’m using StringIO to avoid writing a file,
# if the data is in a file keep using with open(…) as f
buf = StringIO(
    """\
'Date','Category','Ability'
'21,2,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'
"""
)

with buf as f:
    reader = csv.DictReader(f, quotechar="'")
    date = [
        int(v) for row in reader for v in row["Date"].split(",")
        ]

print(date)  # [21, 2, 5, 4, 5, 6]
ljmc
  • 4,830
  • 2
  • 7
  • 26
  • I am getting the same error.Why you used the 'buf'....We need to read the csv file that might be more that 100 Rows and columns – Prasanna Balaji Jun 14 '22 at 05:45
  • I used a StringIO to avoid writing a file, you should keep on opening your file the same way in with. PS, this code runs without error. – ljmc Jun 14 '22 at 06:40
1

Try:

import csv


all_data = []
with open("data.csv", "r") as f_in:
    reader = csv.reader(f_in, quotechar="'")

    next(reader)  # skip header

    for row in reader:
        all_data.extend(map(int, row[0].split(",")))

print(all_data)

Prints:

[21, 2, 5, 4, 5, 6]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Yes it works,But why we have to give row[0]..There is no certainty we can tell the "Date " would be Cell[0,0]So is that possible to get values using header name...? – Prasanna Balaji Jun 14 '22 at 05:44
1

You can accomplice it using Pandas:

import pandas as pd

df = pd.csv_reader("input.csv")
output = df['Date'].to_list()

The output will be:

['21,2,5', '4,5,6']
Prashant Maurya
  • 639
  • 4
  • 13
  • I dont want to use pandas....My Policy wont allow that to install additional libraries..your o/p has 2 elements,but i need 6 elements ....we need to remove" ' ' " using regex or anyother function...Any idea on removing single quotes – Prasanna Balaji Jun 14 '22 at 10:43
0

I didnt use pandas due to policy restriction Below the code which worked for me

Date_values=[]
with open(users_read,'r') as f:
  reader=DictReader(f)
  for row in reader:
    Date_values+=row['Date'].split(",")
  print(Date_values)