how to read data from csv to feed them to cnn? - python-3.x

I have the following csv structure:
image_id, class_name, color, browneyes, feature2, feature3, feature4
for example:
429759,dog,black,1,0,0,husky
352456,cat,white,0,0,0,any
how can i read the csv file so for each row it reads the image file and feeds it to the model? (the image_id is the image filename)

you haven't mentioned what language you code in your question, but since you've mentioned Python in question's tags I show you an example in python
in python there is a nice python built-in module named csv that you can use it for working with CSV files
see the code below
CSV sample
name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March
Code
import csv
with open('employee_birthday.txt') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
line_count += 1
print(f'Processed {line_count} lines.')
reference : https://realpython.com/python-csv/
official docs : https://docs.python.org/3/library/csv.html

Related

Printing data from the csv file

I wanna ask how do I print the number of dentists respective in 2012 from the CSV file.
import csv
csv_file = open('project.csv' , 'r')
data_file = csv.reader(csv_file)
def opt_a():
next(data_file)
for row in data_file:
i = row[1:]
print(i)
opt_a()
This what I have tried but I will only be able to print out in rows.
Sample of data :
year,Private Dental Specialists,Private General Dental Practitioners,Public Dental Specialists,Public General Dental Practitioners
2008,116,864,47,268
2009,180,863,74,246
2010,185,874,87,267
2011,199,961,77,241
2012,203,1012,86,271
2013,219,1192,88,308
2014,216,1219,96,348
2015,215,1326,102,347
2016,219,1425,106,380
2017,232,1516,112,365
2018,189,1579,113,412
2019,237,1644,130,379
In your for loop, simply check if the first value of your row is 2012 :
import csv
csv_file = open('project.csv' , 'r')
data_file = csv.reader(csv_file)
def opt_a():
next(data_file)
for row in data_file:
if row[0] == "2012":
i = row[1:]
print(i)
opt_a()

Read File and count the words in column in python

I'm new in python and I need some help on read the file and count the word in column.
I have 2 data file, which is category.csv and data.csv.
category.csv:
CATEGORY
Technology
Furniture
Office Supplies
and below is data.csv
CATEGORY
Technology
Furniture
Technology
Furniture
Office Supplies
First, I want to select the 'Technology' in category.csv and match it with data.cvs, after that, it will start to count 'Technology' appears how many times in data.cvs.
import csv # import csv file
filePath1 = "category.csv"
filePath2 = "data.csv"
with open(filePath1) as csvfile1: # open category file
with open(filePath2) as csvfile2: # open data file
reader1 = csv.DictReader(csvfile1) # dictread file
reader2 = csv.DictReader(csvfile2) # dictread file
for row1 in reader1: # read all row in data file
for row2 in reader2:
for row1['CATEGORY'] in row2['CATEGORY']:
total_tech = row2['CATEGORY'].count('Technology')
total_furn = row2['CATEGORY'].count('Furniture')
total_offi = row2['CATEGORY'].count('Office Supplies')
print("=============================================================================")
print("Display category average stock level")
print("=============================================================================")
print( "Technology :", total_tech)
print("Furniture :", total_furn)
print("Office Supplies :", total_offi)
print( "=============================================================================")
But i'm failed to count it with above code, can somebody help me ? Thank you so much.
Here is the solution -
import csv # import csv file
filePath1 = "category.csv"
filePath2 = "data.csv"
categories = {}
with open(filePath1) as csvfile: # open category file
reader = csv.DictReader(csvfile) # dictread file
for row in reader: # Create a dictionary map of all the categories, and initialise count to 0
categories[row["CATEGORY"]] = 0
with open(filePath2) as csvfile: # open data file
reader = csv.DictReader(csvfile) # dictread file
for row in reader:
categories[row["CATEGORY"]] += 1 # For every item in data file, increment the count of the category
print("=============================================================================")
print("Display category average stock level")
print("=============================================================================")
for key, value in categories.items():
print("{:<20} :{:>4}".format(key, value))
print("=============================================================================")
The output is like this -
=============================================================================
Display category average stock level
=============================================================================
Technology : 2
Office Supplies : 1
Furniture : 2
=============================================================================

how to remove the quotations from the string in python?

