Reputation: 435
In need open my CSV file and replace the line breaks in column number 44 in c#.
This is the string on the CSV file :
9|D60||08/12/2018 19:09:19|08/12/2018 19:30:07|00:20:48|08/12/2018 22:00|A|L|M|D60 SM |D60 MA |0|19|0|3175|0|3|00:03:25|20,98|0,017|0|0|NO|NO|-|CO|CH|CA|-|D60 - B|DP6 - F|DP6 - L|NO|-|-|DP6 - L|- - -|ND|ND|NO|-|NO|--dic
-acc |-|-|0|0|
I have tried withous success this code :
output = System.Web.HttpContext.Current.Server.MapPath("csv/target_" + DateTime.Now.AddDays(-1).ToString("dd-MM-yyyy") + ".csv")
string so = System.IO.File.ReadAllText(output);
string[] arr = so.Split('-');
for (int i = 2; i < arr.Length; i = i + 44)
{
arr[i] = arr[i].Replace(Environment.NewLine, " ");
}
string res = String.Join("", arr);
System.IO.File.WriteAllText(output, res);
For this new output with only one line :
9|D60||08/12/2018 19:09:19|08/12/2018 19:30:07|00:20:48|08/12/2018 22:00|A|L|M|D60 SM |D60 MA |0|19|0|3175|0|3|00:03:25|20,98|0,017|0|0|NO|NO|-|CO|CH|CA|-|D60 - B|DP6 - F|DP6 - L|NO|-|-|DP6 - L|- - -|ND|ND|NO|-|NO|--dic-acc |-|-|0|0|
How to do resolve this ?
Upvotes: 1
Views: 16099
Reputation: 61013
Ok, let's try new approach.
The idea is to read in the csv file line by line. Then, foreach line, check how many fields there are by splitting it on the pipe symbol separator character.
If there are less then 49 fields (that is how many fields I counted in your example), append the next line to it. Store all these lines (rows) in a List and finally output that as a new file.
int fieldsExpected = 49; // I have counted 49 fields per row of the CSV file. You should check this!
string fileOut = "<PATH-AND-FILENAME-FOR-THE-OUTPUT-CSV>";
string fileIn = System.Web.HttpContext.Current.Server.MapPath("csv/target_" + DateTime.Now.AddDays(-1).ToString("dd-MM-yyyy") + ".csv");
// Read the file line by line.
string[] lines = System.IO.File.ReadAllLines(fileIn);
List<string> newLines = new List<string>();
// If your csv file has a header row, uncomment this next line
// newLines.Add(lines[0]);
// and have the loop start at 'i = 1'
for (int i = 0; i < lines.Length; i++) {
string temp = lines[i];
// Split the line on the separator character. In this case the pipe symbol "|"
string[] fields = temp.Split('|');
// Check if the number of fields in this line is correct.
// It will be less if any of the fields contained a line break.
// If this is the case, append the next line to this one.
while (fields.Length < fieldsExpected && i < (lines.Length - 1)) {
i++;
temp += lines[i];
fields = temp.Split('|');
}
newLines.Add(temp);
}
System.IO.File.WriteAllLines(fileOut, newLines.ToArray());
Upvotes: 4
Reputation: 34
The following code opens a file and removes line breaks from the string in the file :
static string OpenFileWithoutCRLF()
{
var sr = new StreamReader("LineBreak.txt");
return sr.ReadToEnd().Replace("\r", "").Replace("\n", "");
}
.Replace("\r", "").Replace("\n", "") removes line breaks.
Upvotes: 0
Reputation: 616
Have you tried the following ? (replacing \n\r instead of only the Env.newline)
var output = @"C:\TEMP\target.csv";
string so = System.IO.File.ReadAllText(output);
Console.WriteLine("without replace");
Console.Write(so);
Console.WriteLine("");
Console.WriteLine("with replace");
Console.Write(so.Replace("\n", "").Replace("\r", ""));
Console.ReadLine();
Upvotes: 0
Reputation: 409
You could remove line breaks right after this code:
string so = System.IO.File.ReadAllText(output);
// replace line break here
so = so.Replace("\r\n", "");
Now variable so
will contain "9|D60||08/12/2018 19:09:19|08/12/2018 19:30:07|00:20:48|08/12/2018 22:00|A|L|M|D60 SM |D60 MA |0|19|0|3175|0|3|00:03:25|20,98|0,017|0|0|NO|NO|-|CO|CH|CA|-|D60 - B|DP6 - F|DP6 - L|NO|-|-|DP6 - L|- - -|ND|ND|NO|-|NO|--dic-acc |-|-|0|0|"
without any line breaks.
Upvotes: 1