我在C#上工作。我的输入文件看起来像:
d00 d04 WinMain
d00 d04 lpCmdLine: '/UNREGSERVER'
d00 d04 Run
d00 d04
lpCmdLine: '/UNREGSERVER'
d00 d04 nCmdShow: 10
d00 d04 leaving WinMain
CustomerID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57 Berlin NULL 12209 Germany 030-0074321 030-0076545
ANATR Ana Trujillo Emparedados y helados Ana Trujillo Owner Avda. de la Constitución 2222 México D.F. NULL 05021 Mexico (5) 555-4729 (5) 555-3745
ANTON Antonio Moreno Taquería Antonio Moreno Owner Mataderos 2312 México D.F. NULL 05023 Mexico (5) 555-3932 NULL
AROUT Around the Horn Thomas Hardy Sales Representative 120 Hanover Sq. London NULL WA1 1DP UK (171) 555-7788 (171) 555-6750
BERGS Berglunds snabbköp Christina Berglund Order Administrator Berguvsvägen 8 Luleå NULL S-958 22 Sweden 0921-12 34 65 0921-12 34 67
BLAUS Blauer See Delikatessen Hanna Moos Sales Representative Forsterstr. 57 Mannheim NULL 68306 Germany 0621-08460 0621-08924
BLONP Blondesddsl père et fils Frédérique Citeaux Marketing Manager 24, place Kléber Strasbourg NULL 67000 France 88.60.15.31 88.60.15.32
BOLID Bólido Comidas preparadas Martín Sommer Owner C/ Araquil, 67 Madrid NULL 28023 Spain (91) 555 22 82 (91) 555 91 99
BONAP Bon app' Laurence Lebihan Owner 12, rue des Bouche
d00 d04 WinMain
d00 d04 lpCmdLine: '/UNREGSERVER'
d00 d04 Run
d00 d04
lpCmdLine: '/UNREGSERVER'
d00 d04 nCmdShow: 10
d00 d04 leaving WinMain
CustomerID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57 Berlin NULL 12209 Germany 030-0074321 030-0076545
ANATR Ana Trujillo Emparedados y helados Ana Trujillo Owner Avda. de la Constitución 2222 México D.F. NULL 05021 Mexico (5) 555-4729 (5) 555-3745
ANTON Antonio Moreno Taquería Antonio Moreno Owner Mataderos 2312 México D.F. NULL 05023 Mexico (5) 555-3932 NULL
AROUT Around the Horn Thomas Hardy Sales Representative 120 Hanover Sq. London NULL WA1 1DP UK (171) 555-7788 (171) 555-6750
BERGS Berglunds snabbköp Christina Berglund Order Administrator Berguvsvägen 8 Luleå NULL S-958 22 Sweden 0921-12 34 65 0921-12 34 67
BLAUS Blauer See Delikatessen Hanna Moos Sales Representative Forsterstr. 57 Mannheim NULL 68306 Germany 0621-08460 0621-08924
BLONP Blondesddsl père et fils Frédérique Citeaux Marketing Manager 24, place Kléber Strasbourg NULL 67000 France 88.60.15.31 88.60.15.32
BOLID Bólido Comidas preparadas Martín Sommer Owner C/ Araquil, 67 Madrid NULL 28023 Spain (91) 555 22 82 (91) 555 91 99
BONAP Bon app' Laurence Lebihan Owner 12, rue des Bouche
我想将此文本文件保存到* 数据集。 *如何执行此操作。我会编写以下代码来执行此操作。
DataSet ds = TextToDataSet.Convert(@"C:\readme.txt", "MyNewTable", "\t");
public static DataSet Convert(string File,
string TableName, string delimiter)
{
//The DataSet to Return
DataSet result = new DataSet();
//Open the file in a stream reader.
StreamReader s = new StreamReader(File);
//Split the first line into the columns
string[] columns = s.ReadLine().Split(delimiter.ToCharArray());
//Add the new DataTable to the RecordSet
result.Tables.Add(TableName);
//Cycle the colums, adding those that don't exist yet
//and sequencing the one that do.
foreach (string col in columns)
{
bool added = false;
string next = "";
int i = 0;
while (!added)
{
//Build the column name and remove any unwanted characters.
string columnname = col + next;
columnname = columnname.Replace("#", "");
columnname = columnname.Replace("'", "");
columnname = columnname.Replace("&", "");
//See if the column already exists
if (!result.Tables[TableName].Columns.Contains(columnname))
{
//if it doesn't then we add it here and mark it as added
result.Tables[TableName].Columns.Add(columnname);
added = true;
}
else
{
//if it did exist then we increment the sequencer and try again.
i++;
next = "_" + i.ToString();
}
}
}
//Read the rest of the data in the file.
string AllData = s.ReadToEnd();
//Split off each row at the Carriage Return/Line Feed
//Default line ending in most windows exports.
//You may have to edit this to match your particular file.
//This will work for Excel, Access, etc. default exports.
string[] rows = AllData.Split("\r\n".ToCharArray());
//Now add each row to the DataSet
foreach (string r in rows)
{
//Split the row at the delimiter.
string[] items = r.Split(delimiter.ToCharArray());
//Add the item
result.Tables[TableName].Rows.Add(items);
}
//Return the imported data.
return result;
}
如果我使用多分隔而不是错误显示。如何使用多分界。如果我运行代码来加载上面给出的文本文件显示波纹管错误
输入数组比此表中的列数长。
答案 0 :(得分:0)
您的问题是您的分隔符包含在您要分隔的字段中....
换句话说,如果代替空格,客户名称等而不是空格,那么你的代码会很好,这样分割函数只会返回列数据。 例如:
ALFKI Alfreds Futterkiste Maria Anders Sales Representative Obere Str. 57 Berlin NULL 12209 Germany 030-0074321 030-0076545
将拆分为:
ALKFI, Alfreds, Futterkiste, Maria, Anders, Sales, Representative.....
当您使用空格来分隔字段时。
希望能回答你的问题:)
编辑:如下面的评论中所述,我提出了使用RegEx的想法,所以这里是:这将为您提供每行的数据:
Match Telephone = Regex.Match(Target, @"\d*-\d*");
Match Fax = Telephone.NextMatch();
Match Country = Regex.Match(Target, @"\w*(?=\s\d{0,3}-)");
Match PCode = Regex.Match(target, @"(?=\s*)\d*(?=\s*" + Country.Value + ")");
目标是您从文件中读取的行
我现在正在学习RegEx,所以给我一些时间来获得更多的RegEx表达式......