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

log4net log file is not getting created in C#
Save image to web server from window application
Data not displaying in gmail attachment
How can I name a namespace dedicated to span just classes holding exclusively static fields? [closed]
.net core identity change tables names
Visual Studio - 'View Code' not working?
in nested root node how to delete one node particularly
Signalr 2 disconnects on method to ListAll in table, but can insert into table with Create method
parse file in C# with Regex and search lines
adding new property to asp.net identity role [duplicate]
Remove label for in div
Email confirmation link using office 365 in ASP.NET MVC [duplicate]
Calculate the size of a shadow
Call a web service from a desktop application in c#
Get members of custom class to read their values
Dynamically include/exclude fields in where clause LINQ

Categories

HOME
oop
ftp
grpc
phantomjs
ibm
latex
opencv4android
nagios
cryengine
azure-resource-manager
opengl-es
msbi
aggregation-framework
swap
country-codes
obd-ii
software-collections
rename
google-search-appliance
popup
responsive
taxonomy
geography
hapijs
aiml
hololens
javaagents
waterfall
suitescript
oracle10g
rancher
adobe-captivate
gulp-jshint
large-files
launchd
apm
keil
rtos
flickr
angularjs-resource
perf
respect-validation
android-databinding
instances
jquery-inputmask
google-chrome-console
delphi-xe
glide-image-library
java-5
amazon-rds-aurora
npoco
serverless-architecture
rivets.js
denodo
serialversionuid
beanshell
sp
dmalloc
grib
wicketstuff
iban
zsh-completion
alfred
rowname
geokit
syslog4j
android-xmlpullparser
vitamio
user-profile
pebble-js
treelist
radar-chart
methodology
web-performance
encode
javapackager
glog
waterline
genome
sicstus-prolog
pymol
disparity-mapping
xts
nest-initiative
django-tests
code-smell
connections
autofilter
wpa
dd4t
proj4
removeall
bho
ftpwebrequest
dnsbl
configurationsection
doxia
derived-class
diazo
motorola-droid
stl-algorithm
ruby-1.9
visual-leak-detector
virtual-printer
xhtmlrenderer
conditional-operator
pitch-shifting
browser-state
carbon-emacs
focus-stealing
qlibrary

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