Skip to content

Python3 changes #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ xml2csv --input "samples/fruits.xml" --output "samples/fruits.csv" --tag "item"
Convert an XML table to a CSV file.

```
xmltable2csv --input "samples/fruits.xml" --output "samples/fruits.csv" --tag "Data"
xmltable2csv --input "samples/simple-table.xml" --output "samples/simple-table.csv" --tag "Data"
```

###### Arguments
Expand Down
3 changes: 3 additions & 0 deletions samples/simple-table.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Header 1,Header 2
Value R1C1,Value R1C2
Value R2C1,Value R2C2
30 changes: 5 additions & 25 deletions xmlutils/console.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Kailash Nadh, http://nadh.in
June 2013

License: MIT License
Documentation: http://nadh.in/code/xmlutils.py
"""
Expand All @@ -14,14 +14,9 @@


def run_xml2sql():
print("""xml2sql
--help for help

""")

# parse arguments
parser = argparse.ArgumentParser(description='Convert an xml file to sql.')
parser.add_argument('--input', type=file, dest='input_file', required=True, help='input xml filename')
parser.add_argument('--input', dest='input_file', type=argparse.FileType('r'), required=True, help='input xml filename')
parser.add_argument('--output', dest='output_file', required=True, help='output sql filename')
parser.add_argument('--tag', dest='tag', required=True, help='the record tag. eg: item')
parser.add_argument('--table', dest='table', required=True, help='table name')
Expand All @@ -42,14 +37,9 @@ def run_xml2sql():


def run_xml2csv():
print("""xml2csv
--help for help

""")

# parse arguments
parser = argparse.ArgumentParser(description='Convert an xml file to csv format.')
parser.add_argument('--input', dest='input_file', required=True, help='input xml filename')
parser.add_argument('--input', dest='input_file', type=argparse.FileType('r'), required=True, help='input xml filename')
parser.add_argument('--output', dest='output_file', required=True, help='output csv filename')
parser.add_argument('--tag', dest='tag', required=True, help='the record tag. eg: item')
parser.add_argument('--delimiter', dest='delimiter', default=',', help='delimiter character. (default=,)')
Expand All @@ -72,14 +62,9 @@ def run_xml2csv():


def run_xmltable2csv():
print("""xmltable2csv by Yigal Lazarev (http://yig.al)
--help for help

""")

# parse arguments
parser = argparse.ArgumentParser(description='Convert an xml file to csv format.')
parser.add_argument('--input', dest='input_file', required=True, help='input xml filename')
parser.add_argument('--input', dest='input_file', type=argparse.FileType('r'), required=True, help='input xml filename')
parser.add_argument('--output', dest='output_file', required=True, help='output csv filename')
parser.add_argument('--tag', dest='tag', required=True, help='the record tag. eg: Data')
parser.add_argument('--delimiter', dest='delimiter', default=',', help='delimiter character. (default=,)')
Expand All @@ -99,14 +84,9 @@ def run_xmltable2csv():


def run_xml2json():
print("""xml2json
--help for help

""")

# parse arguments
parser = argparse.ArgumentParser(description='Convert an xml file to json.')
parser.add_argument('--input', type=file, dest='input_file', required=True, help='input xml filename')
parser.add_argument('--input', dest='input_file', type=argparse.FileType('r'), required=True, help='input xml filename')
parser.add_argument('--output', dest='output_file', required=True, help='output json filename')
parser.add_argument('--pretty', dest='pretty', required=False, default=False, action='store_true', \
help='pretty print? (default=False)')
Expand Down
23 changes: 16 additions & 7 deletions xmlutils/xml2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xml2json.py
Kailash Nadh, http://nadh.in
December 2012

License: MIT License
Documentation: http://nadh.in/code/xmlutils.py
"""
Expand Down Expand Up @@ -40,7 +40,13 @@ def get_json(self, pretty=True):

try:
while True:
event, root = iterator.next()
try:
# for py version 2.x
event, root = iterator.next()
except AttributeError:
# for py version 3.x
event, root = next(self.context)

except StopIteration:
print("Event StopIteration found, done!")
finally:
Expand Down Expand Up @@ -73,10 +79,13 @@ def _elem2list(self, elem):
block = {}

# get the element's children
children = elem.getchildren()
try:
children = elem.getchildren()
except AttributeError:
children = list(elem)

if children:
cur = map(self._elem2list, children)
cur = list(map(self._elem2list, children))

# create meaningful lists
scalar = False
Expand All @@ -93,7 +102,7 @@ def _elem2list(self, elem):

if scalar:
if len(cur) > 1:
cur = {elem[0].tag: [e.values()[0] for e in cur if e.values()[0] is not None]}
cur = {elem[0].tag: [list(e.values())[0] for e in cur if list(e.values())[0] is not None]}
else:
cur = {elem[0].tag: cur[0].values()[0] }

Expand All @@ -107,8 +116,8 @@ def _elem2list(self, elem):
val = elem.attrib
val = val if len(val) > 0 else None

block[elem.tag] = val
block[elem.tag] = val

return block


Expand Down
9 changes: 7 additions & 2 deletions xmlutils/xml2sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xml2sql.py
Kailash Nadh, http://nadh.in
October 2011

License: MIT License
Documentation: http://nadh.in/code/xmlutils.py
"""
Expand Down Expand Up @@ -57,7 +57,12 @@ def convert(self, tag="item", table="table", ignore=[], limit=-1, packet=8):
self.context = iter(self.context)

# get to the root
event, root = self.context.next()
try:
# for py version 2.x
event, root = self.context.next()
except AttributeError:
# for py version 3.x
event, root = next(self.context)

items = []
fields = []
Expand Down