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

AJAX Panel not working on initial global RouteTable
Entity Framework, strange behaviour with required reference, lazy loading
use a ListBox to Filter Datagrid
regasm.exe is not a valid win32 application
How can i pass parameter from javascript to generic handler?
Task<WebResponse>.Wait lasts forever
How to keep a .NET Socket object connection open after postback?
How To send datagrid in function
Can I call functions of an Excel addin in C# project?
How do I know when an Image is loaded in Picturebox
Modify the content of the home page programmatically in SharePoint 2010
How do you fire the click event of a iwc:ImageButton in Jquery?
How to get type of the custom UserControl?
Returning to top of program to try again
What pattern is being done in the Global.asax.cs file in ASP.NET MVC 4?
Umbraco 4.8, Document save Publish

Categories

HOME
asterisk
stanford-nlp
pyspark
casting
intellisense
dynamic
install
oms
mamp
guidewire
jetbrains
expo
hdf5
jsfiddle
geography
audio-streaming
bootstrap-datetimepicker
computation-theory
fibonacci
functional-dependencies
chartist.js
my.cnf
oracle10g
smart-device
large-files
restangular
arp
ng2-bootstrap
snapkit
flickr
matlab-compiler
cocoa-scripting
ansible-inventory
form-fields
motion
jquery-inputmask
mybatis-generator
ng-lightning
icu
rbac
test-kitchen
serverless-architecture
runtime-permissions
webmatrix
totalview
dynatable
robotc
breadcrumbs
dimensionality-reduction
disconnect
git-cherry-pick
cexception
nslocalizedstring
dday
maven-antrun-plugin
updating
openhab
callfire
cakephp-2.6
log-analysis
gcc-warning
screen-lock
information-hiding
grunt-contrib-connect
nsurlrequest
lwuit
pnunit
sat4j
debuggervisualizer
farpoint-spread
data-layers
kuka-krl
adabas
xml-validation
sygic
nsnumber
violation
friendly-url
simplemembership
nsregularexpression
quit
cryptarithmetic-puzzle
webpage-screenshot
joomla1.7
nsundomanager
jexcelapi
cpack
chars
simultaneous
rpxnow
time-management
opengl-to-opengles

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