使用python并排排列文本文件 [英] arranging text files side by side using python

查看:28
本文介绍了使用python并排排列文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个目录中有 3000 个文本文件,每个 .txt 文件都包含单列数据.我想将它们并排排列,使其成为 mxn 矩阵文件.

I have 3000 text files in a directory and each .txt file contain single column data. i want to arrange them side by side to make it a mxn matrix file.

例如:在linux中粘贴1.txt 2.txt 3.txt 4.txt ................3000.txt

为此我尝试过

printf "%s
" *.txt | sort -n | xargs -d '
' paste

但是它给出了错误粘贴:filename.txt: Too many open files

However it gives error paste: filename.txt: Too many open files

请使用 python 提出更好的解决方案.

please suggest a better solution for the same using python.

推荐答案

根据收到的输入,请按照以下步骤操作.

Based on the inputs received, please follow the below steps.

# Change ulimit to increase the no of open files at a time
$ ulimit -n 4096

# Remove blank lines from all the files
$ sed -i '/^[[:space:]]*$/d' *.txt

# Join all files side by side to form a matrix view
$ paste $(ls -v *.txt) > matrix.txt

# Fill the blank values in the matrix view with 0's using awk inplace
$ awk -i inplace 'BEGIN { FS = OFS = "	" } { for(i=1; i<=NF; i++) if($i ~ /^ *$/) $i = 0 }; 1' matrix.txt

这篇关于使用python并排排列文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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