如何在C#中使用一个.csv文件创建具有不同名称的文件 [英] How to create files with different names by using in one .csv file in C#

查看:61
本文介绍了如何在C#中使用一个.csv文件创建具有不同名称的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有.csv文件。我想创建文件使用此文件。 这个
是一个我的.csv文件的一小部分。我的原始.csv文件中有60000多行。

 Date,Pass,CutMark1,Marks1,CutMark2, Marks2,
22/06/2017 13:04:18,FALSE,TRUE,40,FALSE,35,
22/06/2017 13:04:20,FALSE,TRUE,35,FALSE, 35,
22/06/2017 13:04:35,FALSE,TRUE,55,FALSE,55,
22/06/2017 13:04:37,FALSE,TRUE,100,FALSE, 55,
22/06/2017 13:04:37,FALSE,TRUE,38,FALSE,55,
22/06/2017 13:04:38,FALSE,FALSE,35,FALSE, 55,
22/06/2017 13:04:39,FALSE,FALSE,35,FALSE,38,
22/06/2017 13:04:40,FALSE,FALSE,35,TRUE, 38,
22/06/2017 13:04:41,FALSE,FALSE,55,TRUE,38,
22/06/2017 13:04:42,FALSE,FALSE,55,TRUE, 55,
22/06/2017 13:04:44,FALSE,FALSE,38,TRUE,55,
22/06/2017 13:04:45, TRUE,FALSE,38,TRUE,40,
22/06/2017 13:04:46,TRUE,FALSE,38,TRUE,40,
22/06/2017 13:04:48, TRUE,FALSE,55,FALSE,40,
22/06/2017 13:04:49,TRUE,FALSE,55,FALSE,25,
22/06/2017 13:04:50, TRUE,FALSE,55,FALSE,45,
22/06/2017 13:04:52,FALSE,TRUE,55,FALSE,60,
22/06/2017 13:04:53, FALSE,TRUE,40,FALSE,80,
22/06/2017 13:04:54,FALSE,TRUE,40,FALSE,80,
22/06/2017 13:04:56, FALSE,TRUE,40,FALSE,75,
22/06/2017 13:04:57,FALSE,TRUE,40,FALSE,90,
22/06/2017 13:04:58, FALSE,TRUE,55,FALSE,88,
22/06/2017 13:05:00,TRUE,TRUE,55,TRUE,23,
22/06/2017 13:05:01, TRUE,TRUE,55,TRUE,45,
22/06 05:02,TRUE,TRUE,20,TRUE,78,
22/06/2017 13:05:04, TRUE,TRUE,45,TRUE,45,
22/06/2017 13:05:05,TRUE,TRUE,85,TRUE,69,
22/06/2017 13:05:06, TRUE,TRUE,62,TRUE,45,
22/06/2017 13:05:08,TRUE,TRUE,100,TRUE,35,


我想保存这些文件,如下所示。在这里"通过"列想要检查..有假,然后是真假后
然后再次真等等所以我想保存文件作为一个例子,我想保存这个文件首先遇到False值为False1.csv然后再次True1.csv然后再次遇到False2.csv就像我想在文件名中添加数字那个True
或多次冒错。我希望原始文件保存此.csv文件中的所有其他列。


我用这个例子为我解释,我想首先保存所有false
和不同文件中的真值。我得到了这个例子。在这里,前11行的Pass列是"False"所以我想要保存一个文件中的那些(1-11)原始文件应保存为False1.csv,然后满足传递"True"有原始(12-16)应该
保存为True1.csv ..然后传递值再次满足"False"然后保存那些原始(17-22)为False2.csv再次满足传递列是"True"然后原始(23-29)保存为True2.csv,同样我要保存,我有60000原始在我的.csv文件中。



这是我读取.csv文件的代码。请帮我这样做。谢谢。

使用System.Globalization; 

公共静态字符串dateFormatString =" dd / MM / yyyy HH:mm:ss" ;;


