2016-02-03 22 views
3

塞纳里奥信息和输出,我公司正在改变,以新的应用系统,下面的文字是由旧的应用程序 生成我需要提取信息,我们的外部会计厂商C#中提取文本文件要求与声明

Name computer name/IP address time profit 
Lynn PC2/192.168.100.45  1604 5000 
Kathy PC3/192.168.100.46  1700 8500 
Mike PC6/192.168.100.49  1650 8400 
Luke PC8/192.168.100.51  1600 11500 

与像

Set account lynn PC2 from 192.168.100.45 1604 5000 to acc0001 
Set account Kathy PC3 from 192.168.100.46 1700 8500 to acc0002 
Set account Mike PC6 from 192.168.100.49 1650 8400 to acc0005 
Set account Luke PC8 from 192.168.100.51 1600 11500 to acc0007 

结果是否可以使用C#编程刚做?

目前这是我对编码的初步想法。但我怀疑它的作用,因为它可能需要超载和标题。

using System; 
using System.IO; 

class Test 
{ 
    public static void Main() 
    { 
     try 
     { 
      using (StreamReader sr = new StreamReader(@"C:\temp\Test.txt")) 
      { 
       string line(name, computer, IP_ADD, time, Profit); 

       while ((line = sr.ReadLine()) != null) 
       { 
        Console.WriteLine(line); 
       } 
      } 
     } 

     public static void WriteToFile() 
     { 
      using (StreamWriter sw = File.CreateText(@"c:\temp\EResult.txt")) 
      { 
       sw.WriteLine("Please find the below generated table of 1 to 10"); 
       sw.WriteLine(""); 
       for (int i = 1; i <= 10; i++) 
       { 
        for (int j = 1; j <= 10; j++) 
        { 
         sw.WriteLine(); 
        } 
        sw.WriteLine(""); 
       } 
       Console.WriteLine("successfully written on file."); 
      } 
     } 

     catch (Exception e) 
     { 
      // Let the user know what went wrong. 
      Console.WriteLine("The file could not be read:"); 
      Console.WriteLine(e.Message); 
     } 
    } 
} 
+0

编辑你的代码告诉我,你有一个方法在另一个方法中,并且catch语句不在正确的位置 –

+0

line()在做什么魔术? –

+2

这是真的c#或只是看起来像c#的随机代码? – Gusman

回答

1

要做到这一点,你应该:

  1. 读取和使用CSV库
  2. 采用新格式

写出来的数据到一个新的文件解释旧文件你没有提到的一件事是你如何查找每个新行的最后部分acc0001。你需要弄清楚这一部分。

下面是一个简单LINQPad程序演示的步骤了,请注意,您需要添加一个NuGet包,“CsvHelper”,该计划的工作:

void Main() 
{ 
    var transactions = ReadTransactionsInOldFormat(@"d:\temp\input.txt"); 
    WriteTransactionsInNewFormat(transactions, @"d:\temp\output.txt"); 
} 

public static void WriteTransactionsInNewFormat(IEnumerable<Transaction> transactions, string filename) 
{ 
    using (var file = File.CreateText(filename)) 
     foreach (var transaction in transactions) 
      file.WriteLine($"Set account {transaction.Name} {transaction.ComputerName} from {transaction.IpAddress} {transaction.Time} {transaction.Profit} to acc0001"); 
} 

public static IEnumerable<Transaction> ReadTransactionsInOldFormat(string filename) 
{ 
    using (var file = File.OpenText(filename)) 
    using (var reader = new CsvReader(file)) 
    { 
     reader.Configuration.HasHeaderRecord = true; 
     reader.Configuration.Delimiter = "\t"; 
     reader.Configuration.RegisterClassMap<TransactionMap>(); 

     while (reader.Read()) 
      yield return reader.GetRecord<Transaction>(); 
    } 
} 

public class TransactionMap : CsvClassMap<Transaction> 
{ 
    public TransactionMap() 
    { 
     Map(m => m.Name).Index(0); 
     Map(m => m.ComputerNameAndIpAddress).Index(1); 
     Map(m => m.Time).Index(2); 
     Map(m => m.Profit).Index(3); 
    } 
} 

public class Transaction 
{ 
    public string Name { get; set; } 


    public string ComputerNameAndIpAddress 
    { 
     get 
     { 
      return $"{ComputerName}/{IpAddress}"; 
     } 
     set 
     { 
      var parts = value.Split('/'); 
      ComputerName = parts[0]; 
      IpAddress = parts[1]; 
     } 
    } 

    public string ComputerName { get; set; } 
    public string IpAddress { get; set; } 
    public int Time { get; set; } 
    public int Profit { get; set; } 
} 
+0

我得到的错误无效Main()--->一个namespce不能直接包含成员,如字段或方法 – DerCha

2

有几种方法来做到这一点,但是当文本具有某种结构时,您可以简单地使用正则表达式来解析输入,然后再次格式化结果。

正则表达式可能看起来很复杂,但有一点MSDN的帮助,它可以变得非常简单。或者,如果你想要更多的视觉

Regular expression visualization

See the example on Debuggex

由于这是从来没有解释其中"acc007"来自我会离开这个给你。

void Main() 
{ 
    var input = @"Name computer name/IP address time profit 
Lynn PC2/192.168.100.45  1604 5000 
Kathy PC3/192.168.100.46  1700 8500 
Mike PC6/192.168.100.49  1650 8400 
Luke PC8/192.168.100.51  1600 11500"; 

    var lines = input.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Skip(1); 
    foreach (var line in lines) 
    { 
     // "\w" means any non-whitespace character 
     // "+" means one or more of the previous character 
     // "\s" means whitespace 
     // "[^/]" means all characters except "/" 
     // "\d" means any digit 
     // "\." means "." (since a dot in a regex means any character we need to escape it). 
     // The (?<group-name> group-content) allows us to use name a group 
     // so we can find the group more easily the next time. 
     var regex = @"(?<name>\w+)\s+(?<PC>[^/]+)/(?<IP>[\d\.]+)\s+(?<time>\d+)\s+(?<profit>\d+)"; 
     var match = System.Text.RegularExpressions.Regex.Match(line, regex); 

     var name = match.Groups["name"].Value; 
     var pc = match.Groups["pc"].Value; 
     var ip = match.Groups["ip"].Value; 
     var time = match.Groups["time"].Value; 
     var profit = match.Groups["profit"].Value; 

     // Set account Luke PC8 from 192.168.100.51 1600 11500 to acc0007 
     var result = string.Format("Set account {0} {1} from {2} {3} {4} to {5}", name, pc, ip, time, profit, "acc007"); 

     // Print the result to screen 
     Console.WriteLine(result); 
    } 
}