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.
Related
I need to read in a pipe delimited file change some fields and output. It reads in fine and I am able to change the columns. The writer part writes the header but writerows doesn't write anything. How can I output the updated contents?
csv.register_dialect('pipe',delimiter='|', quoting=csv,QUOTE_NONE)
with open('test.txt') as csvfile
cfile=csv.DictReader(cfile,dialect='pipe')
fieldnames=cfile.fieldnames
for row in cfile:
row['address']=scrubrow(row['address']
with open('c.txt','w') as outfile:
writer=csv.DictWriter(outfile,fieldnames=fieldnames,dialect='pipe')
writer.writeheader()
writer.writerows(cfile)
cfile is an empty iterator. And you discarded all but the last row, so doing row['address']=scrubrow(row['address'] didn't actually do anything.
The simple way to do this is to create a list and use that list:
csv.register_dialect('pipe',delimiter='|', quoting=csv,QUOTE_NONE)
with open('test.txt') as csvfile
reader = csv.DictReader(cfile, dialect='pipe')
fieldnames = cfile.fieldnames
rows = []
for row in reader:
rows.append(scrubrow(row['address']))
with open('c.txt','w') as outfile:
writer = csv.DictWriter(outfile, fieldnames=fieldnames, dialect='pipe')
writer.writeheader()
writer.writerows(rows)
But this will be inefficient because it will require O(N) space. Instead, keeping only a single row in memory at a time, you could do:
csv.register_dialect('pipe',delimiter='|', quoting=csv,QUOTE_NONE)
with open('test.txt') as csvfile, open('c.txt','w') as outfile:
reader = csv.DictReader(cfile,dialect='pipe')
fieldnames = reader.fieldnames
writer = csv.DictWriter(outfile,fieldnames=fieldnames,dialect='pipe')
writer.writeheader()
for row in reader:
writer.writerow(scrubrow(row['address']))
This is how i made my CSV file:
with open('Mail_Txt.csv', 'w', encoding='utf-8', newline='') as csvfile:
fieldnames= ['Sender', 'Subject', 'Snippet']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames , delimiter=',')
for val in final_list:
writer.writerow(val)
I try many methods to print the Sender column of the CSV file. But I fail all the time. so, help me to print the first or 'Sender' column of the CSV file
Reading from a csv file is symetric from writing. The main difference is that as you have skipped the header line, you will use a simple reader and get sequences instead of mappings:
with open('Mail_Txt.csv', 'r', encoding='utf-8', newline='') as csvfile:
reader= csv.reader(csvfile, delimiter=',')
for val in reader:
print(val[0])
you can use use the DictReader function to accomplish this.
with open('yourFile') as f:
data = [row["Sender"] for row in DictReader(f)]
print(data)
I have a database of tweets in csv format which looks like this - screen of csv database - and I need to perform the following task with this file using Python code:
Search for certain words/phrases in tweets (text of a tweet is in the column C) and if the tweet has this word/phrase I'm looking for, I need to write the whole row with this tweet to a new csv file
and (if possible) to delete this tweet from the old csv file or create a new one without it.
I hope I made it clear.
You can use the csv library to read the file and do your search on each row one at a time.
import csv
with open('out.csv', 'w', newline='') as output_file:
writer = csv.writer(output_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
with open('test.csv') as csv_file:
csv_read = csv.reader(csv_file, delimiter=',')
for row in csv_read:
if "a" in row[2]:
writer.writerow(row)
Here is a link to the python docs: https://docs.python.org/3/library/csv.html. Hope this helps.
EDIT: If you want more than one search term, use any() on a list comprehension.
import csv
with open('out.csv', 'w', newline='') as output_file:
writer = csv.writer(output_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
with open('input.csv') as csv_file:
csv_read = csv.reader(csv_file, delimiter=',')
for row in csv_read:
search_terms = ["term1", "term2"]
if any([term in row[2] for term in search_terms]):
writer.writerow(row)
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)
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