By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

Appending N/A to blank strings

P: 22
I have a list of numbers imported from excel, an example line looks like this...

70609,86,91,,66,66,

I would like a N/A to be inserted in between the two commas and after the last one.
This is the code I have so far, but I haven't gotten it to work.

Expand|Select|Wrap|Line Numbers
  1. f = open("C:\users\cory\desktop\code\Verification.csv")
  2. dd = {}
  3. keys = f.readline().strip().split(',')
  4.  
  5. for key in keys:
  6.     dd.setdefault(key, [])
  7.  
  8. for line in f:
  9.     elements = line.strip().split(',')
  10.     for i, item in enumerate(elements):
  11.         if element == "":
  12.             dd[keys[i]].append("N/A")            
  13.         else:
  14.             dd[keys[i]].append(int(item))
  15. f.close()
  16.  
  17. for key in keys:
  18.     print "%s: %s" % (key, dd[key])
  19.  
Jul 6 '09 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You are close. Instead of
Expand|Select|Wrap|Line Numbers
  1.         if element == "":
use this:
Expand|Select|Wrap|Line Numbers
  1.         if item == "":
OR
Expand|Select|Wrap|Line Numbers
  1.         if not item:
Jul 6 '09 #2

P: 22
Ah. So close! Thanks!
Jul 7 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.