如何使用 FileHelpers lib 按列和行映射 txt
How to map a txt by column and line with FileHelpers lib
我想知道如何从 txt 文件中获取列项目并在格式化后将其写入输出。
这是我的input.txt
DC 2INCL1 50000 20190802<
DC 2INCL2 50000 20190809<
DC 2INCL3 50000 20190816<
DC 2INCL4 50000 20190823<
DC 2INCL5 50000 20190830<
DC 2INCL6 50000 20190906<
DC 2INCL7 50000 20190913<
DC 2INCL8 50000 20190920<
DC 2INCL9 50000 20190927<
这就是我在 C# 中读取 input.txt 的方式:
string path = @"c:\Users\Dev-02\Desktop\usingFileHelpers\textfiles\";
var engine = new FileHelperEngine<Cliente>();
var result = engine.ReadFile(path + "input.txt");
我知道带有任何定界符的静态文本文件 ,
我应该使用 [DelimitedRecord(",")]
但在我的情况下,我需要按列和行阅读,然后将其写在我的 output.txt
上
我期待这个输出:
1> Type:DC TypeWeb:2INCL1 ValueToPay:50000 Date:2019/08/02<
2> Type:DC TypeWeb:2INCL2 ValueToPay:50000 Date:2019/08/09<
3> Type:DC TypeWeb:2INCL3 ValueToPay:50000 Date:2019/08/16<
4> Type:DC TypeWeb:2INCL4 ValueToPay:50000 Date:2019/08/23<
5> Type:DC TypeWeb:2INCL5 ValueToPay:50000 Date:2019/08/30<
6> Type:DC TypeWeb:2INCL6 ValueToPay:50000 Date:2019/09/06<
7> Type:DC TypeWeb:2INCL7 ValueToPay:50000 Date:2019/09/13<
8> Type:DC TypeWeb:2INCL8 ValueToPay:50000 Date:2019/09/20<
9> Type:DC TypeWeb:2INCL9 ValueToPay:50000 Date:2019/09/27<
提前致谢
由于值中没有空格,如果您要 File.ReadAllLines(... in,您可以执行以下操作:
string[] lines = File.ReadAllLines("C:\MyFile.txt");
foreach (string line in lines)
{
string[] cols = line.split(' ');
}
然后您可以trim根据需要操作 cols 数组中的值
以下给出了正确的结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_FILENAME = @"c:\temp\test.txt";
const string OUTPUT_FILENAME = @"c:\temp\test1.txt";
static void Main(string[] args)
{
List<Column> columns = new List<Column>() {
new Column() { name = "Type", type = typeof(string), width = 5},
new Column() { name = "TypeWeb", type = typeof(string), width = 9},
new Column() { name = "ValueToPay", type = typeof(int), width = 18},
new Column() { name = "Date", type = typeof(DateTime), width = 8, inputFormat = "yyyyMMdd", outputFormat = "yyyy/MM/dd"}
};
List<List<object>> data = FixedWidth.ReadFile(INPUT_FILENAME, columns);
FixedWidth.WriteFile(OUTPUT_FILENAME, data, columns);
}
}
public class FixedWidth
{
public static List<List<object>> ReadFile(string filename,List<Column> columns)
{
List<List<object>> data = new List<List<object>>();
string line = "";
using (StreamReader reader = new StreamReader(filename))
{
while ((line = reader.ReadLine()) != null)
{
List<object> newLine = new List<object>();
data.Add(newLine);
int startPos = 0;
foreach (Column column in columns)
{
string col = line.Substring(startPos, column.width);
switch (column.type.ToString())
{
case "System.String" :
newLine.Add(col.Trim());
break;
case "System.Int32" :
newLine.Add(int.Parse(col));
break;
case "System.DateTime":
newLine.Add(DateTime.ParseExact(col, column.inputFormat, CultureInfo.InvariantCulture));
break;
default:
break;
}
startPos += column.width;
}
}
}
return data;
}
public static List<List<object>> WriteFile(string filename, List<List<object>> data, List<Column> columns)
{
using (StreamWriter writer = new StreamWriter(filename))
{
int rowCount = 1;
foreach(List<object> row in data)
{
string line = string.Format("{0}> ", rowCount.ToString());
for (int index = 0; index < columns.Count; index++)
{
line += string.Format("{0}:", columns[index].name);
switch (columns[index].type.ToString())
{
case "System.DateTime":
line += ((DateTime)row[index]).ToString(columns[index].outputFormat).PadRight(columns[index].width);
break;
default:
line += row[index].ToString().PadRight(columns[index].width);
break;
}
}
writer.WriteLine("{0}<",line);
rowCount++;
}
}
return data;
}
}
public class Column
{
public string name { get; set; }
public Type type { get; set; }
public int width { get; set; }
public string inputFormat { get; set; }
public string outputFormat { get; set; }
}
}
我想知道如何从 txt 文件中获取列项目并在格式化后将其写入输出。
这是我的input.txt
DC 2INCL1 50000 20190802<
DC 2INCL2 50000 20190809<
DC 2INCL3 50000 20190816<
DC 2INCL4 50000 20190823<
DC 2INCL5 50000 20190830<
DC 2INCL6 50000 20190906<
DC 2INCL7 50000 20190913<
DC 2INCL8 50000 20190920<
DC 2INCL9 50000 20190927<
这就是我在 C# 中读取 input.txt 的方式:
string path = @"c:\Users\Dev-02\Desktop\usingFileHelpers\textfiles\";
var engine = new FileHelperEngine<Cliente>();
var result = engine.ReadFile(path + "input.txt");
我知道带有任何定界符的静态文本文件 ,
我应该使用 [DelimitedRecord(",")]
但在我的情况下,我需要按列和行阅读,然后将其写在我的 output.txt
上我期待这个输出:
1> Type:DC TypeWeb:2INCL1 ValueToPay:50000 Date:2019/08/02<
2> Type:DC TypeWeb:2INCL2 ValueToPay:50000 Date:2019/08/09<
3> Type:DC TypeWeb:2INCL3 ValueToPay:50000 Date:2019/08/16<
4> Type:DC TypeWeb:2INCL4 ValueToPay:50000 Date:2019/08/23<
5> Type:DC TypeWeb:2INCL5 ValueToPay:50000 Date:2019/08/30<
6> Type:DC TypeWeb:2INCL6 ValueToPay:50000 Date:2019/09/06<
7> Type:DC TypeWeb:2INCL7 ValueToPay:50000 Date:2019/09/13<
8> Type:DC TypeWeb:2INCL8 ValueToPay:50000 Date:2019/09/20<
9> Type:DC TypeWeb:2INCL9 ValueToPay:50000 Date:2019/09/27<
提前致谢
由于值中没有空格,如果您要 File.ReadAllLines(... in,您可以执行以下操作:
string[] lines = File.ReadAllLines("C:\MyFile.txt");
foreach (string line in lines)
{
string[] cols = line.split(' ');
}
然后您可以trim根据需要操作 cols 数组中的值
以下给出了正确的结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_FILENAME = @"c:\temp\test.txt";
const string OUTPUT_FILENAME = @"c:\temp\test1.txt";
static void Main(string[] args)
{
List<Column> columns = new List<Column>() {
new Column() { name = "Type", type = typeof(string), width = 5},
new Column() { name = "TypeWeb", type = typeof(string), width = 9},
new Column() { name = "ValueToPay", type = typeof(int), width = 18},
new Column() { name = "Date", type = typeof(DateTime), width = 8, inputFormat = "yyyyMMdd", outputFormat = "yyyy/MM/dd"}
};
List<List<object>> data = FixedWidth.ReadFile(INPUT_FILENAME, columns);
FixedWidth.WriteFile(OUTPUT_FILENAME, data, columns);
}
}
public class FixedWidth
{
public static List<List<object>> ReadFile(string filename,List<Column> columns)
{
List<List<object>> data = new List<List<object>>();
string line = "";
using (StreamReader reader = new StreamReader(filename))
{
while ((line = reader.ReadLine()) != null)
{
List<object> newLine = new List<object>();
data.Add(newLine);
int startPos = 0;
foreach (Column column in columns)
{
string col = line.Substring(startPos, column.width);
switch (column.type.ToString())
{
case "System.String" :
newLine.Add(col.Trim());
break;
case "System.Int32" :
newLine.Add(int.Parse(col));
break;
case "System.DateTime":
newLine.Add(DateTime.ParseExact(col, column.inputFormat, CultureInfo.InvariantCulture));
break;
default:
break;
}
startPos += column.width;
}
}
}
return data;
}
public static List<List<object>> WriteFile(string filename, List<List<object>> data, List<Column> columns)
{
using (StreamWriter writer = new StreamWriter(filename))
{
int rowCount = 1;
foreach(List<object> row in data)
{
string line = string.Format("{0}> ", rowCount.ToString());
for (int index = 0; index < columns.Count; index++)
{
line += string.Format("{0}:", columns[index].name);
switch (columns[index].type.ToString())
{
case "System.DateTime":
line += ((DateTime)row[index]).ToString(columns[index].outputFormat).PadRight(columns[index].width);
break;
default:
line += row[index].ToString().PadRight(columns[index].width);
break;
}
}
writer.WriteLine("{0}<",line);
rowCount++;
}
}
return data;
}
}
public class Column
{
public string name { get; set; }
public Type type { get; set; }
public int width { get; set; }
public string inputFormat { get; set; }
public string outputFormat { get; set; }
}
}