模板生成器

时间:2015-05-07 15:12:12

标签: c# text-files

我有一个名为txtRaw.txt的文本文件,格式为原始数据:

Oranges And Cucumber
Oranges And Apples
Orange And Kiwi
Apples And Mangoes
Apples And Bananas
Apples And Strawberry
Bananas And Wall-nuts
Bananas And Water Melons
Bananas And Grapes

我有第二个名为Template.txt的文本文件:

I went to buy **num0** at the grocery store.
The weather was starting to get cold, so I got thirsty.
I did not want to purchase **num1** so I had to get something else.
I ended up buying **num2** and was done for the day.

我正在尝试创建一个程序,可以获取原始数据并使用Template.txt创建第三个文件,其输出如下:

I went to buy **Oranges And Cucumber** at the grocery store.
The weather was starting to get cold, so I got thirsty.
I did not want to purchase **Oranges And Apples** so I had to get something else.
I ended up buying Orange And Kiwi and was done for the day.
I went to buy **Apples And Mangoes** at the grocery store.
The weather was starting to get cold, so I got thirsty.    
I did not want to purchase **Apples And Bananas** so I had to get something else.
I ended up buying **Apples And Strawberry** and was done for the day.
I went to buy **Bananas And Wall-nuts** at the grocery store.
The weather was starting to get cold, so I got thirsty.
I did not want to purchase **Bananas And Water Melons** so I had to get something else.
I ended up buying **Bananas And Grapes** and was done for the day.

是否有可以执行此操作的现有软件?我有使用C#的经验,所以如果没有软件,那么编写这段代码最有效(内存和处理速度)的方法是什么?截至目前我只想创建一个我读取原始文件的数组。然后我会询问用户txtRaw中每个设置有多少行(橙色是一组,苹果是一组,香蕉是一套,所以每个水果有3套)。然后为每个集合复制模板并粘贴到第二个数组中。然后我将遍历模板数组的每个元素,并用来自rawData数组的元素替换num #text。

我不知道这是否有效,所以我主要询问有关如何有效编程的建议。

2 个答案:

答案 0 :(得分:0)

如果您只是想快速获取输出并且不需要重复此过程,那么您可以尝试使用MS Word的邮件功能。只需将原始数据放在联系人字段中,然后使用联系人变量键入模板。制作您的文件。完成。 (在此选项中,您可以导入原始数据,但可能需要先将其转换为csv文件。

任何表单应用程序都可以这样做;然后你只需使用原始数据填写表格(自动)。

或者如果你想编写一些东西,你可以将原始数据文件的行读入一个字符串数组,然后让它在迭代通过该数组时输出你的模板。

答案 1 :(得分:0)

这个

的代码非常简单

var items=System.IO.File.ReadLines("PathToFile1"); var text=System.IO.File.ReadAllText("PathToFile1"); var optput=string.Format("text",items)

为此您需要使用{0},{1}等来代替使用num0,num1。这可以在存储file2时更改,也可以在使用Regex替换时在代码中更改。