Quantcast
Channel: DiKnows. » Uncategorized
Viewing all articles
Browse latest Browse all 10

Python Formatter to Mysql CSV files

$
0
0

In my last days at Integrant company, Me and Yousef Mamdouh held the task of creating the project documentation. Because the project is database driven, i had to display several data records in the documentation in a nice display. For that I have created a simple python script that formats the .csv files exported from MySQL Query Browser.

For example, I needed to format the following data in a file called resultset.csv


"id","name","design_name","view_setting_id","field_setting_id","width","is_sortable","formatter_id","column_order","renderer_type","is_sum","search_field_setting_id","is_using_in_sort"64,"column_user_profile_username","Username",11,,100,1,,0,5,0,,065,"column_user_profile_group","Group",11,,80,1,,1,5,0,,166,"column_user_profile_role","Role",11,,100,1,,2,5,0,,067,"column_user_profile_first_name","First Name",11,,100,1,,3,5,0,,068,"column_user_profile_last_name","Last Name",11,,100,1,,4,5,0,,0

to the following view, to be embedded in the documentation file :

=============COLUMN_SETTING=============
id 64
name “column_user_profile_username”
design_name “Username”
view_setting_id 11
field_setting_id
width 100
is_sortable 1
formatter_id
column_order 0
renderer_type 5
is_sum 0
search_field_setting_id
is_using_in_sort 0
========================================
id 65
name “column_user_profile_group”
design_name “Group”
view_setting_id 11
field_setting_id
width 80
is_sortable 1
formatter_id
column_order 1
renderer_type 5
is_sum 0
search_field_setting_id
is_using_in_sort 1
========================================
so, I created the following python script to do the job script

#!/usr/bin/python
import csv, sys, re

def fixValues(values):
	correctValues = []
	inblock = 0;
	for i, v in enumerate(values):
		if str(v).startswith('"') and not str(v).endswith('"'):
			correctValues.insert(len(correctValues), v)
			inblock = 1;
			continue;
		else:
			if inblock:
				if str(v).endswith('"') :
					inblock = 0
				correctValues [ len(correctValues) - 1 ] = (correctValues [ len(correctValues) - 1 ] + str(v))
			else :
				correctValues.insert(len(correctValues), str(v))
	return correctValues


def htmlz():
    return ''' 
    
        
            
        
        '''

def endhtml():
    return ''' '''


def process (reader, name):
	index = 0
	names = []
	values = []
	for row in reader:
		row[0].replace(',,', ',__,')
		if index == 0:
			names = row[0].split(',')
			names = [ x.replace('"', '') for x in names ] 
			index += 1 
		else : 
			values.insert(len(values), [ x for x in row[0].split(',') ])

	for i, valuesRecord in enumerate(values):
		values[i] = fixValues(valuesRecord)

    
	index = 1        
        
	if name : 
		print '
' + '{:=^40}'.format(str(name)) + '
' else : print '
' + '{:=^40}'.format(str(index)) + '
' for values in values: for i, name in enumerate(names): print "
%-30s %-50.65s " % (name, values[i]) + "
" print "
" + '=' * 40 + "
" print ' ' if __name__ == '__main__': if len(sys.argv) == 3 : print htmlz() csvReader = csv.reader(open(sys.argv[1], 'rb'), delimiter='\n', quotechar='"') process(csvReader, sys.argv[2]) print endhtml() elif len(sys.argv) == 2 : print htmlz() csvReader = csv.reader(open(sys.argv[1], 'rb'), delimiter='\n', quotechar='"') process(csvReader, 0) print endhtml() else : print '-- ' + 'Usage : ./associated_csv_representation.py file.csv table_name' sys.exit(0)

And I have used the following edited script to produce the HTML for this blog:


#!/usr/bin/python
import csv, sys, re

def fixValues(values):
	correctValues = []
	inblock = 0;
	for i, v in enumerate(values):
		if str(v).startswith('"') and not str(v).endswith('"'):
			correctValues.insert(len(correctValues), v)
			inblock = 1;
			continue;
		else:
			if inblock:
				if str(v).endswith('"') :
					inblock = 0
				correctValues [ len(correctValues) - 1 ] = (correctValues [ len(correctValues) - 1 ] + str(v))
			else :
				correctValues.insert(len(correctValues), str(v))
	return correctValues


def htmlz():
    return ''' 
    
        
            
        
        '''

def endhtml():
    return ''' '''


def process (reader, name):
	index = 0
	names = []
	values = []
	for row in reader:
		row[0].replace(',,', ',__,')
		if index == 0:
			names = row[0].split(',')
			names = [ x.replace('"', '') for x in names ] 
			index += 1 
		else : 
			values.insert(len(values), [ x for x in row[0].split(',') ])

	for i, valuesRecord in enumerate(values):
		values[i] = fixValues(valuesRecord)

    
	index = 1        
        
	if name : 
		print '
' + '{:=^40}'.format(str(name)) + '
' else : print '
' + '{:=^40}'.format(str(index)) + '
' for values in values: for i, name in enumerate(names): print "
%-30s %-50.65s " % (name, values[i]) + "
" print "
" + '=' * 40 + "
" print ' ' if __name__ == '__main__': if len(sys.argv) == 3 : print htmlz() csvReader = csv.reader(open(sys.argv[1], 'rb'), delimiter='\n', quotechar='"') process(csvReader, sys.argv[2]) print endhtml() elif len(sys.argv) == 2 : print htmlz() csvReader = csv.reader(open(sys.argv[1], 'rb'), delimiter='\n', quotechar='"') process(csvReader, 0) print endhtml() else : print '-- ' + 'Usage : ./associated_csv_representation.py file.csv table_name' sys.exit(0)

And this is how you can execute it :


./db_records_formatter.py resultset.csv COLUMN_SETTING > x.html

Wish it was helpful


Viewing all articles
Browse latest Browse all 10

Trending Articles