簡體   English   中英

將表格格式(.csv)的數據轉換為文本文件

[英]converting data from tabular (.csv) format into text files

我試圖弄清楚如何將文本庫數據(多選題)表轉換成標准格式,其中每個問題都是它自己的單獨的.Rnw文件。 這使我可以創建一個可與R的考試包一起使用的測試庫,以書面或計算機呈現的格式創建不同的考試。

我有表格格式(.csv)的測試銀行數據,其中的結構化數據如下所示(用分號分隔):

question.no;question.text;choice.a;choice.b;choice.c;choice.d;choice.e;answer;label.1;label.2
1;This is the question text of 1;text of choice a;text of choice b;text of choice c;text of choice d;text of choice e;A;question.type.1;question.type.2
2;This is the question text of 2;text of choice a;text of choice b;text of choice c;text of choice d;text of choice e;A;question.type.1;question.type.2

我想做的是解析此文件,為每行數據創建一個單獨的.Rnw文件,其中第1行的輸出為:

\begin{question}
This is the question text of 1
\begin{answerlist} 
\item text of choice a
\item text of choice b
\item text of choice c
\item text of choice d
\item text of choice e
\end{answerlist}
\end{question}

\begin{solution}
The right answer is A
\end{solution}

\exname{defaggdemand}
\extype{schoice}
% \label.1{question.type.1}
% \label.2{question.type.2}
% \exsolution{10000}
\exshuffle{TRUE}

該文件將被命名為“ question_1.Rnw”,第2行的輸出類似如下:

\begin{question}
This is the question text of 2 
\begin{answerlist} 
\item text of choice a
\item text of choice b
\item text of choice c
\item text of choice d
\item text of choice e
\end{answerlist}
\end{question}

\begin{solution}
The right answer is A
\end{solution}

\exname{defaggdemand}
\extype{schoice}
% \label.1{question.type.1}
% \label.2{question.type.2}
% \exsolution{10000}
\exshuffle{TRUE}

並根據.csv數據的第一列將該文件稱為“ question_2.Rnw”。

這個想法是,該策略將以一個大的.csv表作為輸入,並輸出到目錄中每行testbank數據一個.Rnw文件,將數據從csv轉換為一個testbank問題目錄,以供考試軟件包使用。

在執行此操作之前,我曾使用過諸如sed或正則表達式之類的文本解析方法來修復打印的文本庫中的問題集,但這是我第一次以這種結構化,統一的格式獲取測試庫數據。

我確定我可以將某種文本替換方法融合在一起,該方法將采用每個定界符並替換正確的文本和行返回集,但這似乎容易出錯,並且我懷疑有一種最優雅的方法。

我將不勝感激關於如何弄清楚該如何做的任何指示。

Perl進行救援!

問題模板保留在“數據”部分中。 Text :: CSV_XS用於處理csv。 將跳過csv的第一行(第一列包含question.no ),其他行用於填充模板-每個%1%2等均替換為相應的列值。 結果保存到從第一列創建名稱的文件中。

#!/usr/bin/perl
use warnings;
use strict;

use Text::CSV_XS qw{ csv };

my $template = do { local $/; <DATA> };

csv(in       => shift,
    sep_char => ';',
    out      => \ 'skip',
    on_in    => sub {
        return if 'question.no' eq $_[1][0];
        open my $out, '>', "question_$_[1][0].Rnw" or die $!;
        ( my $output = $template ) =~ s/%([0-9])/$_[1][$1]/g;
        print {$out} $output;
        close $out;
});

__DATA__
\begin{question}
%1
\begin{answerlist}
\item %2
\item %3
\item %4
\item %5
\item %6
\end{answerlist}
\end{question}

\begin{solution}
The right answer is %7
\end{solution}

\exname{defaggdemand}
\extype{schoice}
% \label.1{%8}
% \label.2{%9}
% \exsolution{10000}
\exshuffle{TRUE}
$ cat tst.awk
BEGIN { FS=";" }
NR>1 {
    out = "\\begin{question}"
    out = out ORS $2
    for (i=3; i<=7; i++) {
        out = out ORS "\\item " $i
    }
    out = out ORS "\\end{answerlist}"
    out = out ORS "\\end{question}"
    out = out ORS
    out = out ORS "\\begin{solution}"
    out = out ORS "The right answer is " $(i++)
    out = out ORS "\\end{solution}"
    out = out ORS
    out = out ORS "\\exname{defaggdemand}"
    out = out ORS "\\extype{schoice}"
    c=0
    for (; i<=NF; i++) {
        out = out ORS "% \\label." ++c "{" $i "}"
    }
    out = out ORS "\\exsolution{10000}"
    out = out ORS "\\exshuffle{TRUE}"
    print out " > " ("question_" NR-1 ".Rnw")
    close("question_" NR-1 ".Rnw")
}

$ awk -f tst.awk file
\begin{question}
This is the question text of 1
\item text of choice a
\item text of choice b
\item text of choice c
\item text of choice d
\item text of choice e
\end{answerlist}
\end{question}

\begin{solution}
The right answer is A
\end{solution}

\exname{defaggdemand}
\extype{schoice}
% \label.1{question.type.1}
% \label.2{question.type.2}
\exsolution{10000}
\exshuffle{TRUE} > question_1.Rnw
\begin{question}
This is the question text of 2
\item text of choice a
\item text of choice b
\item text of choice c
\item text of choice d
\item text of choice e
\end{answerlist}
\end{question}

\begin{solution}
The right answer is A
\end{solution}

\exname{defaggdemand}
\extype{schoice}
% \label.1{question.type.1}
% \label.2{question.type.2}
\exsolution{10000}
\exshuffle{TRUE} > question_2.Rnw
$

只需將" > "更改為>

這是您可以在python中完成的方法。

基本上,您逐行讀取文件。 忽略第一行,因為它似乎只是列描述。 從第二行開始,分割定界符上的每一行。 將列表值分配給一堆變量以供以后參考。 打開一個要寫入的新文件。 使用f.write選項寫出您的模板以及上面保存的變量。

with open("q-and-a-sheet-template.csv", "r") as infile:
    next(infile)
    filecount = 1
    for line in infile:
        if line:
            num, question_text, choice_a, choice_b, choice_c, choice_d, choice_e, answer, tag1, tag2 = line.split(';')
            outfile = "outfile"+str(filecount)+".rnw"
            with open(outfile, "a") as f:
                f.write("\\begin{question}\n")
                f.write(question_text+"\n")
                f.write("\\begin{answerlist}\n")
                f.write("\\"+choice_a+"\n")
                f.write("\\"+choice_b+"\n")
                f.write("\\"+choice_c+"\n")
                f.write("\\"+choice_d+"\n")
                f.write("\\"+choice_e+"\n")
                f.write("\\end{answerlist}\n")
                f.write("\\end{question}\n")
                f.write("\n")
                f.write("\\begin{solution}\n")
                f.write("The right answer is" + answer +"\n")
                f.write("\\end{solution}\n")
                f.write("\n")
                f.write("\\exname{defaggdemand}\n")
                f.write("\\extype{schoice}\n")
                f.write("% \\label.1{"+tag1+"}\n")
                f.write("% \\label.1{"+tag2+"}\n")
                f.write("% \\exsolution{10000}\n")
                f.write("\\exshuffle{TRUE}")
        filecount += 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM