i need to read files that look like this into a datatable:
A02 BLANK031
B02 F357442
C02 F264977
D02 BLANK037
E02 F272521
F02 E121562
G02 F264972
H02 F332321
A03 E208240
B03 F313854
C03 E229786
D03 E229787
E03 F307584
F03 F357478
i have a weird delimitter and some trailing spaces. how would i read this into a datatable such that the first column will contain 'A02','B02'... and the second column will contain 'BLANK031','F357442',etc..
currently i am doing:
DataTable dt = new DataTable();
using (TextReader tr = File.OpenText(batchesAddresses[index]))
{
string line;
while ((line = tr.ReadLine()) != null)
{
string[] items = Regex.Split(line, ' ');
if (dt.Columns.Count == 0)
{
// Create the data columns for the data table based on the number of items
// on the first line of the file
for (int i = 0; i < items.Length; i++)
dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
}
dt.Rows.Add(items);
}
}
but this is not working because i have trailing spaces and multiple spaces between columns
If you use:
static readonly char[] space = { ' ' };
...
string[] items = line.Split(space, StringSplitOptions.RemoveEmptyEntries);
you should get the 2 values you expect, although something more selective might be desirable, especially if the right-hand-side might contain a space in the middle.
Change your regex to something like: (\\w{3})\\s+(\\w{5,10})
. This means capture 3 word chars (including digits) into group 1, look for one or more whitespace characters, and then capture 5-10 word chars into group 2.
Then do:
Regex r = new Regex("(\w{3})\s+(\w{5,10})");
Match m = r.Match(line);
string col1 = m.Groups[1].Value;
string col2 = m.Groups[2].Value;
The error regarding System.StringSplitOptions seems to be a casting bug in the compiler. Add a line prior to your split statement that defines the desired StringSplitOptions and then use the variable in the split statement.
static readonly char[] space = { ' ' };
static readonly StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries;
...
string[] items = line.Split(space, options);
This should work for all overloads.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.