private void btnSeperateFile_Click(object sender,EventArgs e)
{


if(tbOutputFilePath2!= null)
{

List< DrawNew> ; ObservingData = new List< DrawNew>(); //用于存储CSV中所有可用DrawNew对象的列表

//循环CSV
foreach中的每一行(系统中的字符串行) .IO.File.ReadAllLines(outputFilePath.Text).Skip(1))//。Skip(1)用于跳过标题
{

string [] valuesCsvLine = line.Split( ',');
DrawNew mngInstance = new DrawNew();

mngInsta nce.Date = DateTime.ParseExact(valuesCsvLine [0],dateFormatString,CultureInfo.InvariantCulture);
mngInstance.Pass =(valuesCsvLine [1] ==" TRUE"?true:false);
mngInstance.CutMark1 =(valuesCsvLine [2] ==" TRUE"?true:false);
mngInstance.Marks1 = int.Parse(valuesCsvLine [3]);
mngInstance.CutMark2 =(valuesCsvLine [4] ==" TRUE"?true:false);
mngInstance.Marks2 = int.Parse(valuesCsvLine [5]);

ObserveData.Add(mngInstance);

}
}
}


这是用于存储.csv文件数据的类

类DrawNew 
{
public DateTime Date {get;组; } //要存储的属性日期
public bool Pass {get;组; } //存储ChargeMode的属性
public bool CutMark1 {get;组; } //存储ChargeMode的属性
public int Marks1 {get;组; } //存储的属性ScheduleStep
public bool CutMark2 {get;组; } //存储ChargeMode的属性
public int Marks2 {get;组; } //存储的属性ScheduleStep
}








解决方案

Hi Premathilake,


请参考以下完整项目:

 public partial class FrmSaveCsvFiles:表格
{
public FrmSaveCsvFiles()
{
InitializeComponent();
}

private void Form2_Load(object sender,EventArgs e)
{

}

public static string dateFormatString = "dd / MM / yyyy HH:mm:ss";
公共字符串文件路径= @" D:\Test \Test \A.csv" ;;

private void button1_Click(object sender,EventArgs e)
{
if(filepath!= null)
{
//列出所有可用的列表CSV
List< DrawNew>中的DrawNew对象ObservingData = new List< DrawNew>();
//循环CSV
foreach中的每一行(File.ReadAllLines(filepath)中的字符串行.Skip(1))// .Skip(1)用于跳过标题
{
string [] valuesCsvLine = line.Split(',');
DrawNew mngInstance = new DrawNew();
mngInstance.Date = DateTime.ParseExact(valuesCsvLine [0],dateFormatString,CultureInfo.InvariantCulture);
mngInstance.Pass =(valuesCsvLine [1] ==" TRUE"?true:false);
mngInstance.CutMark1 =(valuesCsvLine [2] ==" TRUE"?true:false);
mngInstance.Marks1 = int.Parse(valuesCsvLine [3]);
mngInstance.CutMark2 =(valuesCsvLine [4] ==" TRUE"?true:false);
mngInstance.Marks2 = int.Parse(valuesCsvLine [5]);

ObservingData.Add(mngInstance);
}

string FilesPath = @" D:\Test \Test \" ;;

列表< DrawNew> Files = new List< DrawNew>();
列表< DrawNew> TrueFile = new List< DrawNew>();

int lastCount = 0;
int falseCount = 0;
int tureCount = 0;
Files.Add(ObservingData [0]);
for(int i = 1; i< ObservingData.Count; i ++)
{
if(ObservingData [i] .Pass == ObservingData [i - 1] .Pass)
{
Files.Add(ObservingData [i]);
}
else
{
string fileName = ObservingData [i - 1] .Pass.ToString();
if(ObservingData [i - 1] .Pass == false)
{
falseCount ++;
if(Files.Count> 0)
{
using(StreamWriter swOutputFile = new StreamWriter(FilesPath + fileName + falseCount +" .csv"))
{
foreach(文件中的DrawNew dn)
{
swOutputFile.WriteLine(String.Format(" {0},{1},{2},{3},{4},{5 }",dn.Date,dn.Pass,dn.CutMark1,dn.Marks1,dn.CutMark2,dn.Marks2));
}
}
}
}
其他
{
tureCount ++;
if(Files.Count> 0)
{
using(StreamWriter swOutputFile = new StreamWriter(FilesPath + fileName + tureCount +" .csv"))
{
foreach(文件中的DrawNew dn)
{
swOutputFile.WriteLine(String.Format(" {0},{1},{2},{3},{4},{5 }",dn.Date,dn.Pass,dn.CutMark1,dn.Marks1,dn.CutMark2,dn.Marks2));
}
}
}
}

Files.Clear();
Files.Add(ObservingData [i]);
}
}
if(ObservingData [ObservingData.Count - 1] .Pass == false)
{
lastCount = falseCount + 1;
}
其他
{
lastCount = tureCount + 1;
}
string fileName2 = ObservingData [ObservingData.Count - 1] .Pass.ToString();
if(Files.Count> 0)
{
using(StreamWriter swOutputFile = new StreamWriter(FilesPath + fileName2 + lastCount +" .csv"))
{
foreach(文件中的DrawNew dn)
{
swOutputFile.WriteLine(String.Format(" {0},{1},{2},{3},{4},{5 }",dn.Date,dn.Pass,dn.CutMark1,dn.Marks1,dn.CutMark2,dn.Marks2));
}
}
}
}
MessageBox.Show(" Done");
}
}
类DrawNew
{
public DateTime Date {get;组; } //要存储的属性日期
public bool Pass {get;组; } //存储ChargeMode的属性
public bool CutMark1 {get;组; } //存储ChargeMode的属性
public int Marks1 {get;组; } //存储的属性ScheduleStep
public bool CutMark2 {get;组; } //存储ChargeMode的属性
public int Marks2 {get;组; } //存储的属性ScheduleStep
}


