Reading a csv file in Python with the module csv - python

I'm trying to read a csv file in Python using the module csv.
To do that, I use a reader variable :
with open('oneOrganization.csv', 'r', newline='') as csvfile2:
reader2 = csv.DictReader(csvfile2, delimiter=',')
for row in reader2:
if row["role"] == []:
row_dict['Role'] = "User"
else:
row_dict['Role'] = row["role"]
However, running the program, I realize that it does get in the loop at all although the cvs file exists and is indeed called oneOrganization.csv. What could be the reason of that ?
I'm starting in Python, usually this method works.

The problem that prevents your code from accessing the if loop if row['Role']==[] is because you're trying to find empty elements the wrong way.
try this instead:
Method 1:
with open('oneOrganization.csv','r') as file:
reader = csv.DictReader(file,delimiter=',')
my_list = list(reader)
for row in reader:
if row['test1'] in (None,''): # to check if cell is empty
row_dict['Role'] = "User"
else:
row_dict['Role'] = row["role"]
Method 2: provided by #Jean-François Fabre
with open('oneOrganization.csv','r') as file:
reader = csv.DictReader(file,delimiter=',')
for row in reader:
if not row['test1']:
row_dict['Role'] = "User"
else:
row_dict['Role'] = row["role"]
Method 3 : "elegant one liner" - by #Jean-François Fabre
with open('oneOrganization.csv','r') as file:
reader = csv.DictReader(file,delimiter=',')
for row in reader:
row_dict['Role'] = row["role"] or "User"
I tried it on a csv example that can be represented like this: (np.array form)
[['test1' 'test2' 'test3']
[ 1 2 3 ]
[ 11 22 33 ]
[ 222 333]]
and used this code:
import csv
with open('test_csv.csv','r') as file:
reader = csv.DictReader(file,delimiter=',')
for row in reader:
if row['test1'] in (None,''):
print('no')
else:
print(row['test1'])
or with method 2:
import csv
with open('test_csv.csv','r') as file:
reader = csv.DictReader(file,delimiter=',')
for row in reader:
if not row['test1']:
print('no')
else:
print(row['test1'])
or method 3
import csv
with open('test_csv.csv','r') as file:
reader = csv.DictReader(file,delimiter=',')
for row in reader:
print(row['test1'] or 'no')
output:
1
11
no
you can refer to this topic for more informations about how to check if a "cell" is empty in a csv file.

Related

When i try to remove a row from the csv the file size is multipliying

I want to create a program which generates numbers from 0 to 100000 and stores it in a file then, remove the numbers i give as input
I have done the code for generating the numbers and storing them in a csv file
import csv
nums = list(range(0,100000))
with open('codes.csv', 'w') as f:
writer = csv.writer(f)
for val in nums:
writer.writerow([val])
and i tried to delete the row i wanted with this
import csv
import os
lines = list()
while True:
members= input("Please enter a number to be deleted: ")
with open('codes.csv', 'r') as readFile:
reader = csv.reader(readFile)
for row in reader:
lines.append(row)
for field in row:
if field == members:
lines.remove(row)
os.remove('codes.csv')
with open('codes.csv', 'a+') as writeFile:
writer = csv.writer(writeFile)
writer.writerows(lines)
but the file size is multiplying each time i remove a number, please help
Add check before appending to your list, something like this should work:
with open('codes.csv', 'r') as readFile:
reader = csv.reader(readFile)
for row in reader:
if all(field != members for field in row):
lines.append(row)
Ps: don't forget to clear lines by adding lines = [] at the beginning of the while loop (I assume you know what you're doing).
There a two problems:
The lines list is never cleared. Whenever a number is entered, everything is written again to lines.
When writing, the file is opened with the a+ attributes, which means "append and update" file.
Try to recreate the list within the outer while loop and override the file contents by opening the file with attribute w, like this:
import csv
import os
while True:
members= input("Please enter a number to be deleted: ")
lines = list()
with open('codes.csv', 'r') as readFile:
reader = csv.reader(readFile)
for row in reader:
lines.append(row)
for field in row:
if field == members:
lines.remove(row)
os.remove('codes.csv')
with open('codes.csv', 'w') as writeFile:
writer = csv.writer(writeFile)
writer.writerows(lines)

Editing CSV row with Python

