Advertisement
Lucun_Ji

csv-fixer.py

May 24th, 2021
1,472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. import zipfile
  2. import sys
  3. import io
  4. import os
  5.  
  6. def fix(filename):
  7.     correctlines = []
  8.     with open(filename, 'r') as fields:
  9.         lines = list(fields);
  10.         for line in lines:
  11.             parts = line.split(',')
  12.             if len(parts) > 4:
  13.                 parts = parts[:3] + ['"' +
  14.                     ','.join(parts[3:]).replace('\n', '').replace('"', '""') +
  15.                     '"\n']
  16.             correctlines += ','.join(parts)
  17.     with open(filename, 'w') as fields:
  18.         fields.writelines(correctlines)
  19.  
  20.  
  21. filelist = [
  22.     'fields.csv',
  23.     'methods.csv',
  24. ]
  25. with zipfile.ZipFile(sys.argv[1], 'r') as archive:
  26.     archive.extractall()
  27. for filename in filelist:
  28.     fix(filename)
  29. with zipfile.ZipFile(sys.argv[1].replace('.zip', '-fixed.zip'), 'w') as fixed:
  30.     for filename in filelist:
  31.         fixed.write(filename)
  32.         os.remove(filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement