read all lines in text file with separator - c#

I have a file with this content :
1,2,3,4,5#
1,2,3,4,5#
How can i read all lines using readline ?the important thing is i need to separate the values in each line ,i mean the first line's values 1,2,3,4,5 should be separated .
Suppose i have an array named myarray that can save all values in first line :the array should be like this :
myarray[0]=1
myarray[1]=2
myarray[2]=3
myarray[3]=4
myarray[4]=5
I am so new in IO in c#
Best regards

Using LINQ you can do:
List<string[]> list = File.ReadLines("YourFile.txt")
.Select(r => r.TrimEnd('#'))
.Select(line => line.Split(','))
.ToList();
File.ReadLines would read the file line by line.
.Select(r => r.TrimEnd('#')) would remove the # from end of the
line
.Select(line => line.Split(',')) would split the line on comma and return an array of string items.
ToList() would give you a List<string[]> back.
You can also use TrimEnd and Split in a single Select statement like below, (it would result in the same output):
List<string[]> list = File.ReadLines("YourFile.txt")
.Select(r => r.TrimEnd('#').Split(','))
.ToList();

Try this
string[] readText = File.ReadAllLines(path);
That will return an array of all the lines.
https://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx

You can use a StreamReader to read all the lines in from a file and split them with a given delimiter (,).
var filename = #"C:\data.txt";
using (var sr = new StreamReader(filename))
{
var contents = sr.ReadToEnd();
var myarray = contents.
Split(',');
}
Although I do prefer the LINQ approach answer further up.

Related

Output string after certain character is met

