c#


Cannot store csv file into a dictionary in its original format


Have a csv file in the following format:
a,b
Goal is to store this csv file in a dictionary
Problem: csvOne has this as first field
kfjdfdsdsd, second value
aaaaaaa
sdasdasdaasdasdfffw
as a result it does not get stored in it's original format, i.e. only the below part gets stored:
key: "", value: kfjdfdsdsd
My code:
public void StoreInDictionary(string[] file, Dictionary<string, string> dictionary)
{
foreach (var line in file)
{
var cleansedLine = Regex.Replace(line, #"\s+", "");
var commaIndex = cleansedLine.IndexOf(',');
var valueOne = cleansedLine.Substring(0, commaIndex + 1);
var valueTwo = cleansedLine.Substring(commaIndex + 1);
if (!dictionary.ContainsKey(valueOne))
{
dictionary.Add(valueOne, valueTwo);
}
}
}
p.s. tried Replace \r\n too, did not work
Thanks a lot
For output as:
key value
kfjdfdsdsd second value
aaaaaaa (blank)
sdasdasdaasdasdfffw (blank)
use below code
public void StoreInDictionary(string[] file, Dictionary<string, string> dictionary)
{
foreach (var line in file)
{
var cleansedLine = Regex.Replace(line, #"\s+", "");
var commaIndex = cleansedLine.IndexOf(',');
string valueOne = String.Empty;
string valueTwo = String.Empty;
if (commaIndex > 0)
{
valueOne = cleansedLine.Substring(0, commaIndex + 1);
valueTwo = !String.IsNullOrWhiteSpace(cleansedLine.Substring(commaIndex + 1)) ?
cleansedLine.Substring(commaIndex + 1) :
String.Empty;
}
if (!dictionary.ContainsKey(valueOne))
{
dictionary.Add(valueOne, valueTwo);
}
}
}
This should give you what you want. Just a quick note, you might have line with multiple "," my solution doesn't deal with that.
public void StoreInDictionary(string[] file, Dictionary<string, string> dictionary)
{
foreach (var line in file)
{
if (!string.IsNullOrWhiteSpace(line))
{
string valueOne, valueTwo;
var idx = line.IndexOf(',');
if (idx >= 0)
{
valueOne = line.Substring(0, idx);
valueTwo = line.Substring(idx + 1);
}
else
{
valueOne = line;
valueTwo = string.Empty;
}
if (!dictionary.ContainsKey(valueOne))
{
dictionary.Add(valueOne, valueTwo);
}
}
}
}

Related Links

Generic method in C#
tcp having trouble connecting c#
Are there commonly used delegates available in the standard class libraries?
How to set website theme to all webpages using a drop down list?
How to set selectedcolor of ColorCanvas by using Argb in code behind
How to set a transition property using Transitionals library
How to limit textbox maxlength by byte in winrt?
Entity Framework 6 database migrations for isolated multi-tenant setup
File upload / did I make a mistake choosing vb.net instead of php?
Find data in database
Calling sharepoint from web service
Baidu web search engine API
Searching for records on one exact day
Must declare the scalar variable “#ID” for insert parameter
How to get an Image from facebook and use ImageResizer MVC4 & C#
Launch another application [duplicate]

Categories

HOME
ide
sql-server-2014
oracle-sqldeveloper
fortran
weblogic
display
file-upload
latex
msbi
kendo-asp.net-mvc
browserstack
osx-yosemite
promotions
dcos
aws-sdk-cpp
facebook-sharer
fine-uploader
android-notifications
responsive
alert
airbnb
views
smali
onclicklistener
uipath
scrapy-spider
jira-zephyr
priority-queue
hierarchical-data
dynamics-ax-2012-r3
semantic-ui-react
exacttarget
slim-lang
minitest
aweber
motherboard
stochastic-process
form-fields
mockserver
azure-web-roles
irr
colorbox
intervention
api-key
azure-data-catalog
multivalue
jcs
dna-sequence
jcifs
nservicebus6
google-scholar
visual-web-developer
instaparse
recurring
github-for-mac
opendolphin
app-data
hexagonal-tiles
sparkcore
clearquest
tidesdk
maven-archetype
beagleboard
dojox.grid
launcher
clearcanvas
worklight-appcenter
code-smell
nodeunit
php-amqplib
source-depot
codeigniter-form-helper
mysql2
spawn
architectural-patterns
ftpwebrequest
generator-expression
brew
metapost
wpml
certificate-revocation
event-propagation
flatten
diazo
stl-algorithm
cherokee
marathontesting
javah
azure-appfabric
p4.net
content-delivery-network
high-load

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App