希望这会有所帮助!


最好的问候,


斯坦利


I have a .csv file.I want to create files by using this file.This is a small part of my .csv file.I have 60000 more rows in my original .csv file.

Date,Pass,CutMark1,Marks1,CutMark2,Marks2,
22/06/2017 13:04:18,FALSE,TRUE,40,FALSE,35,
22/06/2017 13:04:20,FALSE,TRUE,35,FALSE,35,
22/06/2017 13:04:35,FALSE,TRUE,55,FALSE,55,
22/06/2017 13:04:37,FALSE,TRUE,100,FALSE,55,
22/06/2017 13:04:37,FALSE,TRUE,38,FALSE,55,
22/06/2017 13:04:38,FALSE,FALSE,35,FALSE,55,
22/06/2017 13:04:39,FALSE,FALSE,35,FALSE,38,
22/06/2017 13:04:40,FALSE,FALSE,35,TRUE,38,
22/06/2017 13:04:41,FALSE,FALSE,55,TRUE,38,
22/06/2017 13:04:42,FALSE,FALSE,55,TRUE,55,
22/06/2017 13:04:44,FALSE,FALSE,38,TRUE,55,
22/06/2017 13:04:45,TRUE,FALSE,38,TRUE,40,
22/06/2017 13:04:46,TRUE,FALSE,38,TRUE,40,
22/06/2017 13:04:48,TRUE,FALSE,55,FALSE,40,
22/06/2017 13:04:49,TRUE,FALSE,55,FALSE,25,
22/06/2017 13:04:50,TRUE,FALSE,55,FALSE,45,
22/06/2017 13:04:52,FALSE,TRUE,55,FALSE,60,
22/06/2017 13:04:53,FALSE,TRUE,40,FALSE,80,
22/06/2017 13:04:54,FALSE,TRUE,40,FALSE,80,
22/06/2017 13:04:56,FALSE,TRUE,40,FALSE,75,
22/06/2017 13:04:57,FALSE,TRUE,40,FALSE,90,
22/06/2017 13:04:58,FALSE,TRUE,55,FALSE,88,
22/06/2017 13:05:00,TRUE,TRUE,55,TRUE,23,
22/06/2017 13:05:01,TRUE,TRUE,55,TRUE,45,
22/06/2017 13:05:02,TRUE,TRUE,20,TRUE,78,
22/06/2017 13:05:04,TRUE,TRUE,45,TRUE,45,
22/06/2017 13:05:05,TRUE,TRUE,85,TRUE,69,
22/06/2017 13:05:06,TRUE,TRUE,62,TRUE,45,
22/06/2017 13:05:08,TRUE,TRUE,100,TRUE,35,