so what im doing currently is
getting the text from a file i.e a .txt
and putting it into an array i.e
whilst comparing the two files and outputting the differences between a and B
string[] linesA = File.ReadAllLines(path\file.txt);
string[] linesB = File.ReadAllLines(path\file2.txt);
IEnumerable<String> onlyB = linesB.Except(linesA);
string[] newstr = new HashSet<string>(onlyB).ToArray();
File.WriteAllLines('C:\path\', newstr);
and lets say the text inside the files includes : i.e
file a:
code(324332): 65dfsdf4fth
code(32342): hdfgvsdfsdgh
code(323462): h29dfs8dh
file b:
code(324332): 65dfsdf4fth
code(32342): hdfgvsdfsdgh
code(323462): h29dfs8dh
code(453453): 8gbhfhk,jv
code(343435): gigdbioyvgi
code(3435343): guidfyvfhs
how would i go about getting the text after :
and removing duplicates
so in the end the output would be
8gbhfhk,jv
gigdbioyvgi
guidfyvfhs
edited:
Kind regards,
Phil
You can browse files and registrars row by row in a list of type "Dictionary .Add (TKey, TValue)" so that you only have the unique values.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.add?view=netframework-4.8
To get the text after : you can use the Substring and IndexOf methods, then remove the whitespace at the beginning of your new string with TrimStart. At the end use Concat to combine the two lists and GroupBy to filter out the values that have duplicates:
string[] linesA = File.ReadAllLines(#"C:\file.txt");
string[] linesB = File.ReadAllLines(#"C:\file2.txt");
IEnumerable<string> linesA2 = linesA.Select(x => x.Substring(x.IndexOf(":") + 1).TrimStart());
IEnumerable<string> linesB2 = linesB.Select(x => x.Substring(x.IndexOf(":") + 1).TrimStart());
string[] result = linesA2.Concat(linesB2).GroupBy(x => x)
.Where(g => g.Count() == 1)
.Select(y => y.Key)
.ToArray();
result:
result[0] ="8gbhfhk,jv"
result[1] ="gigdbioyvgi"
result[2] ="guidfyvfhs"
Write the result array to new text file:
File.WriteAllLines(#"C:\file3.txt", result);

Reading a line up to a certain amount of data

Im creating an application which will read in a large data file and return a specific selection of text from each line in a .dat file. Please see example of the data below.
22/06/2016 22:18:21.209 Type6 -92.31435 2.06424 0.07686
22/06/2016 22:18:21.210 Type34 -91.4085 1.84464 -0.09333
I need the first 3 sets of data which is the date, time and type. The values after the type go on for a while and i have a large amount of rows which need to collected from. I have thought about just splitting each section of the line and taking the first 3 fields. Would this work or would there be an easier way to complete this?
Thanks
You are on the right way (extracting just three fields); I suggest using Linq in the context, e.g.
var source = File
.ReadLines(#"C:\MyData.dat")
.Select(line => line.Split(new char[] { ' ' }, 4))
.Where(items => items.Length >= 3) // it seems that you have empty lines or something
.Select(items => new {
// Let's combine date and time into DateTime
date = DateTime.ParseExact(items[0] + " " + items[1],
#"dd/MM/yyyy H:m:s.fff",
CultureInfo.InvariantCulture),
kind = items[2] });
// .ToArray(); // you may want add materialization (i.e. read once and put into array)
Having got this Linq query you can easily filter out and represent the data you want, e.g.
var test = source
.Where(item => item.date > DateTime.Now.AddDays(-3)) // let's have fresh records only
.OrderByDescending(item => item.date)
.Select(item => $"{item.date} {item.kind}");
Console.Write(string.Join(Environment.NewLine, test));
You could make something just to read the first chars of each line, but the length of the line is not specified anywhere, so you have to read all the data.
You should use File.ReadLines(path) because it is lazy loading the data. This will only load one line per iteration. Foreach line you should check what data you need and save it on whatever you like...
var relevantData = new List<T>();
foreach(var line in File.ReadLines(path))
{
// parse the data you need.
relevantData.Add( new T { Date = whatever, ..... });
}
If you need to parse it multiple times, you could create an index file that contains the start index of each line.

How to convert csv file's specific column to string List

Does anyone know how to convert a specific column in a csv file to string List ?
I am trying to have a 'contact number' list from csv file. (Please see my csv file in below)
Intending to do
List<string> contactNumberList = new List<string>();
-- contactNumberList.Add("1888714"); (Manual)
---contactNumberList.Add("1888759");(Manual)
In my CSV
"Email","Opt In Date","Opted Out","Opt In Details","Email Type","Opted Out Date","Opt Out Details","Contact Number","Salutation"
"test1#testApp.com","05/01/2014 11:23 AM","F","User Name: capn#goldpop.org.uk. IP Address: 62.213.118.139","0","","","1888714","Mrs Hall"
"test2#testApp.com","05/01/2014 11:23 AM","F","User Name: capntransfer#goldpop.org.uk. IP Address: 62.213.118.139","0","","","1888759","Mrs Heyworth"
For parsing CSVs I suggest using a ligrary a s simply splitting by line /columns separator can bring to errors if the values are escaped.
I.e.
"Mr John, Bob Smith"
is a valid CSV as it is escaped with quotes. But a Split function will stop working.
One valid choice is LumenWorks (you can find it in NuGet).
I.e.
using (var csv = new CsvReader(r, false, csvParserConfiguration.ColumnSeparator, '"', '"', '#', ValueTrimmingOptions.None))
{
// Read lines
while (csv.ReadNextRecord())
{
contactNumberList.Add[7];
}
}
Read the file line by line, Split() by commas, select the desired column, trim quotes off and add to a list?
Try this:
int columnId = 3;
string[] lines = File.ReadAllLines(#"C:\test.csv");
var list = lines.Select(line =>
{ var values = line.Split(';');
return values[columnId];
});
You could try the following:
var reader = new StreamReader(File.OpenRead(#"C:\test.csv"));
List<string> contactNumbersList = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
contactNumbersList.Add(values[7]);
}
but better yet you could use a dedicated library like CSV Reader.
IEnumerable<string> strings = System.IO.File.ReadAllLines({filepath})
.Select(x => x.Split(';')[{columnId}])

Get data from csv to dictionary c#

I need to get data from csv to dictionary but when i try to compile this code i recieve error "An item with the same key has already been added." How to do it ? `
Dictionary<string, string> dic = new Dictionary<string, string>();
public void AddToDic()
{
string line = "";
using (StreamReader sr = new StreamReader(#"words.txt"))
{
while (sr.Peek() != -1)
{
line = line + sr.ReadLine();
string[] splitted = line.Split(' ');
dic.Add(splitted[0], splitted[1]); //ERROR An item with the same key has already been added.
}
}
}
//text in words.txt is like: "car auto" newline "water voda" etc...
Since you don't show us the contents of the file you are trying to parse we can only guess. Here are my guesses (followed by a solution):
each line of the file contains two words
the first word should become the key of a dictionary
the file may contain the same key word multiple times
Since a dictionary requires unique keys and the file may contain the same key multiple times there can be multiple values for each key. So a better data structure might be: Dictionary<string, string[]>.
You can use File.ReadLines or File.ReadAllLines to read the lines of a file and then use LINQ to transform that into a dictionary:
Dictionary<string, string[]> result =
File.ReadLines("words.txt")
.Select(line => line.Split(' '))
.GroupBy(arr => arr[0])
.ToDictionary(gr => gr.Key,
gr => gr.Select(s => s[1]).ToArray());
Explanation: After reading a line it is splitted into a string[]. The result is grouped by the first word which will become the key for the dictionary. Each group is an IEnumerable<string[]> and only the second value from each array is selected into the result.
BTW: If you replace ReadLines with ReadAllLines the file will be read at once and then it will be closed before processing it. ReadLines reads the lines one by one and keeps the file open during processing it.
Try below checking:
if(!dic.ContainsKey(splitted[0])
dic.Add(splitted[0], splitted[1]);
Dictionaries keys must be unique
if(!dic.ContainsKey(splitted[0]))
dic.Add(splitted[0], splitted[1]); //ERROR An item with the same key
will stop the error from happening, but probably not the behavior you want. Think about how you want to handle duplicate keys (fail loading of the file, only store the key of the first one you see, only store the latest one you see, append a counter on the end of the key name if there's a collision)

How do find line in a List

I have a text file that I am reading each line of using sr.readline()
As I read that line, I want to search for it in a List that the line should have been added to previously, then add the line to a NEW (different) list. How do I do this?
List.Contains(string) will tell you if a list already contains an element.
So you will wanna do something like:
if (previousList.Contains(line)){
newList.Add(line);
}
You could loop through this logic:
public void DoWhatYouAreAskingFor(StreamReader sr, List<string> list)
{
string line = sr.ReadLine();
if (!list.Contains(line))
{
list.Add(line);
}
}
You could do something like this.
List<string> listOfStrings = new List<string>() { "foo", "baz", "blah"};
string fileName = #"C:\Temp\demo.txt";
var newlist = (from line in File.ReadAllLines(fileName)
join listItem in listOfStrings
on line equals listItem
select line).ToList();
Edit: as a note, my solution shortcircuits the use of the streamreader and trying to find elements in another list and rather uses LINQ to join the elements of an existing list of strings with the lines from a given input file.
List.Contains(input) is certainly fine, and if you have a lot of inputs to filter, you may want to consider converting the searchable list to a HashSet.

Categories

Resources