Reading Text file with Linq with 2 patterns - c#

I need to read a text file like this
MyItemName = Description # MoreInfo
Now I need to convert this 3 fields in to a table. using the '=' and '#' as pattern.

Just splitting on = and # - this returns and IEnumerable of an anonymous class with the properties you are interested in:
var items = File.ReadAllLines(fileName)
.Skip(1) //Skip header
.Where( line => !string.IsNullOrWhiteSpace(line))
.Select(line =>
{
var columns = line.Split('=', '#');
return new
{
ItemName = columns[0].Trim(),
Description = columns[1].Trim(),
MoreInfo = columns[2].Trim()
};
});
This approach would require the separator tokens to be used as separators exclusively - if they do occur in any of the fields, this will mess up everything and void this approach.

if you really want to use linq for it...
It doesn't look very nice and it doesn't create a table, but you get the point:
from line in File.ReadAllLines(filename)
let eqPos = line.IndexOf('=')
let atPos = line.IndexOf('#')
select new {
Name = line.Substring(0, eqPos).Trim(),
Desc = line.Substring(eqPos + 1, atPos - (eqPos + 1)).Trim(),
Info = line.Substring(atPos + 1).Trim()
}

Related

C# Remove unnecessary brackets, but leaving just the one

I have the following string list of different sizes
[5].[1].[2].[3].[4].__class SetObjectModel
[5].[1].[4].__class SetObjectModel
[5].[1].[4].[3].[1].__class SetObjectModel
I need to remove all other brackets and just leave the first one:
[5].__class SetObjectModel
Does anyone have any suggestions on how I might achieve this?
Here a full sample of using the Split feature of a string and the Join.
You can read more about Split here and Join here
// source list of string to modify
var source = new List<string>()
{
"[5].[1].[2].[3].[4].__class SetObjectModel",
"[5].[1].[4].__class SetObjectModel",
"[5].[1].[4].[3].[1].__class SetObjectModel"
};
// convert each element of source into a result set
var results = source.Select(value =>
{
// split by the period
var splitted = value.Split(new[] { "." }, StringSplitOptions.None);
// join the first and last element with a period to match the format you want
return string.Join(".", new[] { splitted.First(), splitted.Last() });
}).ToList();
Here's the second portion of converting without using Linq and other quick 1 liner in case you are not familiar with them
List<string> result = new List<string>();
foreach (string value in source)
{
// split by the period
string[] splitted = value.Split(new[] { "." }, StringSplitOptions.None);
// will contain the elements to keep and join
List<string> elements = new List<string>();
// add first element of the split
elements.Add(splitted[0]);
// add the last element of the split
elements.Add(splitted[splitted.Length - 1]);
// join the elements together
string join = string.Join(".", elements);
// add the join to the result set
result.Add(join);
}

Match sections of a List, and Replace if both exist

I've got dates from separate countries within a single List<>. I'm trying to get two records that contain the same characters before the second comma, and replace BOTH of those items with a new one.
Example:
From This:
18/04/2014,Good Friday,England and Wales
18/04/2014,Good Friday,Scotland
Into this:
18/04/2014,Good Friday,"England, Wales and Scotland"
Please note there may be multiple scenarios within the list like the above example. I've managed to get everything before the second Comma with:
splitSubstring = line.Remove(line.LastIndexOf(','));
I've tried the below, but it's clearly flawed since it won't delete both the records even if it does find a match:
foreach (var line in orderedLines)
{
if (splitSubstring == line.Remove(line.LastIndexOf(',')))
{
//Replace if previous is match here
}
splitSubstring = line.Remove(line.LastIndexOf(','));
File.AppendAllText(correctFile, line);
}
I would suggest parsing it into a structure you can work with e.g.
public class HolidayInfo
{
public DateTime Date { get; set; }
public string Name { get; set; }
public string[] Countries { get; set; }
};
And then
string[] lines = new string[]
{
"18/04/2014,Good Friday,England and Wales",
"18/04/2014,Good Friday,Scotland"
};
// splits the lines into an array of strings
IEnumerable<string[]> parsed = lines.Select(l => l.Split(','));
// copy the parsed lines into a data structure you can write code against
IEnumerable<HolidayInfo> info = parsed
.Select(l => new HolidayInfo
{
Date = DateTime.Parse(l[0]),
Name = l[1],
Countries = l[2].Split(new[] {",", " and " }, StringSplitOptions.RemoveEmptyEntries)
});
...etc. And once you have it in a helpful data structure you can begin to develop the required logic. The above code is just an example, the approach is what you should focus on.
I ended up using LINQ to pull apart the List and .Add() them into another based on an if statement. LINQ made it nice and simple.
//Using LINQ to seperate the two locations from the list.
var seperateScotland = from s in toBeInsertedList
where s.HolidayLocation == scotlandName
select s;
var seperateEngland = from e in toBeInsertedList
where e.HolidayLocation == engAndWales
select e;
Thanks for pointing me to LINQ

