Permalink
Browse files

Arguments update.

  • Loading branch information...
1 parent 8fe6da4 commit 47b04e567c38111f0a183066f8656d8bfe2f1315 @arturgrigor committed Apr 2, 2012
Showing with 1 addition and 1 deletion.
  1. +1 −1 csv2plist.py
View
2 csv2plist.py 100644 → 100755
@@ -1 +1 @@
-import os, sys, plistlibimport csv, codecs, cStringIOclass UTF8Recoder: """ Iterator that reads an encoded stream and reencodes the input to UTF-8 """ def __init__(self, f, encoding): self.reader = codecs.getreader(encoding)(f) def __iter__(self): return self def next(self): return self.reader.next().encode("utf-8")class UnicodeReader: """ A CSV reader which will iterate over lines in the CSV file "f", which is encoded in the given encoding. """ def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): f = UTF8Recoder(f, encoding) self.reader = csv.reader(f, dialect=dialect, **kwds) def next(self): row = self.reader.next() return [unicode(s, "utf-8") for s in row] def __iter__(self): return selfinfile = ''outfile = ''args = sys.argv[1:]if(len(args) == 1): infile = args[0] outfile = infile.replace(".csv", ".plist")if(len(args) == 2): infile = args[0] outfile = args[1]i = 0headers = []plist = []csvr = UnicodeReader(open(infile, 'rb'), delimiter=',')for row in csvr: if (i == 0): headers = row if (i > 0): dictionary = dict(zip(headers, row)) plist.append(dictionary) i+=1plistlib.writePlist(plist, outfile)
+import os, sys, plistlibimport csv, codecs, cStringIOclass UTF8Recoder: """ Iterator that reads an encoded stream and reencodes the input to UTF-8 """ def __init__(self, f, encoding): self.reader = codecs.getreader(encoding)(f) def __iter__(self): return self def next(self): return self.reader.next().encode("utf-8")class UnicodeReader: """ A CSV reader which will iterate over lines in the CSV file "f", which is encoded in the given encoding. """ def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): f = UTF8Recoder(f, encoding) self.reader = csv.reader(f, dialect=dialect, **kwds) def next(self): row = self.reader.next() return [unicode(s, "utf-8") for s in row] def __iter__(self): return selfinfile = ''outfile = ''csvDelimiter = ','args = sys.argv[1:]argsLength = len(args)if (argsLength > 0): infile = args[0] if (argsLength > 1): outfile = args[1] if (argsLength > 2): csvDelimiter = args[2]if (outfile == ''): infile.replace(".csv", ".plist")i = 0headers = []plist = []csvr = UnicodeReader(open(infile, 'rb'), delimiter=csvDelimiter, skipinitialspace=True)for row in csvr: if (i == 0): headers = row if (i > 0): dictionary = dict(zip(headers, row)) plist.append(dictionary) i+=1plistlib.writePlist(plist, outfile)

0 comments on commit 47b04e5

Please sign in to comment.