如何从我的输出中删除引号(“"”) [英] how to delete the inverted commas(" ") from my out put

查看:95
本文介绍了如何从我的输出中删除引号(“"”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我必须将一个csv文件加载到DataGridView并在我的csv文件中我没有任何引号()但当我尝试在我的DataGridView中加载它时我自动获取引号(),我不知道为什么会得到它们。



  private   void  Load_Click( object  sender,EventArgs e)
{
string delimiter = ;
string tablename = export;
string filename =( c:\\ \\\izaz\\test.txt);


DataSet dataset = new DataSet();
StreamReader sr = new StreamReader(filename);

dataset.Tables.Add(tablename);
dataset.Tables [tablename] .Columns.Add( Name);
dataset.Tables [tablename] .Columns.Add( Path);

string allData = sr.ReadToEnd();
string [] rows = allData.Split( \r .ToCharArray());

foreach string r in rows)
{
string [] items = r.Split(delimiter.ToCharArray());
dataset.Tables [tablename] .Rows.Add(items);
}
.dataGridView1.DataSource = dataset.Tables [ 0 ]。默认视图;

foreach (DataGridViewColumn列 in dataGridView1.Columns)
列.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;


}







out put看起来像这样:



名称路径
AWServer_3.1 / vbnrt735w6 / sdfasd34564 / AWServer_3.1
CCW5.1.1_DEMO / CCW5.1.1_DEMO
DEMO-EA / 4564 / DEMO-EA
MUSE_DEMO / vbnrt735w6 / MUSE_DEMO
UV_CARDIO / asd / UV_CARDIO
UView-Web_Cardio_2015_v2 / UView-Web_Cardio_2015_v2

解决方案

< blockquote>

你的问题的原因是你的字符串中有控制字符,即换行符。尝试这样做。

  char  delimiter = ' ,' ; 
string tablename = export;
string filename =( c:\\ \\\izaz\\test.txt);


DataSet dataset = new DataSet();
dataset.Tables.Add(tablename);
dataset.Tables [tablename] .Columns.Add( Name);
dataset.Tables [tablename] .Columns.Add( Path);

string [] rows = File.ReadAllLines(filename);
foreach string r in 行)
{
string [] items = r.Split(delimiter);
dataset.Tables [tablename] .Rows.Add(items);
}


更改



  string  [] items = r.Split(delimiter.ToCharArray()); 





to



  string  [] items = r .Replace(  \ string  .Empty)。Split(delimiter.ToCharArray()); 


小心CSV格式的数据。



  1. 请注意,CSV没有官方标准。
  2. 如果您的意思是带有引号的双引号,那么请注意它们是否有效目的:引号之间的字符串可能包含分隔符和引号字符(主要通过加倍引号字符来转义)。



例如3列CSV可能如下(假设逗号为字段分隔符):

 a,b,c 
d ,,
这不是分隔符:,,这是带引号的字符串中的,空格也可能是字段值$ b的一部分$ b ,,引用字符串周围的空格也可以作为字段值的一部分,



字段中甚至可能有新行,通常在引用的字符串中。



如果您的目标是读取由任何类型的程序生成的任何CSV文件,那么您必须使用适当的CSV解析器。该解析器将处理所有特殊情况以及删除和取消转义引号字符等。



如果您已生成CSV文件您可以完全控制,然后知道如何根据需要简化通用CSV语法,例如通过声明没有分隔符,换行符或引号都不是你的字段的一部分,并且所有字段都有内容等。如果给出第二种情况,你可以使用简单的 string.Split 功能。



干杯

Andi


In my application i have to load a csv file to DataGridView and in my csv file i dont have any inverted commas(" ") but when i try to load it in my DataGridView am automatically getting inverted commas (" "), I have no idea why am getting them.

private void Load_Click(object sender, EventArgs e)
        {
            string delimiter = ",";
            string tablename = "export";
            string filename = ("c:\\izaz\\test.txt");


            DataSet dataset = new DataSet();
            StreamReader sr = new StreamReader(filename);

            dataset.Tables.Add(tablename);
            dataset.Tables[tablename].Columns.Add("Name");
            dataset.Tables[tablename].Columns.Add("Path");

            string allData = sr.ReadToEnd();
            string[] rows = allData.Split("\r".ToCharArray());

            foreach (string r in rows)
            {
                string[] items = r.Split(delimiter.ToCharArray());
                dataset.Tables[tablename].Rows.Add(items);
            }
            this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
        
            foreach (DataGridViewColumn column in dataGridView1.Columns)
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            
           
        }




out put looks like this:

  Name                                     Path     
"AWServer_3.1"	              "/vbnrt735w6/sdfasd34564/AWServer_3.1"
"CCW5.1.1_DEMO"               "/CCW5.1.1_DEMO"
"DEMO-EA"                     "/4564/DEMO-EA"
"MUSE_DEMO"                   "/vbnrt735w6/MUSE_DEMO"
"UV_CARDIO"                   "/asd/UV_CARDIO"
"UView-Web_Cardio_2015_v2"    "/UView-Web_Cardio_2015_v2"

解决方案

The reason for your problem is that you have control characters in your strings, namely the new line character.Try doing it this way.

char delimiter = ',';
string tablename = "export";
string filename = ("c:\\izaz\\test.txt");


DataSet dataset = new DataSet();
dataset.Tables.Add(tablename);
dataset.Tables[tablename].Columns.Add("Name");
dataset.Tables[tablename].Columns.Add("Path");

string[] rows = File.ReadAllLines(filename);
foreach (string r in rows)
{
    string[] items = r.Split(delimiter);
    dataset.Tables[tablename].Rows.Add(items);
}


Change

string[] items = r.Split(delimiter.ToCharArray());



to

string[] items = r.Replace("\"", string.Empty).Split(delimiter.ToCharArray());


Be careful with CSV formatted data.


  1. Be aware that there is no official standard for CSV.
  2. If you mean double quotes with "inverted commas", then be also aware that they serve a certain purpose: the string between the quotes may contain the separator character as well as quotes character (mostly escaped by doubling the quotes character).


E.g. a 3-column CSV may be as follows (assuming comma as field separator):

a,b,c
d,,
"this is not a separator: ,", this is a "" in a quoted string",   spaces    may also be part of the field value
,,  "spaces around a quoted string may also be taken as part of the filed value"  ,


There may even be a new-line in a field, typically in a quoted string.

If you aim to read any CSV file that was produced by any kind of program, then you have to employ a proper CSV parser. That parser would take care of all the special cases as well as of removing and un-escaping quote characters, etc.

If you have the production of the CSV file under your full control, then you know how you may simplify the general purpose CSV grammar for your needs, e.g. by asserting that no separators nor newlines nor quotes are ever part of your fields and that all fields have content, etc. If the second case is given, you may employ the simple string.Split function.

Cheers
Andi


这篇关于如何从我的输出中删除引号(“&quot;”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