How to make String.Split() work on the new line? [duplicate] - c#

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 3 months ago.
string candidates;
string[] candidatesSplit = { };
string line;
int countLines = 0;
StreamReader sr = new StreamReader("..\\..\\..\\candidates.txt"); // Read candidates from file
candidates = sr.ReadToEnd();
sr.Close();
candidatesSplit = candidates.Split(','); // Split the file with ','
Console.WriteLine(candidatesSplit[30]);
Using this code, I wanted to split every ',' and get specific words out from my text file.
My candidates file looks like this:
100,Esra Tarak,90,D1,D4,D2,A,B,D,C, ,C,A,D,B,C,D,B,A, ,B,A,C,D,C,D,A,D,B,C,D
101,Cem Ak,84,D1,D5, ,A,C,D,C,C,C,A,C,B,C,D,B,A,C,B,A,C,D,C,C,A,D,B,C,D
Code works perfectly for the first line in candidates.txt, however when it comes to the second line on the text file, the output comes out like this:
D
101
I need it to show only like this
101
I cannot put a ',' at the end of my lines. Is there any way to fix this?

Just Split(Environment.NewLine) on the entire input first and then Split(',') again on each line.
using var sr = new StreamReader("..\\..\\..\\candidates.txt");
var candidates = sr.ReadToEnd();
foreach (var line in candidates.Split(Environment.NewLine))
{
var candidatesSplit = line.Split(',');
Console.WriteLine(candidatesSplit[30]);
}

Related

Trying to get certain line endings when using streamreader

