CSV Comma question

02/22/2017 08:32 Underfisk#1
Hi, i was trying to figure out in loading CSV Files how to dont remove one of the things, because CSV is splited by comma but my main editor is oriented for a private server files and when he value is NULL they put the value as a comma.

For example :
115,1,NAME,1
,1,NAME,1
,2,NAME,1

So this means that , is the sub topic, and i wanna know how i can read that comma in values without split them and substitue with another letter or text.

Thanks
02/22/2017 12:01 xShizoidx#2
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication64
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> dataList = new List<string>();
            using (StreamReader reader = new StreamReader(@"C:\test\test.csv"))
            {
                while (!reader.EndOfStream)
                {
                    dataList.AddRange(reader.ReadLine().Split(','));
                }
            }

            for(int i=0;i<dataList.Count;i++)
            {
                if(dataList[i].Trim() == "")
                {
                    dataList[i] = ",";
                }
            }
        }
    }
}
02/22/2017 13:13 Underfisk#3
Thanks for help first of all.
Btw i'm trying to find the "" as you did but i wanna put as some character not as "," because when i save i put the number of , that a single line need so if there is a blank space, it ignores and put the necessary , and it works aswell.
My real question is when i'm reading i wanna find the , before split so i can substitue with another thing and see at textbox that temporary thing instead of blank space.

Thanks in advanced and i hope you understand my need xd.
02/22/2017 14:32 xShizoidx#4
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication64
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> dataList = new List<string>();
            using (StreamReader reader = new StreamReader(@"C:\test\test.csv"))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    string firstCharacter = line.Substring(0, 1);
                    if(firstCharacter == ",")
                    {
                        // your Code
                    }
                    dataList.AddRange(line.Split('\n'));
                }
            }
        }
    }
}
02/22/2017 18:53 Underfisk#5
Nah is not that tho.
I'll give you an advanced question.
In the first you gave me, you check if is blank and put a ,.
In the second one, you check the first one, but i dont wanna check the first one because in the middle, in the final and everywhere i can put a , and in my textbox it shows as null

So here is an official CSV Lines and what they mean, i'll try to explain you so you probably know why i wanna do.

Quote:
ImageIndex,Count,ItemIndex,cash
So in imageindex i can put a number but the sub's that use the same they will be after and will use the , as the value.
the same to count, they will use the , as a value.
So my real question is how i can find in every position of the line (reading every split) and check if the value there is, if is a , but when im spliting it deletes the , from occasion and it fully ignores my condition and when CSV loads, in textbox it will assume a null value and stay blank.

Idk if i had explained well but i think you can understand better than before
02/23/2017 12:35 .Scy#6
bring an example that says something instead of text.
02/23/2017 14:36 Underfisk#7
Alright here it goes.
Example : Reading a csv

Read CSV and put the index at listbox
Quote:
public Boolean getCSV(string actualPath, List<string> temp, ListBox name, string nameToDif)
{
try
{
FileStream fs = new FileStream(actualPath, FileMode.Open, FileAccess.Read);
StreamReader str = new StreamReader(fs);
string line;
while (!str.EndOfStream)
{
line = str.ReadLine();
temp.Add(line);
}
str.Close();


//Add index to ListBox
foreach (string l in temp)
{
string[] mobs = l.Split(',');
if (mobs[1] != nameToDif)
name.Items.Add(mobs[1]);
}
return true;
}
catch (Exception ex)
{
MessageBox.Show("The file is currently being used. Please close the file to being open here!" + ex.ToString(),"File is busy" + MessageBoxIcon.Error + MessageBoxButtons.OK);
return false;
}


When it loads the CSV for example, itemlist that its here :
Quote:
ImageIndex,Count,ItemIndex,cash,String,Description ,Waring,Packageitemindex,Packageitemcash,
115,5,2310041,2000,0,15Days,1,1170007,15000,
,,62729,7000,0,15Days,0,0,0,
,,2310040,7500,0,15Days,0,0,0,
It will show as blank the 1st value in textbox imageindex, at the subs after 155 and i wanna instead of , because the blank value is behind i wanna add something such as sub or comma, so when i save i just need to check if is sub or comma and put a blank value but for the tool editor its better the developer know its a comma or a sub value
02/27/2017 16:26 stepux#8
if you have static data structure, i recommend to use [Only registered and activated users can see links. Click Here To Register...] nuget. It helps a lot :)
02/27/2017 22:25 Underfisk#9
Thanks for help, i already found how i'll do this :)