SHARE
TWEET

Untitled

a guest Jun 25th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import csv, cStringIO
  2.  
  3. data = [['NULL/None value',None],
  4.         ['empty string','']]
  5.  
  6. f = cStringIO.StringIO()
  7. csv.writer(f).writerows(data)
  8.  
  9. f = cStringIO.StringIO(f.getvalue())
  10. data2 = [e for e in csv.reader(f)]
  11.  
  12. print "input : ", data
  13. print "output: ", data2
  14.      
  15. data = [d if d!=None else 'None' for d in data]
  16. data2 = [d if d!='None' else None for d in data2]
  17.      
  18. class NONE(object):
  19.     def __repr__(self): # method csv.writer class uses to write values
  20.         return 'NONE'   # unique string value to represent None
  21.     def __len__(self):  # method called to determine length and truthiness
  22.         return 0        # (optional)
  23.  
  24. NONE = NONE()  # singleton instance of the class
  25.  
  26. import csv
  27. import cStringIO
  28.  
  29. data = [['None value', None], ['NONE value', NONE], ['empty string', '']]
  30. f = cStringIO.StringIO()
  31. csv.writer(f).writerows(data)
  32. f = cStringIO.StringIO(f.getvalue())
  33. print " input:", data
  34. print "output:", [e for e in csv.reader(f)]
  35.      
  36. import csv
  37.  
  38. class csvProxyBase(object): _NONE = '<None>'  # unique value representing None
  39.  
  40. class csvWriter(csvProxyBase):
  41.     def __init__(self, csvfile, *args, **kwrags):
  42.         self.writer = csv.writer(csvfile, *args, **kwrags)
  43.     def writerow(self, row):
  44.         self.writer.writerow([self._NONE if val is None else val for val in row])
  45.     def writerows(self, rows):
  46.         map(self.writerow, rows)
  47.  
  48. class csvReader(csvProxyBase):
  49.     def __init__(self, csvfile, *args, **kwrags):
  50.         self.reader = csv.reader(csvfile, *args, **kwrags)
  51.     def __iter__(self):
  52.         return self
  53.     def next(self):
  54.         return [None if val == self._NONE else val for val in self.reader.next()]
  55.  
  56. if __name__ == '__main__':
  57.     import cStringIO as StringIO
  58.     data = [['None value', None], ['empty string', '']]
  59.     f = StringIO.StringIO()
  60.     csvWriter(f).writerows(data)
  61.     f = StringIO.StringIO(f.getvalue())
  62.     print " input:", data
  63.     print "output:", [e for e in csvReader(f)]
  64.      
  65. >>> data = [['NULL/None value',None],['empty string','']]
  66. >>> i = cStringIO.StringIO()
  67. >>> csv.writer(i).writerows(map(str,row) for row in data)
  68. >>> print i.getvalue()
  69. NULL/None value,None
  70. empty string,
  71.      
  72. >>> import json
  73. >>> json.dumps(['foo', '', None, 666])
  74. '["foo", "", null, 666]'
  75. >>>
  76.      
  77. import csv, cStringIO
  78. NULL = '<NULL>'  # something unlikely to ever appear as a regular value in your csv files
  79.  
  80. class MyCsvWriter(object):
  81.     def __init__(self, *args, **kwrds):
  82.         self.csv_writer = csv.writer(*args, **kwrds)
  83.  
  84.     def __getattr__(self, name):
  85.         return getattr(self.csv_writer, name)
  86.  
  87.     def writerow(self, row):
  88.         self.csv_writer.writerow([item if item is not None else NULL
  89.                                       for item in row])
  90.     def writerows(self, rows):
  91.         for row in rows:
  92.             self.writerow(row)
  93.  
  94. class MyCsvReader(object):
  95.     def __init__(self, *args, **kwrds):
  96.         self.csv_reader = csv.reader(*args, **kwrds)
  97.  
  98.     def __getattr__(self, name):
  99.         return getattr(self.csv_reader, name)
  100.  
  101.     def __iter__(self):
  102.         rows = iter(self.csv_reader)
  103.         for row in rows:
  104.             yield [item if item != NULL else None for item in row]
  105.  
  106. data = [['NULL/None value', None],
  107.         ['empty string', '']]
  108.  
  109. f = cStringIO.StringIO()
  110. MyCsvWriter(f).writerows(data)  # instead of csv.writer(f).writerows(data)
  111.  
  112. f = cStringIO.StringIO(f.getvalue())
  113. data2 = [e for e in MyCsvReader(f)]  # instead of [e for e in csv.reader(f)]
  114.  
  115. print "input : ", data
  116. print "ouput : ", data2
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top