如何将不同的CSV文件转换为标准格式?

huangapple 未分类评论48阅读模式
英文:

How to change convert different csv files into standard format?

问题

我有2-3个.csv文件,其中包含诸如日期(Date)、金额(Amount)、交易说明(Transaction Description)等字段,所有.csv文件都包含这些字段,但顺序被打乱了。我希望能够生成一个输出文件,其中字段按照标准顺序排列(例如,如果我输入示例.csv文件,那么输出文件中的字段就会按照顺序排列)。

我曾尝试为一个文件进行处理,从.csv文件中提取子字符串(当时我并不知道其他文件的字段顺序被打乱了)。

我是一个新手,如果我的提问格式合适,请告诉我!
我可以为示例输入和输出的.csv文件提供链接以供参考吗?
--> https://drive.google.com/drive/folders/1-NZi5OTMTbOWXAfCTsc-ahNYm1N5DG2g(这只是因为描述文件看起来会非常困难)

我做了什么?

我只是尝试使用BufferReader和split从文件中提取数据,但这只能适用于一种类型的文件,我无法使用这种方法获得标准格式!

抱歉发布了如此长的代码,但我所做的是从文件中选择字段,并将它们复制到与输出文件中的标准字段对应的位置。

如果有其他方法可以进行,请给我一些建议。

File file = new File("C:\\Users\\R\\Desktop\\CSVDemo.csv"); 
try { 
    // 创建带有文件参数的FileWriter对象
    FileWriter outputfile = new FileWriter(file); 

    CSVWriter writer = new CSVWriter(outputfile, ',',
            CSVWriter.NO_QUOTE_CHARACTER,
            CSVWriter.DEFAULT_ESCAPE_CHARACTER,
            CSVWriter.DEFAULT_LINE_END); 

    // 创建一个包含字符串数组的列表
    String[] header = { "Date", "Transaction Description", "Debit","Credit","Currency","CardName","Transaction","Location" }; 
    writer.writeNext(header); 

    String splitBy = ",";
    BufferedReader br = new BufferedReader(new FileReader("G:\\US\\HDFC-Input-Case1.csv"));
    String line;
    String transaction = "", name = "";
    while ((line = br.readLine()) != null) {
        // 使用逗号作为分隔符
        String[] cols = line.split(splitBy);

        if(cols.length == 2 && cols[1].equals("Domestic Transactions")) {
            transaction  = "Domestic";
            continue;
        }
        else if(cols.length == 2 && cols[1].equals("International Transactions")) {
            transaction  = "International";
            continue;
        }
        else if(cols.length == 2) {
            name = cols[1];
            continue;
        }
        else if(cols.length < 1){
            continue;
        }
        else if(cols.length > 2) {
            if(cols[0].contains("Date")){
                continue;
            }
            String[] data1 = new String[header.length];
                data1[0] = cols[0];
                String curr ;
                if(cols[1].substring(cols[1].length()-3).equals("USD") || cols[1].substring(cols[1].length()-3).equals("EUR")) {
                    data1[4] = cols[1].substring(cols[1].length()-3);
                    curr = cols[1].substring(0,cols[1].length()-4);
                    data1[1] = curr;
                }
                else {
                    data1[4] = "INR";
                    data1[1] = cols[1];
                }
                
                if(cols[2].contains("cr")){
                    data1[3] = cols[2].substring(0,cols[2].length()-2);
                    data1[2] = "0";
                }
                else {
                    data1[2] = cols[2];
                    data1[3] = "0";
                }
                
                data1[5] = name;
                data1[6] = transaction;
                writer.writeNext(data1); 
        }
        System.out.println();
    } 

    // 关闭写入器连接 
    writer.close(); 
}

请注意,上述代码段只是您原始代码的翻译,没有进行代码审查或修改。如果您有任何特定问题或需要进一步帮助,请随时提问。

英文:

I have 2-3 .csv files with fields like Date, Amount, Transaction Description etc and all the csv files contains these fields but in shuffled order. I want a output file to with a standard order (like if I input the sample .csv file, then I can get the things in order in output file).

