Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接多个数据文件_Python_C++_Shell_Sed_Text Files - Fatal编程技术网

Python 连接多个数据文件

Python 连接多个数据文件,python,c++,shell,sed,text-files,Python,C++,Shell,Sed,Text Files,我有几个数据文件如下所示: HR0 012312010 001230202 HR1 012031020 012320102 012323222 012321010 HR2 321020202 ... auto key = cbegin(output)->first; filep << key << ":\n" << setfill('0'); for(const auto& it : output) { if(it.first

我有几个数据文件如下所示:

HR0
012312010
001230202

HR1
012031020
012320102
012323222
012321010

HR2
321020202
...
auto key = cbegin(output)->first;

filep << key << ":\n" << setfill('0');

for(const auto& it : output) {
    if(it.first == key) {
        filep << '\t' << setw(9) << it.second << endl;
    } else {
        key = it.first;
        filep << key << ":\n\t" << setw(9) << it.second << endl;
    }
}
解释:有一行定义字段(HR“n”)、一个具有四元数的可变行数(321020202)以及两个字段之间的额外换行。我想合并等效的HR字段。所以在某种意义上,我想把这些文件压缩成一个大文件。我认为使用sed是答案,但我不知道从哪里开始


我想在Python或C++程序上使用shell脚本,因为我觉得它在编写和执行方面可能更快。思想?

< P>这在C++中是很容易做到的,如果你有C++ 17,那么它会更容易。 您可以编写一个函数来读取
multimap
,例如:

multimap<int, int> read(istream& input) {
    multimap<int, int> output;
    string i;

    while(input >> i) {
        const auto key = std::atoi(data(i) + 2);

        transform(istream_iterator<int>(input), istream_iterator<int>(), inserter(output, begin(output)), [key](const auto value){ return make_pair(key, value); });
        input.clear();
    }
    return output; 
}

我在这里写了一个只涉及一个文件的实例:

过早优化。编译的C++程序是最快的。你想如何处理冲突?如果在不同的文件HR0s中有20000013个元素呢?@JonathanMee所有元素都是唯一的,因为数据是如何创建的。所以,如果我正确理解了你的问题,就不需要检查了。哇,太棒了!非常感谢你@杰夫YUP,C++ 11/14/17已经如此先进的语言,它现在非常有竞争力的Python和脚本语言,易于使用,但保留低级别的权力,C++一直受到尊重。无论如何如果这能解决你的问题,我希望你能接受。