I want to save this files as follows.In here "Pass" column want to check..there is False then True after that False then again True and etc So i want to save files by using this.As an example i want to save this file first meet False values as False1.csv then secondly True1.csv then again meet False2.csv like that I want to add numbers into the file name which that True or False meet in how many times.I want that raws save particular all other column in this .csv file.

I use this example for explain for me,I want to save first all false and true values in different file.I get this  example for that.In here first 11 rows of Pass column is "False"So I want to save those (1-11 )raws in one file should save as False1.csv,then meet pass "True" there are raws (12-16) should save as True1.csv..then Pass value has again meet "False" then save those raws(17-22) as False2.csv again meet pass column is "True" then that raws(23-29)save as True2.csv,likewise I want to save,I have 60000 raws in my .csv file.

This is my code for reading .csv file.please help me to do this.thank you.

using System.Globalization;

  public static string dateFormatString = "dd/MM/yyyy HH:mm:ss";


  private void btnSeperateFile_Click(object sender, EventArgs e)
        {
          

            if (tbOutputFilePath2 != null)
            {

                List<DrawNew> ObservingData = new List<DrawNew>(); // List to store all available DrawNew objects from the CSV

                // Loops through each lines in the CSV
                foreach (string line in System.IO.File.ReadAllLines(outputFilePath.Text).Skip(1)) // .Skip(1) is for skipping header
                {

                    string[] valuesCsvLine = line.Split(',');
                    DrawNew mngInstance = new DrawNew();

                    mngInstance.Date = DateTime.ParseExact(valuesCsvLine[0], dateFormatString, CultureInfo.InvariantCulture); 
                    mngInstance.Pass = (valuesCsvLine[1] == "TRUE" ? true : false);
                    mngInstance.CutMark1 = (valuesCsvLine[2] == "TRUE" ?true: false);
                    mngInstance.Marks1 = int.Parse(valuesCsvLine[3]);                 
                    mngInstance.CutMark2 = (valuesCsvLine[4] == "TRUE" ? true : false);
                    mngInstance.Marks2 = int.Parse(valuesCsvLine[5]); 

                    ObserveData.Add(mngInstance);

                    }            
                }
            }

This is the class for storing data of .csv file

class DrawNew
    {
        public DateTime Date { get; set; } // property to store Date 
        public bool Pass { get; set; } // property to store ChargeMode
        public bool CutMark1 { get; set; } // property to store ChargeMode
        public int Marks1 { get; set; } // property to store ScheduleStep
        public bool CutMark2 { get; set; } // property to store ChargeMode
        public int Marks2 { get; set; } // property to store ScheduleStep 
     }



解决方案

Hi Premathilake,

