用于将许多.dat文件转换为.csv的函数



我正在努力编写一个for循环,以将大约100个.dat文件转换为.csv

我的.dat文件如下所示:

% Filename : Spm04A1_00288_00001.tif
% Date & Time : 26-Oct-2021 15:45:01
% X-ray Energy (keV) : 20.000
% Exposure Time (s) : 1.030
% Beam Center : 718.20700, 1572.10000
% Sample to Detector Distance (SDD) (mm) : 2175.520
% Detector Pixel Size (mm) : 0.146
% Photodiode Value : 176453.000
% 10 of Sample : 198187
% 10 of Standard : 1
% q(A^-1) I(q) sqrt(I(q))
0.00000000e+00    0.00000000e+00    0.00000000e+00
6.78047596e-04    0.00000000e+00    0.00000000e+00
1.35609519e-03    0.00000000e+00    0.00000000e+00
2.03414279e-03    0.00000000e+00    0.00000000e+00
2.71219038e-03    0.00000000e+00    0.00000000e+00
3.39023798e-03    0.00000000e+00    0.00000000e+00
4.06828558e-03    0.00000000e+00    0.00000000e+00
4.74633317e-03    0.00000000e+00    0.00000000e+00
5.42438077e-03    0.00000000e+00    0.00000000e+00
6.10242836e-03    0.00000000e+00    0.00000000e+00
6.78047596e-03    0.00000000e+00    0.00000000e+00
7.45852356e-03    0.00000000e+00    0.00000000e+00
8.13657115e-03    0.00000000e+00    0.00000000e+00
8.81461875e-03    9.12221748e+00    3.23146137e+00
9.49266634e-03    8.47547513e+00    1.27051027e+00

数据文件由三列(scattering vectorintensitysqrt(intensity)(的X射线散射数据组成。它们是最近一次散射旅行中收到的原始数据文件。为了在不同的软件中处理这些数据文件,我需要将它们转换为.csv

我能够使用以下代码编辑一个文件(并添加标题(:

headerList = ['q(A^-1)', 'I(q)', 'sqrt(I(q))']
data.to_csv("Spm04A3_00258_00001.csv", header=headerList, index=False)
data2 = pd.read_csv("Spm04A3_00258_00001.csv")
print('nModified file:')
print(data2)

不幸的是,这对于转换100个数据文件来说并不有效,但我真的很难编写循环。如果有任何建议,我将不胜感激。

我假设您希望循环遍历每个CSV文件。我将做一些非常宽泛的假设,由你来验证。

from pathlib import Path
headerList = ['q(A^-1)', 'I(q)', 'sqrt(I(q))']
csv_dir = Path("/path/where/dat/files/are/located")
for file in csv_dir.glob("*.dat"):
# each file is of type PosixPath. You can access its parent directory, its name, etc
# Here I'm placing the CSV file in the same place as the dat file
csv_file = file.with_suffix(".csv")
# Add your code here, that loads the dat file
data = load_the_dat_file(file)
data.to_csv(csv_file, header=headerList, index=False)
data2 = pd.read_csv(csv_file)
print('nModified file:')
print(data2)

我把你的代码放在一个循环中。我不确定这是否是您想要实现的,但这是对所有.dat文件的循环。

额外:

之后可能没有必要再阅读CSV。您只需替换数据帧的标题:

data.headers = headerList

这里有一个仅使用标准Python模块的替代方案:

from pathlib import Path
import csv
dats = Path('/folder/with/datfiles')
headers = ['q(A^-1)', 'I(q)', 'sqrt(I(q))']
for dat in dats.glob('*.dat'):
with dat.with_suffix('csv').open('w') as f:
rows = [
_.strip().split() 
for _ in dat.read_text().readlines()
if not _.startswith('%')
]
writer = csv.writer(f, delimiter=',')
writer.writerow(headers)
writer.writerows(rows)

上面的代码将处理在dats文件夹中找到的任何.dat文件,并在同一文件夹中生成相应的.csv文件。

rows是由当前.dat文件中不以%开头的所有行填充的列表。

最新更新