### 此文仅作记录
使用HighScore软件只能打开.xrdml格式的XRD数据文件,对于一些.txt或者.xy格式的文件是无法打开的,因此需要转换,工具:Python。
.txt 或.xy的数据格式如下:
第一列:2 theta
第二列:Intensity
.xrdml格式如下
2 theta是以起始角和终止角的形式写入
intensity以空格形式分割
文件转换思路如下:
1. 逐行读取.txt文件
2. 分割(split),split()[0]为2theta,split()[1]为intensity
3. 从其他地方复制一个.xrdml文件
4. 将intensity写入.xrdml文件的对应行处
5. 将2theta的第一个写入<startPosition>
6. 将2theta的最后一个写入<endPosition>
代码如下:
import shutil
def Copyxrdml():
shutil.copy("F:/1.xrdml", file_path + "/" + str(name) + ".xrdml")
def Conversion():
with open(file_path + "/" + data) as f:
f_line = f.readlines()
two_theta = []
intensity = " <intensities unit=\"counts\">"
for i in range(2, len(f_line)):
two_theta.append(f_line[i].split("\t")[0])
intensity = intensity + f_line[i].split("\t")[1].split("\n")[0] + " "
intensity = intensity + "</intensities>"
two_theta_start = float(two_theta[0])
two_theta_end = float(two_theta[len(two_theta)-1])
print(two_theta_start, two_theta_end)
print(intensity)
with open(file_path + "/" + str(name) + ".xrdml", "r+") as f2:
f2_lines = f2.readlines()
f2_lines[76] = " <startPosition>" + str(two_theta_start) + "</startPosition>\n"
f2_lines[77] = " <endPosition>" + str(two_theta_end) + "</endPosition>\n"
f2_lines[80] = " <startPosition>" + str(two_theta_start / 2) + "</startPosition>\n"
f2_lines[81] = " <endPosition>" + str(two_theta_end / 2) + "</endPosition>\n"
f2_lines[84] = intensity
f2.close()
with open(file_path + "/" + str(name) + ".xrdml", "w+") as f3:
f3.writelines(f2_lines)
f3.close()
if __name__ == "__main__":
file_path = r"F:\20210806\210629-0572-YCM"
for name in range(4, 11):
data = str(name) + ".TXT"
Copyxrdml()
Conversion()