2016-12-04 99 views
-7

我一直在使用谷歌搜索,但没有找到任何东西,所以我虽然我会问这里。CSV复制列代码

我需要做的是编写一个脚本(此时任何语言都可以),它将复制一个csv文件的指定列。

例如:我需要它复制第4列的副本,并在第1列之后添加此副本。

我知道你可以用excel做到这一点,但我需要有一些代码为我做这个。

这可能吗?任何指针?

+0

阅读CSV成矩阵阵列,在列读取值,插入重复的值成矩阵阵列列,保存为CSV。请显示代码表明你已经完成了这个,谢谢 –

+0

你已经问过这个问题了。你为什么一次又一次地问。 –

+0

为什么不在这里讨论:http://stackoverflow.com/questions/40946226/delphi-programatically-duplicating-a-csv-column/ – MBo

回答

1

使用C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 


namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string IN_FILENAME = @"c:\temp\testin.csv"; 
     const string OUT_FILENAME = @"c:\temp\testout.csv"; 
     static void Main(string[] args) 
     { 
      StreamReader reader = new StreamReader(IN_FILENAME); 
      StreamWriter writer = new StreamWriter(OUT_FILENAME); 

      string inputLine = ""; 
      while ((inputLine = reader.ReadLine()) != null) 
      { 
       List<string> inputArray = inputLine.Split(new char[] { ',' }).ToList(); 
       inputArray.Add(inputArray[3]); 
       writer.WriteLine(string.Join(",", inputArray)); 
      } 

      reader.Close(); 
      writer.Flush(); 
      writer.Close(); 


     } 

    } 
} 
1

在Python与pandas

import pandas 
df=pandas.read_csv('your_csv.csv') 
df['copy_column_4']=df['column_4'] 
df.to_csv('your_new_csv.csv') 
+0

请问这是什么语言? – johnofark