Removing empty entries from an Array
up vote
0
down vote
favorite
I am reading the contents of the CSV to a string as below:
string csvData = string.Empty;
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
{
csvData = reader.ReadToEnd();
int totalLength = csvData.TrimEnd('|').Split('|').Length;
string result = null;
result = csvData.TrimEnd('|').Split('|');
if (String.IsNullOrEmpty(result[totalLength-1].Replace(",", "").Trim()))
{
result = result.Take(result.Count() - 1).ToArray();
}
//do some processing to the result here.
}
So below is the contents of my sample csvData:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,
If you see the sample above it does contain the last empty row as it comes out from CSV. To remove the above empty row I use the above code that I have posted.
This all works fine. But the issue comes when I have more than one empty row as example below:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,
With the above input my code just removes one empty row.
What I want that the result should have no empty rows as below:
123,a,b,3|456,c,d,5|111,acd,55,c1
How can I remove all the empty rows from my array.
Thanks
c#
|
show 1 more comment
up vote
0
down vote
favorite
I am reading the contents of the CSV to a string as below:
string csvData = string.Empty;
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
{
csvData = reader.ReadToEnd();
int totalLength = csvData.TrimEnd('|').Split('|').Length;
string result = null;
result = csvData.TrimEnd('|').Split('|');
if (String.IsNullOrEmpty(result[totalLength-1].Replace(",", "").Trim()))
{
result = result.Take(result.Count() - 1).ToArray();
}
//do some processing to the result here.
}
So below is the contents of my sample csvData:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,
If you see the sample above it does contain the last empty row as it comes out from CSV. To remove the above empty row I use the above code that I have posted.
This all works fine. But the issue comes when I have more than one empty row as example below:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,
With the above input my code just removes one empty row.
What I want that the result should have no empty rows as below:
123,a,b,3|456,c,d,5|111,acd,55,c1
How can I remove all the empty rows from my array.
Thanks
c#
3
There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required
– Disaffected 1070452
2 days ago
Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.
– Will
2 days ago
@Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper
– user1563677
2 days ago
@Will sorry could you give me an example please.
– user1563677
2 days ago
@Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.
– user1563677
2 days ago
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am reading the contents of the CSV to a string as below:
string csvData = string.Empty;
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
{
csvData = reader.ReadToEnd();
int totalLength = csvData.TrimEnd('|').Split('|').Length;
string result = null;
result = csvData.TrimEnd('|').Split('|');
if (String.IsNullOrEmpty(result[totalLength-1].Replace(",", "").Trim()))
{
result = result.Take(result.Count() - 1).ToArray();
}
//do some processing to the result here.
}
So below is the contents of my sample csvData:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,
If you see the sample above it does contain the last empty row as it comes out from CSV. To remove the above empty row I use the above code that I have posted.
This all works fine. But the issue comes when I have more than one empty row as example below:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,
With the above input my code just removes one empty row.
What I want that the result should have no empty rows as below:
123,a,b,3|456,c,d,5|111,acd,55,c1
How can I remove all the empty rows from my array.
Thanks
c#
I am reading the contents of the CSV to a string as below:
string csvData = string.Empty;
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
{
csvData = reader.ReadToEnd();
int totalLength = csvData.TrimEnd('|').Split('|').Length;
string result = null;
result = csvData.TrimEnd('|').Split('|');
if (String.IsNullOrEmpty(result[totalLength-1].Replace(",", "").Trim()))
{
result = result.Take(result.Count() - 1).ToArray();
}
//do some processing to the result here.
}
So below is the contents of my sample csvData:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,
If you see the sample above it does contain the last empty row as it comes out from CSV. To remove the above empty row I use the above code that I have posted.
This all works fine. But the issue comes when I have more than one empty row as example below:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,
With the above input my code just removes one empty row.
What I want that the result should have no empty rows as below:
123,a,b,3|456,c,d,5|111,acd,55,c1
How can I remove all the empty rows from my array.
Thanks
c#
c#
asked 2 days ago
user1563677
147116
147116
3
There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required
– Disaffected 1070452
2 days ago
Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.
– Will
2 days ago
@Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper
– user1563677
2 days ago
@Will sorry could you give me an example please.
– user1563677
2 days ago
@Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.
– user1563677
2 days ago
|
show 1 more comment
3
There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required
– Disaffected 1070452
2 days ago
Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.
– Will
2 days ago
@Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper
– user1563677
2 days ago
@Will sorry could you give me an example please.
– user1563677
2 days ago
@Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.
– user1563677
2 days ago
3
3
There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required
– Disaffected 1070452
2 days ago
There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required
– Disaffected 1070452
2 days ago
Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.
– Will
2 days ago
Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.
– Will
2 days ago
@Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper
– user1563677
2 days ago
@Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper
– user1563677
2 days ago
@Will sorry could you give me an example please.
– user1563677
2 days ago
@Will sorry could you give me an example please.
– user1563677
2 days ago
@Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.
– user1563677
2 days ago
@Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.
– user1563677
2 days ago
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
I advise using CSVHelper. CSV is not such a simple format as it seems.
With CSVHelper you can do this:
using (var csv = new CsvReader(reader))
{
csv.Configuration.SkipEmptyRecords = true;
var records = csv.GetRecords<Foo>().ToArray();
}
there is no such property as SkipEmptyRecords
– user1563677
2 days ago
1
Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
– Stanislav Molchanovsky
2 days ago
I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };
– user1563677
2 days ago
1
If skipping records does not work because of empty strings, you can use TrimFields property
– Stanislav Molchanovsky
2 days ago
add a comment |
up vote
0
down vote
I'm almost certain the people advising you to CSVHelper
or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:
private void func()
{
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
parsedLines.Add(line.Split(',')); //add that as a list of columns
//select rows that have at least one column with text
var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
}
If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
var unparsedLines = input.TrimEnd('|').Split('|');
Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
var result = unparsedLines.Where(o => re.Match(o).Success);
add a comment |
up vote
0
down vote
also
s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
var sprev = s; string res;
while(true)
{
var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
if(snew == sprev)
{
res = snew.Trim('|');
break;
}
sprev = snew;
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I advise using CSVHelper. CSV is not such a simple format as it seems.
With CSVHelper you can do this:
using (var csv = new CsvReader(reader))
{
csv.Configuration.SkipEmptyRecords = true;
var records = csv.GetRecords<Foo>().ToArray();
}
there is no such property as SkipEmptyRecords
– user1563677
2 days ago
1
Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
– Stanislav Molchanovsky
2 days ago
I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };
– user1563677
2 days ago
1
If skipping records does not work because of empty strings, you can use TrimFields property
– Stanislav Molchanovsky
2 days ago
add a comment |
up vote
2
down vote
accepted
I advise using CSVHelper. CSV is not such a simple format as it seems.
With CSVHelper you can do this:
using (var csv = new CsvReader(reader))
{
csv.Configuration.SkipEmptyRecords = true;
var records = csv.GetRecords<Foo>().ToArray();
}
there is no such property as SkipEmptyRecords
– user1563677
2 days ago
1
Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
– Stanislav Molchanovsky
2 days ago
I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };
– user1563677
2 days ago
1
If skipping records does not work because of empty strings, you can use TrimFields property
– Stanislav Molchanovsky
2 days ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I advise using CSVHelper. CSV is not such a simple format as it seems.
With CSVHelper you can do this:
using (var csv = new CsvReader(reader))
{
csv.Configuration.SkipEmptyRecords = true;
var records = csv.GetRecords<Foo>().ToArray();
}
I advise using CSVHelper. CSV is not such a simple format as it seems.
With CSVHelper you can do this:
using (var csv = new CsvReader(reader))
{
csv.Configuration.SkipEmptyRecords = true;
var records = csv.GetRecords<Foo>().ToArray();
}
answered 2 days ago
Stanislav Molchanovsky
447116
447116
there is no such property as SkipEmptyRecords
– user1563677
2 days ago
1
Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
– Stanislav Molchanovsky
2 days ago
I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };
– user1563677
2 days ago
1
If skipping records does not work because of empty strings, you can use TrimFields property
– Stanislav Molchanovsky
2 days ago
add a comment |
there is no such property as SkipEmptyRecords
– user1563677
2 days ago
1
Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
– Stanislav Molchanovsky
2 days ago
I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };
– user1563677
2 days ago
1
If skipping records does not work because of empty strings, you can use TrimFields property
– Stanislav Molchanovsky
2 days ago
there is no such property as SkipEmptyRecords
– user1563677
2 days ago
there is no such property as SkipEmptyRecords
– user1563677
2 days ago
1
1
Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
– Stanislav Molchanovsky
2 days ago
Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
– Stanislav Molchanovsky
2 days ago
I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };
– user1563677
2 days ago
I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };
– user1563677
2 days ago
1
1
If skipping records does not work because of empty strings, you can use TrimFields property
– Stanislav Molchanovsky
2 days ago
If skipping records does not work because of empty strings, you can use TrimFields property
– Stanislav Molchanovsky
2 days ago
add a comment |
up vote
0
down vote
I'm almost certain the people advising you to CSVHelper
or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:
private void func()
{
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
parsedLines.Add(line.Split(',')); //add that as a list of columns
//select rows that have at least one column with text
var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
}
If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
var unparsedLines = input.TrimEnd('|').Split('|');
Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
var result = unparsedLines.Where(o => re.Match(o).Success);
add a comment |
up vote
0
down vote
I'm almost certain the people advising you to CSVHelper
or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:
private void func()
{
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
parsedLines.Add(line.Split(',')); //add that as a list of columns
//select rows that have at least one column with text
var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
}
If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
var unparsedLines = input.TrimEnd('|').Split('|');
Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
var result = unparsedLines.Where(o => re.Match(o).Success);
add a comment |
up vote
0
down vote
up vote
0
down vote
I'm almost certain the people advising you to CSVHelper
or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:
private void func()
{
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
parsedLines.Add(line.Split(',')); //add that as a list of columns
//select rows that have at least one column with text
var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
}
If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
var unparsedLines = input.TrimEnd('|').Split('|');
Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
var result = unparsedLines.Where(o => re.Match(o).Success);
I'm almost certain the people advising you to CSVHelper
or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:
private void func()
{
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
parsedLines.Add(line.Split(',')); //add that as a list of columns
//select rows that have at least one column with text
var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
}
If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";
var unparsedLines = input.TrimEnd('|').Split('|');
Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
var result = unparsedLines.Where(o => re.Match(o).Success);
edited 2 days ago
answered 2 days ago
zzxyz
2,1551624
2,1551624
add a comment |
add a comment |
up vote
0
down vote
also
s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
var sprev = s; string res;
while(true)
{
var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
if(snew == sprev)
{
res = snew.Trim('|');
break;
}
sprev = snew;
}
add a comment |
up vote
0
down vote
also
s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
var sprev = s; string res;
while(true)
{
var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
if(snew == sprev)
{
res = snew.Trim('|');
break;
}
sprev = snew;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
also
s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
var sprev = s; string res;
while(true)
{
var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
if(snew == sprev)
{
res = snew.Trim('|');
break;
}
sprev = snew;
}
also
s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
var sprev = s; string res;
while(true)
{
var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
if(snew == sprev)
{
res = snew.Trim('|');
break;
}
sprev = snew;
}
answered 2 days ago
AndrewF
332
332
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418053%2fremoving-empty-entries-from-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required
– Disaffected 1070452
2 days ago
Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.
– Will
2 days ago
@Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper
– user1563677
2 days ago
@Will sorry could you give me an example please.
– user1563677
2 days ago
@Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.
– user1563677
2 days ago