I tried to do it for one file by taking substrings from the .csv file (at that time I didn't know that other files have shuffled order fields).

I am kind of new, tell me if I am asking question in a good format!
Can I put a link for the sample input and output .csv file for the reference?
--> https://drive.google.com/drive/folders/1-NZi5OTMTbOWXAfCTsc-ahNYm1N5DG2g (just because it would be very hard to explain that how file looks like)

What I have done?

I have just tried to extract data from the fields using the BufferReader using split but it can only work for one type of file, I cant have a standard format using this!

Sorry for posting such a long code but what I have done is selected field from the file and copied them into output file corresponding to the standard fields in the output file.

Suggest me if there is any other method with which I can proceed.

File file = new File(&quot;C:\\Users\\R\\Desktop\\CSVDemo.csv&quot;); 
		try { 
			// create FileWriter object with file as parameter 
			FileWriter outputfile = new FileWriter(file); 

			CSVWriter writer = new CSVWriter(outputfile, &#39;,&#39;, 
					CSVWriter.NO_QUOTE_CHARACTER, 
					CSVWriter.DEFAULT_ESCAPE_CHARACTER, 
					CSVWriter.DEFAULT_LINE_END); 

			// create a List which contains String array 
			String[] header = { &quot;Date&quot;, &quot;Transaction Description&quot;, &quot;Debit&quot;,&quot;Credit&quot;,&quot;Currency&quot;,&quot;CardName&quot;,&quot;Transaction&quot;,&quot;Location&quot; }; 
			writer.writeNext(header); 

			String splitBy = &quot;,&quot;;
			BufferedReader br = new BufferedReader(new FileReader(&quot;G:\\US\\HDFC-Input-Case1.csv&quot;));
			String line;
			String transaction = &quot;&quot;,name  = &quot;&quot;;
			while ((line = br.readLine()) != null) {
				// use comma as separator
				String[] cols = line.split(splitBy);

				if(cols.length == 2 &amp;&amp; cols[1].equals(&quot;Domestic Transactions&quot;)) {
					transaction  = &quot;Domestic&quot;;
					continue;
				}
				else if(cols.length == 2 &amp;&amp; cols[1].equals(&quot;International Transactions&quot;)) {
					transaction  = &quot;International&quot;;
					continue;
				}
				else if(cols.length == 2) {
					name = cols[1];
					continue;
				}
				else if(cols.length&lt;1){
					continue;
				}
				else if(cols.length&gt;2) {
					if(cols[0].contains(&quot;Date&quot;)){
						continue;
					}
					String[] data1 = new String[header.length];
						data1[0] = cols[0];
						String curr ;
						if(cols[1].substring(cols[1].length()-3).equals(&quot;USD&quot;) || cols[1].substring(cols[1].length()-3).equals(&quot;EUR&quot;)) {
							data1[4] = cols[1].substring(cols[1].length()-3);
							curr = cols[1].substring(0,cols[1].length()-4);
							data1[1] = curr;
						}
						else {
							data1[4] = &quot;INR&quot;;
							data1[1] = cols[1];
						}
						
						if(cols[2].contains(&quot;cr&quot;)){
							data1[3] = cols[2].substring(0,cols[2].length()-2);
							data1[2] = &quot;0&quot;;
						}
						else {
							data1[2] = cols[2];
							data1[3] = &quot;0&quot;;
						}
						
						data1[5] = name;
						data1[6] = transaction;
						writer.writeNext(data1); 
				}
				System.out.println();
			} 

			// closing writer connection 
			writer.close(); 
		}

答案1

得分: 0

你可以首先读取输入 CSV 文件的标题行,并在给定的 CSV 文件中找到所需字段的索引。
一旦你获得了每个标题对应的索引,就可以按照你想要的标准顺序,使用这些索引读取相应的字段,然后写入到输出 CSV 文件中。
示例代码:

CSVReader reader = new CSVReader(new FileReader(fileName));
String[] header = reader.readNext();
List<String> list = Arrays.asList(header);
int indexOfFieldTransaction = list.indexOf("transaction");

现在创建一个列表,按照你希望写入输出文件的顺序插入字段。如果你尝试获取的字段索引在输入文件中不存在,你将会得到 -1。

英文:

You can read the header of your input csv files first and find the indexes of required field in this given csv file.
Once you have required indexes for every header, read those fields using indexes in the standard order you want for your output csv file.
sample codes:

`CSVReader reader = new CSVReader(new FileReader(fileName ));
String[] header = reader.readNext();
List&lt;String&gt; list= Arrays.asList(header);
int indexOfFieldTransaction=list.indexOf(&quot;transaction&quot;);`

Now make a List and insert the field in order you want to write in output file.you will get -1 if the field you are trying to get index of is not present in the input file.

huangapple
  • 本文由 发表于 2020年7月25日 22:44:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/63089690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定