I'm trying to edit a single row in a csv file. I've got a CSV file that looks like the bellow:
TYPE|FOOD TYPE|FEED TIME|WASH TIME
LION|MEAT|4H|1D
FOX|MEAT|5H|3D
HEN|SEED|6H|6D
FISH|PLANTS|7H|99D
I want to edit the row based on its TYPE. If the user wants to edit the FOX row they only need to type FOX when prompted. The issue I'm facing is that the I can't edit the file for some reason.
My code is bellow, I open the existing db, find the row in question, change it, then write it, along with the other rows, into a temp file that I can overwrite the original with.
def edit_animal_entry(type):
with open(animal_csv, 'r') as file_read:
reader = csv.reader(file_read, delimiter="|")
with open(temp, 'w') as file_write:
writer = csv.writer(file_write)
for row in reader:
print(f"{' | '.join(row)}")
if row[0] == type:
animal_type, animal_food, animal_feed, animal_wash = animal_inputs()
writer.writerow([animal_type, animal_food, (animal_feed+"H"), (animal_wash+"D")])
else:
writer.writerow(row)
shutil.move(temp, animal_csv)
You've 'closed' the read file by stopping the with block before reading anything out of it. Therefore you aren't looping over your input file. A solution would be to open the input and the output file in the same with statement:
def edit_animal_entry(type):
with open(animal_csv, 'r') as file_read, open(temp, 'w') as file_write:
reader = csv.reader(file_read, delimiter="|")
writer = csv.writer(file_write)
for row in reader:
print(f"{' | '.join(row)}")
if row[0] == type:
animal_type, animal_food, animal_feed, animal_wash = animal_inputs()
writer.writerow([animal_type, animal_food, (animal_feed+"H"), (animal_wash+"D")])
else:
writer.writerow(row)
shutil.move(temp, animal_csv)

Python CSV Row Loop

I am very new to Python programming and decided on a small project to learn the language.
Basically I am trying to:
Read the first cell of a CSV file.
Ask if that cell value is "liked".
If liked, write to the column next to the cell on 1., "1".
Else, write "0".
Repeat on next row until end of list.
My code right now:
import csv
reader = csv.reader(open("mylist.csv"), delimiter=',')
data = []
for row in reader:
data.append(row)
ask = (data[0][0])
ans = input("Do you like {}? ".format(ask))
if ans == ("y"):
f = open('mylist.csv', 'r')
reader = csv.reader(f)
data = list(reader)
f.close()
data[0][1] = '1'
my_new_list = open('mylist.csv', 'w', newline='')
csv_writer = csv.writer(my_new_list)
csv_writer.writerows(data)
my_new_list.close()
else:
f = open('mylist.csv', 'r')
reader = csv.reader(f)
data = list(reader)
f.close()
data[0][1] = '0'
my_new_list = open('mylist.csv', 'w', newline='')
csv_writer = csv.writer(my_new_list)
csv_writer.writerows(data)
my_new_list.close()
So basically, I am stuck trying to get the content of the next row.
FYI, I am looking to implement machine learning to this process.
First learning how to do this in a basic manner.
Any help is welcome.
Thank you!
You shouldn't read from and write to the same file/list/dict at the same time. If you do, references to data may change. You can start with something like this for your task. However, note that as the file grows you code becomes slower.
import csv
reader = csv.reader(open("test.csv", 'r'), delimiter=',')
content = []
for row in reader:
item = row[0]
ans = raw_input("Do you like {}? ".format(item))
if ans == 'y':
content.append([item, 1])
else:
content.append([item, 0])
writer = csv.writer(open('test.csv', 'w'))
writer.writerows(content)
In my last work with csv I opened the file so:
import csv
with open(name) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
If you want the resultant csv file to contain all of the data from the input file but with the question results added in, you could use something like this.
It will insert you answer (0 or 1) after the first item in each record.
import csv
reader = csv.reader(open("mylist.csv", 'r'), delimiter=',')
data = []
for row in reader:
data.append(row)
for row in data:
ans = raw_input("Do you like {}? ".format(row[0]))
if ans == 'y':
row[1:1] = "1"
else:
row[1:1] = "0"
writer = csv.writer(open('myresult.csv', 'w'))
writer.writerows(data)

(Simple Python) CSV input to usernames

