我需要从我们的MRT扫描仪生成的txt文件中提取信息,然后将信息转换为具有特定结构的JSON文件。 文本文件不是数据文件,而是包含有关该扫描会话的信息的实际文本文件。有人可以让我走上正确的道路吗?
下面是部分文件内容的示例。我更愿意在R中执行此操作,但是也可以使用MATLAB或Python。
Example:
Image filter = "system default";
Uniformity correction = "no";
Geometry correction = "default";
IF_info_seperator = 0;
Total scan duration = "09:05.0";
Rel. SNR = 0.752056241;
Act. TR (ms) = "5000";
Act. TE (ms) = "74";
ACQ matrix M x P = "96 x 94";
ACQ voxel MPS (mm) = "2.50 / 2.55 / 2.50";
REC voxel MPS (mm) = "2.50 / 2.50 / 2.50";
Scan percentage (%) = 97.9166641;
答案 0 :(得分:1)
在R中,使用readLines
,gsub
和同事。
## read raw lines; don't warn "final line" (might have unwanted side-effects)
txt <- readLines("mri.txt", warn=FALSE)
## replace first `=` with `§` and split there
spl <- strsplit(sub("=", "§", txt), "§")
## expand lengths of list elements to 2 (throws a `NA` in case)
spl <- lapply(spl, `length<-`, 2)
## remove leading/trailing whitespace; make matrix
tmp <- t(sapply(spl, trimws))
## replace `;` or `"` with empty string,
tmp[,2] <- gsub(";|\"", "", tmp[,2])
tmp
# [,1] [,2]
# [1,] "Image filter" "system default"
# [2,] "Uniformity correction" "no"
# [3,] "Geometry correction" "default"
# [4,] "IF_info_seperator" "0"
# [5,] "Total scan duration" "09:05.0"
# [6,] "Rel. SNR" "0.752056241"
# [7,] "Act. TR (ms)" "5000"
# [8,] "Act. TE (ms)" "74"
# [9,] "ACQ matrix M x P" "96 x 94"
# [10,] "ACQ voxel MPS (mm)" "2.50 / 2.55 / 2.50"
# [11,] "REC voxel MPS (mm)" "2.50 / 2.50 / 2.50"
# [12,] "Scan percentage (%)" "97.9166641"
最后是jsonlite::toJSON
。
library(jsonlite)
toJSON(tmp)
[["Image filter","system default"],["Uniformity correction","no"],["Geometry correction","default"],["IF_info_seperator","0"],["Total scan duration","09:05.0"],["Rel. SNR","0.752056241"],["Act. TR (ms)","5000"],["Act. TE (ms)","74"],["ACQ matrix M x P","96 x 94"],["ACQ voxel MPS (mm)","2.50 / 2.55 / 2.50"],["REC voxel MPS (mm)","2.50 / 2.50 / 2.50"],["Scan percentage (%)","97.9166641"]]
当然,您可能需要对此进行微调。