Project

General

Profile

« Previous | Next » 

Revision 51

Created join_maps to join two 2-column map spreadsheets

View differences:

data2xml
1 1
#!/usr/bin/env python
2
# Converts a CSV dataset to XML using a mappings spreadsheet
2
# Converts a CSV dataset to XML using a map spreadsheet
3 3

  
4
import copy
4 5
import csv
5 6
import os.path
6 7
import re
7 8
import sys
8
from copy import deepcopy
9 9
from xml.dom.minidom import getDOMImplementation
10 10

  
11 11
sys.path.append(os.path.dirname(__file__)+"/lib")
......
13 13

  
14 14
def main():
15 15
    prog_name = sys.argv[0]
16
    try: mappings_path = sys.argv[1]
16
    try: prog_name, map_path = sys.argv
17 17
    except ValueError:
18
        raise Exception('Usage: '+prog_name+' mappings_path <dataset >output')
18
        raise Exception('Usage: '+prog_name+' map_path <dataset >output')
19 19
    
20
    # Get mappings
21
    mappings = {}
20
    # Get map
21
    map = {}
22 22
    has_types = False # whether outer elements are type containiners
23
    stream = open(mappings_path, 'rb')
23
    stream = open(map_path, 'rb')
24 24
    reader = csv.reader(stream, delimiter=',', quotechar='"')
25 25
    src, dest = reader.next()[:2]
26 26
    for row in reader:
......
28 28
        if name != '' and path != '':
29 29
            if path.startswith('/*s/'): has_types = True # *s used for type elem
30 30
            path = path.replace('<name>', name)
31
            mappings[name] = xpath.XpathParser(path).parse()
31
            map[name] = xpath.XpathParser(path).parse()
32 32
    stream.close()
33 33
    
34 34
    # Process dataset
......
41 41
        row_id = str(row_idx)
42 42
        for idx, name in enumerate(fieldnames):
43 43
            value = row[idx]
44
            if value != '' and name in mappings:
45
                path = deepcopy(mappings[name]) # don't modify main value!
44
            if value != '' and name in map:
45
                path = copy.deepcopy(map[name]) # don't modify main value!
46 46
                xpath.set_id(path, row_id, has_types)
47 47
                xpath.set_value(path, value)
48 48
                xpath.get(doc, path, True)

Also available in: Unified diff