Please refer to the following complete project:

    public partial class FrmSaveCsvFiles : Form
    {
        public FrmSaveCsvFiles()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        public static string dateFormatString = "dd/MM/yyyy HH:mm:ss";
        public string filepath = @"D:\Test\Test\A.csv";

        private void button1_Click(object sender, EventArgs e)
        {
            if (filepath != null)
            {
                // List to store all available DrawNew objects from the CSV
                List<DrawNew> ObservingData = new List<DrawNew>();
                // Loops through each lines in the CSV
                foreach (string line in File.ReadAllLines(filepath).Skip(1)) // .Skip(1) is for skipping header
                {
                    string[] valuesCsvLine = line.Split(',');
                    DrawNew mngInstance = new DrawNew();
                    mngInstance.Date = DateTime.ParseExact(valuesCsvLine[0], dateFormatString, CultureInfo.InvariantCulture);
                    mngInstance.Pass = (valuesCsvLine[1] == "TRUE" ? true : false);
                    mngInstance.CutMark1 = (valuesCsvLine[2] == "TRUE" ? true : false);
                    mngInstance.Marks1 = int.Parse(valuesCsvLine[3]);
                    mngInstance.CutMark2 = (valuesCsvLine[4] == "TRUE" ? true : false);
                    mngInstance.Marks2 = int.Parse(valuesCsvLine[5]);

                    ObservingData.Add(mngInstance);
                }

                string FilesPath = @"D:\Test\Test\";

                List<DrawNew> Files = new List<DrawNew>();
                List<DrawNew> TrueFile = new List<DrawNew>();

                int lastCount = 0;
                int falseCount = 0;
                int tureCount = 0;
                Files.Add(ObservingData[0]);
                for (int i = 1; i < ObservingData.Count; i++)
                {
                    if (ObservingData[i].Pass == ObservingData[i - 1].Pass)
                    {
                        Files.Add(ObservingData[i]);
                    }
                    else
                    {
                        string fileName = ObservingData[i - 1].Pass.ToString();
                        if (ObservingData[i - 1].Pass == false)
                        {
                            falseCount++;
                            if (Files.Count > 0)
                            {
                                using (StreamWriter swOutputFile = new StreamWriter(FilesPath + fileName + falseCount + ".csv"))
                                {
                                    foreach (DrawNew dn in Files)
                                    {
                                        swOutputFile.WriteLine(String.Format("{0},{1},{2},{3},{4},{5}", dn.Date, dn.Pass, dn.CutMark1, dn.Marks1, dn.CutMark2, dn.Marks2));
                                    }
                                }
                            }
                        }
                        else
                        {
                            tureCount++;
                            if (Files.Count > 0)
                            {
                                using (StreamWriter swOutputFile = new StreamWriter(FilesPath + fileName + tureCount + ".csv"))
                                {
                                    foreach (DrawNew dn in Files)
                                    {
                                        swOutputFile.WriteLine(String.Format("{0},{1},{2},{3},{4},{5}", dn.Date, dn.Pass, dn.CutMark1, dn.Marks1, dn.CutMark2, dn.Marks2));
                                    }
                                }
                            }
                        }

                        Files.Clear();
                        Files.Add(ObservingData[i]);
                    }
                }
                if (ObservingData[ObservingData.Count - 1].Pass == false)
                {
                    lastCount = falseCount + 1;
                }
                else
                {
                    lastCount = tureCount + 1;
                }
                string fileName2 = ObservingData[ObservingData.Count - 1].Pass.ToString();
                if (Files.Count > 0)
                {
                    using (StreamWriter swOutputFile = new StreamWriter(FilesPath + fileName2 + lastCount + ".csv"))
                    {
                        foreach (DrawNew dn in Files)
                        {
                            swOutputFile.WriteLine(String.Format("{0},{1},{2},{3},{4},{5}", dn.Date, dn.Pass, dn.CutMark1, dn.Marks1, dn.CutMark2, dn.Marks2));
                        }
                    }
                }
            }
            MessageBox.Show("Done");
        }
    }
    class DrawNew
    {
        public DateTime Date { get; set; } // property to store Date 
        public bool Pass { get; set; } // property to store ChargeMode
        public bool CutMark1 { get; set; } // property to store ChargeMode
        public int Marks1 { get; set; } // property to store ScheduleStep
        public bool CutMark2 { get; set; } // property to store ChargeMode
        public int Marks2 { get; set; } // property to store ScheduleStep 
    }

Hope this helps!

Best Regards,

Stanly


这篇关于如何在C#中使用一个.csv文件创建具有不同名称的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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