Ad
How Could I Skip A Row If A Csv Field Is Empty
I have a csv file where I have some names and the date from when students have passed the exam. if they did not pass the exam the date will be empty. how could I filter out the empty field so I only have a list with people that have passed the test?
Here is the code that I have. the row[0] is the names of the student and row[12] is the date they have passed it.
with open("MYCSV_FILE", 'r') as read_obj:
csv_dict_reader = csv.reader(read_obj, delimiter=';')
for row in csv_dict_reader:
if row[12]:
with open('fed_results\Behaalde_studenten.csv', 'a') as f_object:
# Pass the CSV file object to the writer() function
writer_object = csv.writer(f_object)
# Result - a writer object
# Pass the data in the list as an argument into the writerow() function
writer_object.writerow(row[0])
# Close the file object
f_object.close()
Ad
Answer
row[12]
can be checked for an empty result like this:
if row[12]:
# do something #
So your code can check for non-empty rows by:
with open("MYCSV_FILE", 'r') as read_obj:
csv_dict_reader = csv.reader(read_obj, delimiter=';')
for row in csv_dict_reader:
if row[12]:
print(row[0], row[12])
Or you can consider an if/else statement:
with open("MYCSV_FILE", 'r') as read_obj:
csv_dict_reader = csv.reader(read_obj, delimiter=';')
for row in csv_dict_reader:
if row[12]:
print(row[0], row[12])
else:
# what to do if it isn't there #
This all assumes that each row is delimited by a semicolon even when empty values are given, such that:
a;b;c;d;e;f;g;h;i;j;k;l
1;2;3;4;5;6;7;8;9;10;11;
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad