6
\$\begingroup\$

I am trying to import a .csv file, clean the contents, and make sure all the errors are eliminated.

  • IO error
  • file format error
  • data error

I am using the snippet below. Could someone help me clean it and make it more pythonic?

try:
 fl = request.FILES['csv']
except:
 return HttpResponse("Some IO Error")
try: 
 reader = csv.reader(fl)
except:
 return HttpResponse("File Not Supported")
reader.next() # this makes sure the heading is 
cleaned_csv_content = list()
for row in reader:
 err = 0
 fname, lname, e_mail = row
 try:
 validate_email(email)
 except:
 pass
 err = 1
 if not err:
 cleaned_csv_content.append(fname, lname, e_mail)
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Dec 13, 2013 at 10:18
\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

Firstly, you should raise not return your errors.

Secondly, try: with a bare except: is bad practice (e.g. http://blog.codekills.net/2011/09/29/the-evils-of--except--/); what do you expect could go wrong with each step? Test for specific errors and deal with them appropriately. It is this which has hidden the NameError I mention at the end.

Thirdly, your use of the flag err seems a bit odd; much more Pythonic to use an actual boolean err = False or, better, else::

try:
 validate_email(e_mail) # see comment below
except: # but only the errors you expect - see above 
 pass
else:
 cleaned_csv_content.append((fname, lname, e_mail)) # note additional parentheses

or, even better, refactor validate_email to return True for a valid email and False otherwise, rather than raising errors:

if validate_email(e_mail):
 cleaned_csv_content.append((fname, lname, e_mail))

Finally, note that "email" != "e_mail".

answered Dec 13, 2013 at 10:29
\$\endgroup\$
6
  • \$\begingroup\$ how about the for loops, can that be made more idiomatic? \$\endgroup\$ Commented Dec 13, 2013 at 10:36
  • \$\begingroup\$ No; for row in reader: is fine and the tuple unpacking of each row is very Pythonic \$\endgroup\$ Commented Dec 13, 2013 at 10:38
  • \$\begingroup\$ I am getting this error - Error: 'line contains NULL byte' for row in reader Any corrections? \$\endgroup\$ Commented Dec 13, 2013 at 10:43
  • \$\begingroup\$ Not to the code, as such; you will need to look at your input data and what is ending up in reader, or use another try and handle that specific error appropriately (e.g. pass on that line). \$\endgroup\$ Commented Dec 13, 2013 at 10:51
  • \$\begingroup\$ thanks done, another doubt. I am getting output of - for row in readline: print row as follows ['First Name\tLast Name\tEmail Address'] ['ASdf \tasdf\[email protected]'] The delimiter here is \t, any idea how to fix it? \$\endgroup\$ Commented Dec 13, 2013 at 11:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.