|
You last visited: Today at 00:40
Advertisement
CSV Comma question
Discussion on CSV Comma question within the .NET Languages forum part of the Coders Den category.
02/22/2017, 08:32
|
#1
|
Trade Restricted
elite*gold: LOCKED
Join Date: Oct 2016
Posts: 321
Received Thanks: 79
|
CSV Comma question
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
|
#2
|
elite*gold: 0
Join Date: Feb 2011
Posts: 1,342
Received Thanks: 410
|
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
|
#3
|
Trade Restricted
elite*gold: LOCKED
Join Date: Oct 2016
Posts: 321
Received Thanks: 79
|
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
|
#4
|
elite*gold: 0
Join Date: Feb 2011
Posts: 1,342
Received Thanks: 410
|
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
|
#5
|
Trade Restricted
elite*gold: LOCKED
Join Date: Oct 2016
Posts: 321
Received Thanks: 79
|
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
|
#6
|
elite*gold: 15
Join Date: Jul 2010
Posts: 3,926
Received Thanks: 1,158
|
bring an example that says something instead of text.
|
|
|
02/23/2017, 14:36
|
#7
|
Trade Restricted
elite*gold: LOCKED
Join Date: Oct 2016
Posts: 321
Received Thanks: 79
|
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
|
#8
|
elite*gold: 0
Join Date: Aug 2005
Posts: 2
Received Thanks: 0
|
if you have static data structure, i recommend to use nuget. It helps a lot
|
|
|
02/27/2017, 22:25
|
#9
|
Trade Restricted
elite*gold: LOCKED
Join Date: Oct 2016
Posts: 321
Received Thanks: 79
|
Thanks for help, i already found how i'll do this
|
|
|
Similar Threads
|
Some saved because the sql not accept me the comma (, )
09/02/2016 - SRO Private Server - 6 Replies
Some saved because the sql not accept me the comma (, )
|
Vaga Comma Packet
06/25/2010 - Kal Online - 15 Replies
if (strcmp(input,"comma") ==0)
{
SendDetour(0x33,"d",596312067);
printf("Sent comma packet!");
}
if (strcmp(input,"vaga") ==0)
{
SendDetour(0x33,"d",130940929);
printf("Sent vaga packet!");
|
Selling 51 comma on hanin
06/26/2007 - Kal Online - 0 Replies
Hello , im selling 51 comma in hanin for geons , or i can change for vaga lvl 50 + . i got s/n , e mail , id , pw , evrything ^^ , pm me or post reply here
|
All times are GMT +2. The time now is 00:40.
|
|