I'm trying to get certain line endings when using streamreader in a C# app.
Code:
public static IEnumerable<string> ReadAllLines(string path)
{
if (!File.Exists(path)) return null;
List<string> lines = new List<string>();
using (var reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
lines.Add(reader.ReadLine(#"(\r\n|\n)"));
}
}
return lines.ToArray();
}
you can see where I have reader.ReadLine(#"(\r\n|\n)"); If I write reader.ReadLine(); i have no issues but when I try to add line endings to it like I found online it tells me there is no overload to ReadLine.
Question: Can someone assist me with figuring out how to add certain line endings so I can successfully scan my CSV files?
Update:
So I found a way to add the line endings i was looking for and attempted it three different ways. But I'm still getting \r only one some lines. It doesn't make a lot of sense. Can anyone see any issues with the below lines of code?
var reader = new StreamReader(path, Encoding.Default);
//string text = reader.ReadToEnd();
////// attampt 1 - this gives the best result but is still splitting an a \r in one of the fields
//// List<string> lines = new List<string>(text.Split(new[] {"\r","\n"}, StringSplitOptions.None));
////// attempt 2 This worked almost identical to the option above but seemed faster.
//var lines = Regex.Split(text, "\r\n");
//// attempt 3 - this split both \r and \n separately
// List<string> lines = new List<string>(text.Split("\r\n".ToCharArray()));
any other suggestions on how to do this would be great!
Based on your comment to your question:
so just to explain what is going on i have a CSV file. when you put it in excel i have some lines that go to ZZ and other lines that go to AZ (not as long). the white space at the end of AZ all the way to ZZ gets added to the next line and screws everything. i assumed it was because the line endings were not correct but they are as you state above
Try a String.TrimEnd() method call before adding the string to your list.
public static IEnumerable<string> ReadAllLines(string path)
{
if (!File.Exists(path)) return null;
var lines = new List<string>();
using (var reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
// add the TrimEnd call here
lines.Add(reader.ReadLine().TrimEnd());
}
}
return lines.ToArray();
}

CSV file contents to two dimensional array [duplicate]

This question already has answers here:
Parsing CSV files in C#, with header
(19 answers)
Closed 8 years ago.
This question has been asked several times with different inputs so I thought of reposting it with my requirement.
I have a CSV file which contents string fields in the way given below.
idnum,name1, name2,groupid
idnum,name1, name2,groupid
idnum,name1, name2,groupid
example
s001,sahil,payap,gid0
s002,Amir,Khan,gid02
d003,hrithik,roshan,gid03
I have two dimensional string array. I want to read row by row to my two dimensional array.
When it read it should be like this
arr[0][0]=s001
arr[0][1]=name1
arr[0][2]=name2
arr[0][3]=gid01
arr[1][0]=s002
arr[1][1]=Amir
arr[1][2]=Khan
arr[1][3]=gid04
there are 40 records in a file and it should read till the end of the file.
I need to implement this in C#
Any code sample or any explanation would be great help.
I have no knowledge in csv file handling so please don't ask what did you try, at least if you could give me a code sample for reading just one string for a variable it would be a great help.
And please don't ask to go for another solution.
Thanks.
The simplest way to read a csv file in the way you suggest is probably:
var rows = File.ReadAllLines("myfile.csv").Select(l => l.Split(',').ToArray()).ToArray();
Then:
Console.WriteLine(rows[0][0]); // Will output s001
Console.WriteLine(rows[0][1]); // Will output sahil
Console.WriteLine(rows[0][2]); // Will output payap
Console.WriteLine(rows[0][3]); // Will output gid0
Console.WriteLine(rows[1][0]); // Will output s002
Console.WriteLine(rows[2][0]); // Will output d003
The file would have to be read in line-wise. Each line would have to be separated using String.Split. Then the resulting strings would have to be trimmed using Trim, and finally would have to be written into the respective columns of the current row. However I totally second the comments above; more convenient would be to use some class or struct called Person and then to parse into a List<Person>.
The reading could be done as follows:
String line = String.Empty;
System.IO.StreamReader file = new System.IO.StreamReader("c:\\file.txt");
while((line = file.ReadLine()) != null)
{
String[] parts_of_line = line.Split(',')
for ( int i = 0; i < parts_of_line.Length; i++ )
parts_of_line[i] = parts_of_line[i].Trim();
// do with the parts of the line whatever you like
}
You can do that using the CsvHelper library:
const string Csv = #"s001,sahil,payap,gid0
s002,Amir,Khan,gid02
d003,hrithik,roshan,gid03";
var rows = new List<string[]>();
string[] row;
using (var stringReader = new StringReader(Csv))
using (var parser = new CsvParser(stringReader))
while ((row = parser.Read()) != null)
{
rows.Add(row);
}
Console.WriteLine(rows[0][0]); // Will output s001
Console.WriteLine(rows[0][1]); // Will output sahil
Console.WriteLine(rows[0][2]); // Will output payap
Console.WriteLine(rows[0][3]); // Will output gid0
Console.WriteLine(rows[1][0]); // Will output s002
Console.WriteLine(rows[2][0]); // Will output d003
For a working example, check out this .NET fiddle: http://dotnetfiddle.net/PLPXo8
If you want to read directly from file, you can do this:
var rows = new List<string[]>();
string[] row;
using (var parser = new CsvParser(File.OpenText(#"c:\test.csv")))
while ((row = parser.Read()) != null)
{
rows.Add(row);
}

Read and extract from file

I have a huge file with ~3 mill rows. Every line contains record like this:
1|2|3|4|5|6|7|8|9
Exactly 8 separators like '|' on every line. I am looking for a way to read this file then extract last '9' number only from every line and store it into another file.
edit:
Ok here is what i done already.
using (StreamReader sr = new StreamReader(filepath))
using (StreamWriter sw = new StreamWriter(filepath1))
{
string line = null;
while ((line = sr.ReadLine()) != null)
sw.WriteLine(line.Split('|')[8]);
}
File.WriteAllLines("filepath", File.ReadAllLines(filepath).Where(l => !string.IsNullOrWhiteSpace(l)));
Read file, extract last digits then write in new file and clear blank lines. Last digit is 10-15 symbols and I want to extract first 6. I continue to read and try some and when I'm done or have some question I'll edit again.
Thanks
Edit 2:
Ok, here I take first 8 digits from the number:
sw.WriteLine(line.Substring(0, Math.Min(line.Length, 8)));
Edit 3:
I have no idea how can I match now every numbers that left in file. I want to match them and to see witch number how many times is in the file.
Any help?
I am looking for a way to read this file then extract last [..] number only from every line and store it into another file.
What part exactly are you having trouble with? In psuedo code, this is what you want:
fileReader = OpenFile("input")
fileWriter = OpenFile("output")
while !fileReader.EndOfFile
line = fileReader.ReadLine
records[] = line.Split('|')
value = records[8]
fileWriter.WriteLine(value)
do
So start implementing it and feel free to ask a question on any specific line you're having trouble with. Each line of code I posted contains enough pointers to figure out the C# code or the terms to do a web search for it.
You don't say where you are stuck. Break the problem down:
Write and run minimal C# program
Read lines from file
Break up one line
write result line to a file
Are you stuck on any one of those? Then ask a specific question about that. This decomposition technique is key to many programming tasks, and indeed complex tasks in general.
You might find the string split capability useful.
Because it's a huge file you must read it line by line!
public IEnumerable ReadFileIterator(String filePath)
{
using (StreamReader streamReader = new StreamReader(filePath, Encoding.Default))
{
String line;
while ((line = streamReader.ReadLine()) != null)
{
yield return line;
}
yield break;
}
}
public void WriteToFile(String inputFilePath, String outputFilePath)
{
using (StreamWriter streamWriter = new StreamWriter(outputFilePath, true, Encoding.Default))
{
foreach (String line in ReadFileIterator(inputFilePath))
{
String[] subStrings = line.Split('|');
streamWriter.WriteLine(subStrings[8]);
}
streamWriter.Flush();
streamWriter.Close();
}
}
using (StreamReader sr = new StreamReader("input"))
using (StreamWriter sw = new StreamWriter("output"))
{
string line = null;
while ((line=sr.ReadLine())!=null)
sw.WriteLine(line.Split('|')[8]);
}
Some pointer to start from: StreamReader.Readline() and String.Split(). There are examples on both pages.
With LINQ you could do a thing like the following to filter the numbers:
var numbers = from l in File.ReadLines(fileName)
let p = l.Split('|')
select p[8];
and then write them into a new file like that:
File.WriteAllText(newFileName, String.Join("\r\n", numbers));
Use String.Split() to get the line inside an array and get the last element and store it into another file. Repeat the process for each line.
Try this...
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
string[] words = s.Split('|');
string value = words [8]
Console.WriteLine (value);
}
file.Close();

Fetch csv kind of data from text file [duplicate]

This question already has answers here:
Parsing CSV files in C#, with header
(19 answers)
Closed 1 year ago.
I am having string which gives data more like to csv format .
&stamp;field1;field2;field3;field4;&event
10:44:00.6100;0.000;0.000;0.000;0.000; 10:44:00.7100;23.2;0.230;411.2;0.000; 10:44:00.8100;0.000;0.000;1.022;0.000; 10:44:00.9100;8.000;0.000;232.3;0.000;
10:44:01.2100;0.000;0.000;0.000;0.000; 10:44:01.3100;23.2;0.230;411.2;0.000; 10:44:01.5100;0.000;0.000;1.022;0.000; 10:44:01.7100;8.000;0.000;232.3;0.000;
I want to deserialize this data.
this will give you a list of strings "split" at every ; char. you will want to trim and parse the values after. hope this helps
var stringOfData = "0:44:00.6100;0.000;0.000;0.000;0.000; 10:44:00.7100;23.2;0.230;411.2;0.000; 10:44:00.8100;0.000;0.000;1.022;0.000; 10:44:00.9100;8.000;0.000;232.3;0.000;";
List<string> parsed = new List<string>();
parsed.AddRange(stringOfData.Split(';'));
string line = String.Empty;
string[] parts = null;
using (StreamReader sr = new StreamReader(new FileStream(#"C:\yourfile.csv",
FileMode.Open)))
{
while ((line = sr.ReadLine()) != null)
{
parts = line.Split(new [] { ';' });
//Do whatever you want to do with the array of values here
}
}
As for the deserialization part of the question; you would need to specify the kind of format you would want. If you just want a collection of numbers you want to loop through, you could just add every value to a generic List.
If you want a specific format, you could create a class with fields for each of those values and populate a generic list of that class using those values.

How to delete string from txt file? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Efficient way to delete a line from a text file
I have a multithreaded app, and a text file containing a list of proxy servers. If a proxy is not valid, I need to delete it from text file.
How to do it, if I don't want to lose speed of app?
For example I need to delete 62.109.29.58:8085
from
62.109.7.22:8085
62.109.0.35:8085
92.63.106.111:8085
78.24.216.163:8085
92.63.97.156:8085
82.146.56.156:8085
62.109.29.58:8085
78.24.220.173:8085
78.24.220.111:8085
92.63.106.124:8085
Since your file appears small, you can read the whole file in, remove the line that you must remove, and then write the file back:
File.WriteAllLines("myfile.txt"
, File.ReadLines("myfile.txt").Where(s => s != "62.109.29.58:8085").ToList()
);
string[] lines = System.IO.File.ReadAllLines("filename.txt");
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line == "what you are looking for")
lines[i] = "";
}
string[] newLines = lines.Where(str => !string.IsNullOrEmpty(str)).ToArray();
using (Stream stream = File.OpenWrite("filename.txt"))
{
using (StreamWriter sw = new StreamWriter(stream))
{
foreach (string line in newLines)
{
sw.WriteLine(line);
}
}
}

Categories

Resources