parsing and writing complete for csv

This commit is contained in:
megazig 2009-06-30 17:40:43 -05:00
parent 675bf6f947
commit 8fa3891f00

View File

@ -4,41 +4,108 @@ import sys, re, struct
from Struct import Struct
hardstring = '\xfe\xff\x00\x22\x63\xa5\x7d\x9a\x30\x59\x30\x8b\x98\x06\x75\x6a'
hardstring += '\x30\x6b\x00\x0d\x00\x0a\x24\x60\x30\xdc\x30\xbf\x30\xf3\x30\x68'
def nullterm(str_plus):
z = str_plus.find('\x00\x22')
z = str_plus.find('\x00\x09')
if z != -1:
return str_plus[:z]
else:
return str_plus
def denullterm(str_plus):
z = str_plus.find('\x0a\x0a')
if z != -1:
return str_plus[:z]
else:
return str_plus
class CSV(object):
def __init__(self, data):
def __init__(self, data=None, outfile=None, debug=False):
self.data = []
if data != None:
self.Unpack(data)
self.Unpack(data, outfile, debug)
def Unpack(self, data):
pos = 0x4a
def Unpack(self, data, outfile=None, debug=False):
if outfile != None:
file = open(outfile, 'wb')
pos = 0
version = Struct.uint16(data[pos:pos+2], endian='>')
pos += 2
if debug == True:
print "Version: %04x" % version
print
self.string_list = []
while pos < len(data):
string = nullterm(data[pos:])
string = unicode(string, 'utf_16_be')
print "String: %s" % string
pos += len(string) * 2 + 6
if debug == True:
print "String: %s" % string
pos += len(string) * 2 + 2
self.string_list.append(string)
file.write(string.encode('utf-8'))
file.write('\n')
if pos < len(data):
file.write('\n')
file.close()
def write(self, data, output_file):
file = open(output_file, 'wb')
if file:
file.write('\xfe\xff')
pos = 0
while pos < len(data):
temp = denullterm(data[pos:])
pos += len(temp) + 2
string = unicode(temp, 'utf-8')
file.write(string.encode('utf_16_be'))
if pos < len(data):
file.write('\x00\x09')
else:
file.write('\x00\x0a')
file.close()
else:
print "Could not open file for writing"
sys.exit(1)
def main():
if len(sys.argv) == 1:
print 'Usage: python csv.py <filename>'
print 'Usage: python -r csv.py <filename.csv> <filename.hog>'
print ' == OR == '
print 'Usage: python -w csv.py <filename.hog> <filename.csv>'
sys.exit(1)
f = open(sys.argv[1], 'rb')
if f:
csv_buffer = f.read()
f.close()
if sys.argv[1] == "-r":
f = open(sys.argv[2], 'rb')
if f:
csv_buffer = f.read()
f.close()
else:
print 'Could not open file for reading'
sys.exit(1)
csv = CSV(csv_buffer, outfile=sys.argv[3], debug=False)
elif sys.argv[1] == "-w":
f = open(sys.argv[2], 'rb')
if f:
input_file = f.read()
f.close()
else:
print 'Could not open file for reading'
sys.exit(1)
csv = CSV()
csv.write(input_file, sys.argv[3])
else:
print 'Could not open file for reading'
print 'Usage: python -r csv.py <filename.csv> <filename.hog>'
print ' == OR == '
print 'Usage: python -w csv.py <filename.hog> <filename.csv>'
sys.exit(1)
csv = CSV(csv_buffer)
if __name__ == "__main__":
main()