如何将文件合并为一个CSV文件? [英] How can I combine files into one CSV file?

查看:134
本文介绍了如何将文件合并为一个CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个文件 FOO_1.txt ,其中包含:

If I have one file FOO_1.txt that contains:

FOOA

FOOB

FOOC

FOOD

...

和许多其他文件 FOO_files.txt 。每个都包含:

and a lots of other files FOO_files.txt. Each of them contains:

1110000000 ...

1110000000...

一行包含 0 FOO1 值( fooa ) / code>, foob ,...)

one line that contain 0 or 1 as the number of FOO1 values (fooa,foob, ...)

现在我想将它们合并到一个文件 FOO_RES.csv 将具有以下格式:

Now I want to combine them to one file FOO_RES.csv that will have the following format:

FOOA,1,0,0,0,0,0,0...

FOOB,1,0,0,0,0,0,0...

FOOC,1,0,0,0,1,0,0...

FOOD,0,0,0,0,0,0,0...

...

优雅的方式来执行
(with hash& arrays - > $ hash {$ key} = \ @ data)?

What is the simple & elegant way to conduct that (with hash & arrays -> $hash{$key} = \@data ) ?

帮助!

Yohad

推荐答案

你的数据和你想要的结果清楚,没有办法,你将能够编码 - 采取一个简单的项目是一个很好的方式开始使用一种新的语言。

If you can't describe a your data and your desired result clearly, there is no way that you will be able to code it--taking on a simple project is a good way to get started using a new language.

允许我介绍一个简单的方法,你可以使用它来翻译任何语言的代码,无论你知道与否。此方法仅适用于小型项目。

Allow me to present a simple method you can use to churn out code in any language, whether you know it or not. This method only works for smallish projects. You'll need to actually plan ahead for larger projects.


  1. 打开文本编辑器,写下您的数据。让每一行都有注释

  2. 描述您想要的结果。

  3. 开始描述将资料变更为所需格式所需的步骤。

& 2 completed:

Numbers 1 & 2 completed:

#!/usr/bin perl
use strict;
use warnings;

# Read data from multiple files and combine it into one file.
# Source files:
#    Field definitions: has a list of field names, one per line.
#    Data files:  
#      * Each data file has a string of digits.
#      * There is a one-to-one relationship between the digits in the data file and the fields in the field defs file.
# 
# Results File:
# * The results file is a CSV file.
# * Each field will have one row in the CSV file.
# * The first column will contain the name of the field represented by the row.
# * Subsequent values in the row will be derived from the data files.
# * The order of subsequent fields will be based on the order files are read.
# * However, each column (2-X) must represent the data from one data file.

现在你知道你有什么,你需要去哪里,你可以flesh程序需要做的是让你到那里 - 这是第3步:

Now that you know what you have, and where you need to go, you can flesh out what the program needs to do to get you there - this is step 3:

你知道你需要有字段列表,所以先得到:

You know you need to have the list of fields, so get that first:

# Get a list of fields.
#   Read the field definitions file into an array.

由于最容易以行为方式编写CSV,因此您需要处理所有文件之前生成每行。因此,您需要在某个位置存储数据。

Since it is easiest to write CSV in a row oriented fashion, you will need to process all your files before generating each row. So you'll need someplace to store the data.

# Create a variable to store the data structure.

现在我们读取数据文件:

Now we read the data files:

# Get a list of data files to parse
# Iterate over list

# For each data file:
#    Read the string of digits.
#    Assign each digit to its field.
#    Store data for later use.

我们有内存中的所有数据,现在写输出:

We've got all the data in memory, now write the output:

# Write the CSV file.
# Open a file handle.

# Iterate over list of fields
# For each field
#   Get field name and list of values.
#   Create a string - comma separated string with field name and values  
#   Write string to file handle

# close file handle.

现在您可以开始将注释转换为代码。对于每个注释,您可以有1到100行代码。你可能会发现你需要做的事情是非常复杂的,你现在不想把它。做一个虚拟子程序来处理复杂的任务,忽略它,直到你做了一切。现在你可以自己解决那个复杂的,棘手的子问题。

Now you can start converting comments into code. You could have anywhere from 1 to 100 lines of code for each comment. You may find that something you need to do is very complex and you don't want to take it on at the moment. Make a dummy subroutine to handle the complex task, and ignore it until you have everything else done. Now you can solve that complex, thorny sub-problem on it's own.

由于你只是学习Perl,你需要打开文档找到如何执行您写入的注释表示的每个子任务。此类工作的最佳资源是 perlfunc Perl语法指南也会派上用场。由于您需要使用复杂的数据结构,因此您还需要阅读数据结构Cookbook

Since you are just learning Perl, you'll need to hit the docs to find out how to do each of the subtasks represented by the comments you've written. The best resource for this kind of work is the list of functions by category in perlfunc. The Perl syntax guide will come in handy too. Since you'll need to work with a complex data structure, you'll also want to read from the Data Structures Cookbook.

你可能想知道你应该知道你应该为一个给定的问题阅读哪些页面。关于Perlmonks的文章标题为如何RTFM 提供了一个很好的介绍文档和如何使用

You may be wondering how the heck you should know which perldoc pages you should be reading for a given problem. An article on Perlmonks titled How to RTFM provides a nice introduction to the documentation and how to use it.

伟大的事情是,如果你陷入困境,当你寻求帮助时,你有一些代码可以分享。

The great thing, is if you get stuck, you have some code to share when you ask for help.

这篇关于如何将文件合并为一个CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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