the csv file returns the column value as dictionary format.but i cant get the value from dictionary by using dic.get("name") .it shows an error like ['str' object has no attribute 'get'].the actual problem is csv return the dict with quates so the python consider this as string.how to remove the quates and how can i fix it. please help!!
with open('file.csv') as file:
reader=csv.reader(file)
count=0
for idx,row in enumerate(reader):
dic=row[5]
if(idx==0):
continue
else:
print(dic.get("name"))
filename file_size file_attributes region_count region_id region_shape_attributes region_attributes
adutta_swan.jpg -1 {"caption":"Swan in lake Geneve","public_domain":"no","image_url":"http://www.robots.ox.ac.uk/~vgg/software/via/images/swan.jpg"} 1 0 {"name":"rect","x":82,"y":105,"width":356,"height":207} {"name":"not_defined","type":"unknown","image_quality":{"good":true,"frontal":true,"good_illumination":true}}
wikimedia_death_of_socrates.jpg -1 {"caption":"The Death of Socrates by David","public_domain":"yes","image_url":"https://en.wikipedia.org/wiki/The_Death_of_Socrates#/media/File:David_-_The_Death_of_Socrates.jpg"} 3 0 {"name":"rect","x":174,"y":139,"width":108,"height":227} {"name":"Plato","type":"human","image_quality":{"good_illumination":true}}
wikimedia_death_of_socrates.jpg -1 {"caption":"The Death of Socrates by David","public_domain":"yes","image_url":"https://en.wikipedia.org/wiki/The_Death_of_Socrates#/media/File:David_-_The_Death_of_Socrates.jpg"} 3 1 {"name":"rect","x":347,"y":114,"width":91,"height":209} {"name":"Socrates","type":"human","image_quality":{"frontal":true,"good_illumination":true}}
wikimedia_death_of_socrates.jpg -1 {"caption":"The Death of Socrates by David","public_domain":"yes","image_url":"https://en.wikipedia.org/wiki/The_Death_of_Socrates#/media/File:David_-_The_Death_of_Socrates.jpg"} 3 2 {"name":"ellipse","cx":316,"cy":180,"rx":17,"ry":12} {"name":"Hemlock","type":"cup"}
Use DictReader which reads the csv as a dictionary!
import csv
import json
with open('graph.csv') as file:
#Read csv as dictreader
reader=csv.DictReader(file)
count=0
#Iterate through rows
for idx,row in enumerate(reader):
#Load the string as a dictionary
region_shape_attributes = json.loads(row['region_shape_attributes'])
print(region_shape_attributes['name'])
`import csv
import ast
with open('file.csv') as file:
#Read csv as dictreader
reader=csv.DictReader(file)
count=0
#Iterate through rows
for idx,row in enumerate(reader):
#print(row)
row_5=row['region_shape_attributes']
y=ast.literal_eval(row_5)
print(y.get("name"))`
this code also work to me

Reading CSV file Into a Dictionary (KeyError: '","')

I want to read a CSV file into a Dictionary, here is my code:
import csv
import sys
with open('/Users/m/based_final.csv',mode='r') as my_input_file:
csv_reader=csv.DictReader(my_input_file)
line_count=0
for row in csv_reader:
if line_count == 0:
print(str.format('Column names are {",".join(row)}'))
line_count += 1
print(str.format('\t{row["Indication"]} {row["Name"]} {row["Drug id"]} {row["Synonyms"]} '))
line_count += 1
print('Processed {line_count} lines.')
But I get this error:
KeyError: '","'.
Here is how data looks like:
Indication Name Drug id Synonyms
for_treatment bivalirudin ['db00006', 'btd00076'] ['bivalirudina', 'bivalirudinum', 'hirulog']
for_alteplase a name ['db00009', 'btd00050', 'biod00050'] ['alteplase (genetical recombination)', 'alteplase, recombinant']
Any idea how this can be fixed?
Thx
Here is your code fixed with comments telling what I changed, however, you need to change the first line in the file to work with this code. The CVS file's first line needs to be formatted. each key that you want in your dictionary needs to have a coma after it. There can not be any space surrounding the coma either.
import csv
import sys
with open('/Users/m/based_final.csv',mode='r') as my_input_file:
csv_reader=csv.DictReader(my_input_file)
line_count=0
for row in csv_reader:
if line_count == 0:
print(str.format(f'Column names are {", ".join(row)}')) # an f added before the single quotes and a space after the coma for readibility
line_count += 1
print(str.format(f'\t{row["Indication"]} {row["Name"]} {row["Drug id"]} {row["Synonyms"]} ')) # same thing with the f
line_count += 1
print(f'Processed {line_count} lines.') # ditto

Export multiple print statements to csv using python 3.5

I have csv file which have two columns and i want to export it into the format i want. SO far i am able to print it into shell, i am struck with exporting that into csv file. Below is the code so far I have tried.
import csv
with open('file.csv','r') as csv_file:
csv_reader = csv.reader(csv_file)
next(csv_reader)
sku = 0
with open('newfile.csv','w') as new_file:
csv_writer = csv.writer(new_file)
for i in csv_reader:
sku = sku + 1
csv_writer.writerow(i)
print("message:: {")
print(" Message ID :",sku)
print(" sku :", i)
Any help or pointer would be helpfull. Thank you

Resources