Convert ICDD-XML file to CSV file

xml2csv.py

Enter the ICDD-XML file name exported from ICDD PDF 5+, and the CSV (comma separated values) file name listing 2Θ, d, h, k, l.

Example:

PDF Card – 01-086-4266.xml

Usage of xml2csv.py:

> type ‘PDF Card – 01-086-4266.xml’ | python3 xml2csv.py > PDF01-086-4266.csv

on the directory including ‘PDF Card – 01-086-4266.xml’ on cmd.exe (Windows).

A new file ‘PDF01-086-4266.csv’ will be created on the directory.

Output:

tt,d,Int,h,k,l
28.440,3.135760,1000,1,1,1
47.298,1.920250,582,2,2,0
56.117,1.637600,318,3,1,1
69.123,1.357820,73,4,0,0
76.368,1.246030,102,3,3,1
88.020,1.108660,124,4,2,2
94.942,1.045250,65,5,1,1
106.695,0.960127,36,4,4,0
114.076,0.918057,64,5,3,1
127.524,0.858764,55,6,2,0
136.867,0.828265,25,5,3,3

Python code of xml2csv.py:

Click (tap) [Python] icon on the right-top edge of the code area to copy the code to clipboard.

################################################################################
# xml2csv.py
# Convert ICDD XML file to CSV file
# Coded by T. Ida, Sep. 26, 2022
# Modified by T. Ida, Sep. 28, 2022
# How to use:
# For Windows (cmd.exe), 
#  ...> type PDF00-034-0427.xml | python xml2csv.py > PDF00-034-0427.csv
# For macOS and Linux (terminal), 
#  ...$ cat PDF00-034-0427.xml | python xml2csv.py > PDF00-034-0427.csv
#   or
#  ...$ python xml2csv.py < PSD00-034-0327.xml > PSD00-034-0427.csv
################################################################################
import sys
import re
p = re.compile(r"<[^>]*?>")
lines = sys.stdin.readlines()
print("tt,d,Int,h,k,l")
for line in lines:
    if line.rstrip() == "<intensity>":
        pass
    elif "</theta>" in line:
        dest = p.sub("",line).rstrip()
    elif "</da>" in line:
        dest = dest + "," + p.sub("",line).rstrip()
    elif "</intensity>" in line:
        dest = dest + "," + p.sub("",line).rstrip()
    elif "</h>" in line:
        dest = dest + "," + p.sub("",line).rstrip()
    elif "</k>" in line:
        dest = dest + "," + p.sub("",line).rstrip()
    elif "</l>" in line:
        dest = dest + "," + p.sub("",line).rstrip()
    elif "<F/>" in line:
        print(dest)
    elif line.rstrip() == "</intensity>":
        pass