Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将表格(.csv)格式的数据转换为文本文件_Regex_Csv_Parsing_Awk_Sed - Fatal编程技术网

Regex 将表格(.csv)格式的数据转换为文本文件

Regex 将表格(.csv)格式的数据转换为文本文件,regex,csv,parsing,awk,sed,Regex,Csv,Parsing,Awk,Sed,我试图找出如何将文本库数据表(多项选择题)转换为标准格式,其中每个问题都是它自己单独的.Rnw文件。这允许我创建一个测试库,我可以将它与R的考试包一起使用,以书面或计算机呈现的格式创建不同的考试 我有表格格式(.csv)的测试银行数据,其中结构化数据如下所示(用分号分隔): 我想做的是解析此文件,为每行数据创建一个单独的.Rnw文件,其中第1行的输出为: \begin{question} This is the question text of 1 \begin{answerlist} \it

我试图找出如何将文本库数据表(多项选择题)转换为标准格式,其中每个问题都是它自己单独的.Rnw文件。这允许我创建一个测试库,我可以将它与R的考试包一起使用,以书面或计算机呈现的格式创建不同的考试

我有表格格式(.csv)的测试银行数据,其中结构化数据如下所示(用分号分隔):

我想做的是解析此文件,为每行数据创建一个单独的.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”的文件被称为“question_2.Rnw”

其思想是,该策略将一个大的.csv表作为输入,并将每行测试银行数据输出到一个目录.Rnw文件,将csv中的数据转换为一个测试银行问题目录,以便与考试包一起使用

在此之前,我曾使用sed或正则表达式等文本解析方法修复打印的文本库问题集,但这是我第一次使用这种结构化、统一格式的测试库数据

我确信我可以拼凑出某种文本替换方法,使用每个分隔符替换正确的文本和行返回集,但这似乎容易出错,我怀疑有一种最优雅的方法

如果有人给我指点怎么做,我将不胜感激

救援

问题模板保存在数据部分。用于处理csv。跳过csv的第一行(其中第一列包含
问题。否
),其他行用于填充模板-每个
%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}
#/usr/bin/perl
使用警告;
严格使用;
使用Text::CSV_XS qw{CSV};
my$template=do{local$/;};
csv(输入=>shift,
sep_char=>“;”,
out=>\“跳过”,
on_in=>sub{
返回if'question.no'eq$_[1][0];
打开我的$out,“>”,“问题[1][0].Rnw”或die$!;
(我的$output=$template)=~s/%([0-9])/$[1][$1]/g;
打印{$out}$output;
收尾美元;
});
__资料__
\开始{问题}
%1
\开始{应答列表}
\项目%2
\项目%3
\项目%4
\项目%5
\项目%6
\结束{应答列表}
\完{问题}
\开始{解决方案}
正确答案是%7
\结束{解决方案}
\exname{defaggdemand}
\extype{schoice}
%\label.1{%8}
%\label.2{%9}
%\exsolution{10000}
\exshuffle{TRUE}
Perl来营救

问题模板保存在数据部分。用于处理csv。跳过csv的第一行(其中第一列包含
问题。否
),其他行用于填充模板-每个
%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}
#/usr/bin/perl
使用警告;
严格使用;
使用Text::CSV_XS qw{CSV};
my$template=do{local$/;};
csv(输入=>shift,
sep_char=>“;”,
out=>\“跳过”,
on_in=>sub{
返回if'question.no'eq$_[1][0];
打开我的$out,“>”,“问题[1][0].Rnw”或die$!;
(我的$output=$template)=~s/%([0-9])/$[1][$1]/g;
打印{$out}$output;
收尾美元;
});
__资料__
\开始{问题}
%1
\开始{应答列表}
\项目%2
\项目%3
\项目%4
\项目%5
\项目%6
\结束{应答列表}
\完{问题}
\开始{解决方案}
正确答案是%7
\结束{解决方案}
\exname{defaggdemand}
\extype{schoice}
%\label.1{%8}
%\label.2{%9}
%\exsolution{10000}
\exshuffle{TRUE}
只需将
“>”
更改为


只需将
“>”
更改为

以下是如何在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

下面是如何在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

艾德,我不认为到目前为止我所做的任何事情都是有用的,但我认为你的观点是,在这里要勤勉,做一个好公民。我试过你的代码,试着让它为我工作。tst.awk是否应该引用文本文件(我在上面使用的带有数据的.csv文件)?我还没有让它发挥作用,但我确信这只是我的无知,不是你给我的帮助。也许有了这个指导,我可以到达那里。谢谢你的帮助,艾德。请在我的答案下面写上与我的答案相关的评论,以保持问题整洁,谢谢。但在任何情况下,您只需创建一个名为tst.awk的文件,其中完全包含我显示的内容(即awk脚本),然后使用awk执行它,也完全如我所示。我将输入文件(您称之为CSV)命名为
file
。艾德,我不认为到目前为止我所做的任何事情都是有用的,但我认为你的观点是,在这里要勤勉,做一个好公民。我试过你的代码,试着让它为我工作。tst.awk应该是指
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