How do i create a c# dictionary that will read in key and values from .csv file

I have a .csv file with a list of abbreviations and their actual meaning e.g.
Laughing Out Loud, LOL
I need to be able to search for an abbreviation in a text box and replace the abbreviation with the actual words. This is what I have attempted so far to understand dictionaries but cannot work out how to read in values from the file.
Dictionary<string, string> Abbreviations = new Dictionary<string, string>();
Abbreviations.Add("Laughing Out Loud", "lol");
foreach (KeyValuePair<string, string> abbrev in Abbreviations)
{
txtinput.Text = txtinput + "<<" + abbrev.Key + ">>";
}
You can try this LINQ solution the GroupBy is to handle the case where a key is in a file multiple times.
Dictionary<string, string[]> result =
File.ReadLines("test.csv")
.Select(line => line.Split(','))
.GroupBy(arr => arr[0])
.ToDictionary(gr => gr.Key,
gr => gr.Select(s => s[1]).ToArray());
To check if the abbreviation in the TextBox exists in the Dictionary:
foreach (KeyValuePair<string, string[]> abbrev in result)
{
if (txtinput.Text == abbrev.Value)
{
txtinput.Text = txtinput + "<<" + abbrev.Key + ">>";
}
}
You can start by creating a Stream Reader for your file, then looping for all your values in the CSV and add them to the dictionary.
static void Main(string[] args)
{
var csv_reader = new StreamReader(File.OpenRead(#"your_file_path"));
//declare your dictionary somewhere outside the loop.
while (!csv_reader.EndOfStream)
{
//read the line and split if you need to with .split('')
var line = reader.ReadLine();
//Add to the dictionary here
}
//Call another method for your search and replace.
SearchAndReplace(your_input)
}
Then have the implementation of that method, search if the input exists in the dictionary and if it does replace it.
You could use LINQ to put the values of the csv into your dictionary, if that's easier for you.
I'm going to assume that your input file may have commas in the actual text, and not just separating the two fields.
Now, if that were the case, then the standard CSV file format for format the file like this:
Laughing Out Loud,LOL
"I Came, I Saw, I Conquered",ICISIC
However, from your example you have a space before the "LOL", so it doesn't appear that you're using standard CSV.
So I'll work on this input:
Laughing Out Loud, LOL
"I Came, I Saw, I Conquered",ICISIC
"to, too, or two", 2
because,B/C
For this input then this code produces a dictionary:
var dictionary =
(
from line in File.ReadAllLines("FILE.CSV")
let lastComma = line.LastIndexOf(',')
let abbreviation = line.Substring(lastComma + 1).Trim()
let actualRaw = line.Substring(0, lastComma).Trim()
let actual = actualRaw.StartsWith("\"") && actualRaw.EndsWith("\"")
? actualRaw.Substring(1, actualRaw.Length - 2)
: actualRaw
select new { abbreviation, actual }
).ToDictionary(x => x.abbreviation, x => x.actual);
You can go one better than this though. It's quite possible to create a "super function" that will do all of the replaces in one go for you.
Try this:
var translate =
(
from line in File.ReadAllLines("FILE.CSV")
let lastComma = line.LastIndexOf(',')
let abbreviation = line.Substring(lastComma + 1).Trim()
let actualRaw = line.Substring(0, lastComma).Trim()
let actual = actualRaw.StartsWith("\"") && actualRaw.EndsWith("\"")
? actualRaw.Substring(1, actualRaw.Length - 2)
: actualRaw
select (Func<string, string>)(x => x.Replace(abbreviation, actual))
).Aggregate((f1, f2) => x => f2(f1(x)));
Then I can do this:
Console.WriteLine(translate("It was me 2 B/C ICISIC, LOL!"));
I get this result:
It was me to, too, or two because I Came, I Saw, I Conquered, Laughing Out Loud!

Extract a substring from a string in C#

How can I extract the substring "John Woo" from the below string in C#
CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com
Thanks !
You could use a Lookup<TKey, TElement>:
string text = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
var keyValues = text.Split(',')
.Select(s => s.Split('='))
.ToLookup(kv => kv[0], kv => kv.Last());
string cn = keyValues["CN"].FirstOrDefault(); // John Woo
// or, if multiple values with the same key are allowed (as suggested in the given string)
string dc = string.Join(",", keyValues["DC"]); // ABC,com
Note that you neither get an exception if the key is not present(as in a dictionary) nor if the key is not uniqe (as in a dictionary). The value is a IEnumerable<TElement>.
Try this
var regex = new Regex("CN=(?<mygroup>.*?),");
var match = regex.Match("CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com");
if(match.Success)
{
string result = match.Groups["mygroup"].Value;
}
Try this (this is a non generic answer) :
var name = str.Split(',').Where(n => n.StartsWith("CN=")).FirstOrDefault().Substring(3);
Something like this
var s = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
// this give you a enumarable of anonymous key/value
var v = s.Split(',')
.Select(x => x.Split('='))
.Select(x => new
{
key = x[0],
value = x[1],
});
var name = v.First().value; // John Woo
You can firstly split the string by the commas to get an array of strings, each of which is a name/value pair separated by =:
string input = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
var nameValuePairs = input.Split(new[] {','});
Then you can split the first name/value pair like so:
var nameValuePair = nameValuePairs[0].Split(new[]{'='});
Finally, the value part will be nameValuePair[1]:
var value = nameValuePair[1];
(No error handling shown above - you would of course have to add some.)
I created the below code of my own and finally got the substring I needed. The below code works for every substring that I want to extract that falls after "CN=" and before first occurrence of ",".
string name = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
int index1 = name.IndexOf("=") + 1;
int index2 = name.IndexOf(",") - 3;
string managerName = name.Substring(index1, index2);
The Result was "John Woo"
Thanks all for your help...

Getting values from a list using foreach

I have list that have values like"
[0] = "{ id = ES10209005, views = 501 }"
[1] = "{ id = HYT0209005, views = 5678}"
[3] = "{ id = POI0209005, views = 4568}"
I would like to pass the values(id,views) to a method using a for each loop.
method(id,views)
Something like:
foreach (string v in updatereponse)
{
method()
}
How do I isolate each value(id,views) from each row in the list then pass it to the method?
The list contains just a bunch of strings, anything based on this to fix the problem would be just a workaround (e.g. string parsing). You should really switch to a strongly typed model, e.g. define a class ViewCount:
public class ViewCount
{
public string Id {get;set;}
public int Views {get;set;}
}
You can then use a List<ViewCount> populate the list:
List<ViewCount> viewcounts = new List<ViewCount>();
viewCounts.Add(new ViewCount() { Id = "ES10209005", Views = 501 });
Since each ViewCount instance has Id and Views properties you can now do the proper thing:
foreach (var item in updatereponse)
{
method(item.Id, item.Views);
}
If you are saving this data in a file, an alternative would be to use XML instead of custom strings, then you could use Linq to XML to populate a List<ViewCount>, e.g. using a simple XML like this:
<ViewCounts>
<ViewCount id="ES10209005" views="501" />
</ViewCounts>
You can then load your list:
XElement viewXml = XElement.Load("test.xml");
List<ViewCount> viewCounts = viewXml.Descendants("ViewCount")
.Select(x => new ViewCount()
{
Id = (string)x.Attribute("id"),
Views = (int)x.Attribute("views")
}).ToList();
foreach (string v in updateresponse)
{
var values = v.Split(",");
var id = values[0].Replace("{ id = ", "").Trim();
var view = values[1].Replace("views = ", "").("}","").Trim();
method(id, value);
}
Here's another way...you may want to add error checking:
String Data = "{ id = ES10209005, views = 501 }";
String[] Segments = Data.Split(new char[] { ' ', ',' });
string ID = Segments[3];
int views = int.Parse(Segments[7]);
Assuming the structure of your String is like you showed us always, this can work for you.
// First split id and views part.
String[] firstSplit = v.Split(',');
// Get the respected value for each part.
String id = firstSplit[0].Split('=')[1].Trim();
String views = firstSplit[1].Split('=')[1].Trim().Replace("}","");
You can use String methods to retrieve the items (use Split and SubString for example) or you can use a regular expression.
E.g.
var list = UpdateResponse[0].Split("=,} ") ;
will result in a list split by all these characters (including space).
Then check the correct indices to use (use a debugger for that). Then you get something like:
var id = list[5];
var views = list[8];
(note: check the indices 5 or 8, they are just a guess).

Categories

Resources