I want to read a CSV file in Python, and then print out every row apart from the first row.
I know how to print out all the rows:
with open('myfile.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
print row
And the only way I can think of not printing out the first row is:
with open('myfile.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for i, row in enumerate(reader):
if i != 0:
print row
But this doesn't seem very elegant. Any other solutions?
csv reader objects are iterators, which means you can skip single entries using next():
with open('myfile.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader) # just ignore the result
for row in reader:
print row
Related
I am trying to write out only TWO SPECIFIC ROWS from this csv file. Here is my code
with open('StudentsMajorsList.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
with open('ComputerScienceStudents.csv', 'w') as new_file:
fieldnames = ['StudentID','Major','FirstName','LastName','DisciplinaryAction']
csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter=',')
csv_writer.writeheader()
for row in csv_reader:
csv_writer.writerow(row)
in this area
for row in csv_reader:
csv_writer.writerow(row)
I only want to pull TWO ROWS and not all of them. Please help!
I think you're looking for something like this:
for row in csv_reader:
if some_condition:
csv_writer.writerow(row)
You haven't told us what some_condition is, so we can only guess.
EDIT:
I need to store/add/append additional information in a specific column in a csv file with out using csv.DictReader.
If I wanted to skip a row in a column and it was empty, what do I need to do for it?
For example:
Sample csv file:
$ cat file.csv
"A","B","C","D","E"
"a1","b1","c1","d1","e1"
"a2","b2","c2","d2","e2"
"a2","b2","c2",,"e2"
Code:
sample = ['dx;dy']
with(openfile.csv, "r") as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
headers = next(reader)
for row in reader:
#sample.append(to the column D)
The Output should look like this:
$ cat file.csv
"A","B","C","D","E"
"a1","b1","c1","d1;dx;dy","e1"
"a2","b2","c2","d2;dx;dy","e2"
"a2","b2","c2",,"e2"
Since you know the header of the column you want to append to, you can find its index in the headers row, and then modify that element of each row.
append_to_column = 'D'
separator = ';'
sample = ['dx;dy']
with open('file.csv', "r") as csvfile, open("outfile.csv", "w") as outfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
headers = next(reader)
writer = csv.writer(outfile, delimiter=',', quotechar='"')
col_index = headers.index(append_to_column)
for row in reader:
value = row[col_index]
new_value = value + separator + sample[0]
row[col_index] = new_value
writer.writerow(row)
Which gives:
A,B,C,D,E
a1,b1,c1,d1;dx;dy,e1
a2,b2,c2,d2;dx;dy,e2
Note that this file doesn't have quotes because they aren't required, since the fields don't contain any commas. If you want to force the csv.writer to write quotes, you can add the quoting=csv.QUOTE_ALL argument to the csv.writer() call, like so: writer = csv.writer(outfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
Then, you'll get:
"A","B","C","D","E"
"a1","b1","c1","d1;dx;dy","e1"
"a2","b2","c2","d2;dx;dy","e2"
I want to append separate rows from a csv file. I want to append the first, third, fourth, and fifth element. Here is what I have so far but it doesn't seem to work — any ideas? This code currently saves into two separate lists; I want the rows to be appended into the same list.
import csv
game_file = open(gamefile + ".csv")
game_file = []
score_file.append([row[0]])
score_file.append([row[2:5]])
with open(studentsclass + ".csv", "w") as f:
writer = csv.writer(f)
writer.writerows(score_file)
f.close()
game_file.close()
Using csv could help:
import csv
game_file = []
included_cols = [0, 2, 3, 4, 5]
with open(filename, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=';', quotechar='"')
for row in reader:
game_file.append(list(row[i] for i in included_cols))
You can use enumerate on your reader object and check if the index is in your desired list (which I have converted it to set that has O(1) for check membership ):
import csv
temp=0
rows=[]
with open('eggs.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for i,row in enumerate(spamreader):
if i in {0,2,3,4,5}:
temp+=1
rows.append(row)
if temp==5:
print rows
break
I open a file and read it with csv.DictReader. I iterate over it twice, but the second time nothing is printed. Why is this, and how can I make it work?
with open('MySpreadsheet.csv', 'rU') as wb:
reader = csv.DictReader(wb, dialect=csv.excel)
for row in reader:
print row
for row in reader:
print 'XXXXX'
# XXXXX is not printed
You read the entire file the first time you iterated, so there is nothing left to read the second time. Since you don't appear to be using the csv data the second time, it would be simpler to count the number of rows and just iterate over that range the second time.
import csv
from itertools import count
with open('MySpreadsheet.csv', 'rU') as f:
reader = csv.DictReader(f, dialect=csv.excel)
row_count = count(1)
for row in reader:
next(count)
print(row)
for i in range(row_count):
print('Stack Overflow')
If you need to iterate over the raw csv data again, it's simple to open the file again. Most likely, you should be iterating over some data you stored the first time, rather than reading the file again.
with open('MySpreadsheet.csv', 'rU') as f:
reader = csv.DictReader(f, dialect=csv.excel)
for row in reader:
print(row)
with open('MySpreadsheet.csv', 'rU') as f:
reader = csv.DictReader(f, dialect=csv.excel)
for row in reader:
print('Stack Overflow')
If you don't want to open the file again, you can seek to the beginning, skip the header, and iterate again.
with open('MySpreadsheet.csv', 'rU') as f:
reader = csv.DictReader(f, dialect=csv.excel)
for row in reader:
print(row)
f.seek(0)
next(reader)
for row in reader:
print('Stack Overflow')
You can create a list of dictionaries, each dictionary representing a row in your file, and then count the length of the list, or use list indexing to print each dictionary item.
Something like:
with open('YourCsv.csv') as csvfile:
reader = csv.DictReader(csvfile)
rowslist = list(reader)
for i in range(len(rowslist))
print(rowslist[i])
add a wb.seek(0) (goes back to the start of the file) and next(reader) (skips the header row) before your second loop.
You can try store the dict in list and output
input_csv = []
with open('YourCsv.csv', 'r', encoding='UTF-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
input_csv.append(row)
for row in input_csv:
print(row)
for row in input_csv:
print(row)
with open("test.txt", "r") as test:
reader = csv.reader(test, delimiter="\t")
writer = csv.writer(table, delimiter="\t")
for row in reader:
for field in row:
if field not in keywords:
writer.writerow(row)
break
It seems that this code writes out every row multiple times. I guess that it looks up every single field in each column. How can I specify a single column?
So this is the code I am using right now and it seems that it misses a few rows where the keyword is not present in any column.
table = open("table.txt", "w")
with open("test.txt", "r") as test:
reader = csv.reader(test, delimiter="\t")
writer = csv.writer(table, delimiter="\t")
for row in reader:
if all(field not in keywords for field in row):
writer.writerow(row)
You can use zip to get your columns then.You can use a generator expression within all function for checking that all the elements mett the condition :
with open("test.txt", "r") as Spenn,open("test.txt", "r") as table:
reader = zip(*csv.reader(Spenn, delimiter="\t"))
writer = csv.writer(table, delimiter="\t")
for row in reader:
if all(field not in keywords for field in row):
writer.writerow(row)
But if you just want to write the rows that meet the condition you can use the following code :
with open("test.txt", "r") as Spenn,open("test.txt", "r") as table:
reader = csv.reader(Spenn, delimiter="\t")
writer = csv.writer(table, delimiter="\t")
for row in reader:
if all(field not in keywords for field in row):
writer.writerow(row)