I have a CSV file names.csv
First_name, Last_name
Mike, Hughes
James, Tango
, Stoke
Jack,
....etc
What I want is to be able to take the first letter of the First_name and the full Last_name and output it on screen as usernames but not include the people with First_name and Last_name property's empty. I'm completely stuck any help would be greatly appreciated
import csv
ifile = open('names.csv', "rb")
reader = csv.reader(ifile)
rownum = 0
for row in reader:
if rownum == 0:
header = row
else:
colnum = 0
for col in row:
print '%-8s: %s' % (header[colnum], col)
colnum += 1
rownum += 1
ifile.close()
Attempt #2
import csv
dataFile = open('names.csv','rb')
reader = csv.reader(dataFile)
next(reader, None)
for row in reader:
if (row in reader )
print (row[0])
I haven't saved many attempts because none of them have worked :S
import csv
dataFile = open('names.csv','rb')
reader = csv.reader(dataFile, delimiter=',', quoting=csv.QUOTE_NONE)
for row in reader:
if not row[0] or not row[1]:
continue
print (row[0][0] + row[1]).lower()
Or
import csv
dataFile = open('names.csv','rb')
reader = csv.reader(dataFile, delimiter=',', quoting=csv.QUOTE_NONE)
[(row[0][0] + row[1]).lower() for row in reader if
row[0] and row[1]]
Once you get the text from the .csv you can use the split() function to break up the text by the new lines. Your sample text is a little inconsistent, but if I understand you question correctly you can say
import csv
dataFile = open('names.csv','rb')
reader = csv.reader(dataFile)
reader = reader.split('\n')
for x in reader
print(reader[x])
Or if you want to break it up by commas just replace the '\n' with ','
Maybe like this
from csv import DictReader
with open('names.csv') as f:
dw = DictReader(f, skipinitialspace=True)
fullnames = filter(lambda n: n['First_name'] and n['Last_name'], dw)
for f in fullnames:
print('{}{}'.format(f['First_name'][0], f['Last_name']))
You have headings in your csv so use a DictReader and just filter out those whose with empty first or last names and display the remaining names.

How to read a row one by one using for loop in python

I want to use the For Loop and print a row one by one whatever i required.
here is my code:
import csv
with open("details.csv") as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
if['age'] == '21':
print(row['Name'], row['age'], row['DOB'])
else:
continue
Here i want run the for loop until 6 times and also i want specific data of who ever age is '21', that person details only i want print, if it is other than '21' then skip the row. but my code is doesn't perform exactly like i want.
can anyone help me..?
Thank you :)
my csv is:
Name age dob
Arun 21 01/08/93
Banni 20 05/11/94
charan 23 23/03/92
nani 21 04/05/93
Simple error: Try this
import csv
with open("details.csv") as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
if row['age'].strip() == '21': #error in this line
print(row['Name'], row['age'], row['DOB'])
else:
continue
I tried your code and your file and got an error.
Then I replaced tabs in your csv file with commas and capitalized "dob" to "DOB":
Name,age,DOB
Arun,21,01/08/93
Banni,20,05/11/94
charan,23,23/03/92
nani,21,04/05/93
Then the output was correct:
>>>
Arun 21 01/08/93
nani 21 04/05/93
>>>
Of course I changed Line 5 to if row['age'] == '21':
By default DictReader uses comma , as a seperator between two fields but your csv file uses tabs.
If you don't want to change your csv file the solution is to change the creation of your DictReader to
reader = csv.DictReader(f, delimiter='\t')
Next change the line if['age'] == '21': to if row['age'] == '21':.
Finally row['DOB'] should be row['dob'] as the field names are case sensitive.
Try This if you dont have specified the header in csv file
import csv
import sys
f = open("File_Name.csv", 'rt') #open the file reading
print "Name\tAge\tDOB"
try:
reader = csv.reader(f)
for row in reader:
if row[1] == '21': #Check the condition
print "%s\t%s\t%s" %(row[0],row[1],row[2]) #print the columns
else:
continue
finally:
f.close() #close the file at the end
and if the file has header as first line(Name,Age,DOB) then use following
import csv #import csv package
with open("details.csv") as csvFile: #open the file
reader = csv.DictReader(csvFile)
for row in reader: #iterate the file
if row['age'].strip() == '21': #check the condition
print(row['Name'], row['age'], row['DOB'])
else: #skip if not satisfies the condition
continue

Categories

Resources