Python,从文件中删除特定列

时间:2013-04-20 18:26:03

标签: python text text-processing

我有一个以下格式的文件;

string1     string2         ........    stringN

value1,1    value1,2    ........    value1,N
   .            .       ........        .
   .            .       ........        .
   .            .       ........        .
valueM,1    valueM,2    ........    valueM,N

M的规模为10000 N的范围为100

我需要;

  • 删除空行
  • 删除前两列
  • 保留第7,14,21 ...列并删除其余

分别来自此文件。

由于此数据中还有字符串(每列的标题),因此numpy变得非常棘手。我很感激任何指导。

2 个答案:

答案 0 :(得分:3)

您有一个自定义的ASCII表格格式,其中包含固定列:

*********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
*    Row   * Instance * test_string * test_string * test_string * test_string * test_string * test_string * test_string * string__722 * string__722 * string__722 * string__722 * string__722 * string__722 * string__722 * string__720 * string__720 * string__720 * string__720 * string__720 * string__720 * string__720 * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * string__718 * string__718 * string__718 * string__718 * string__718 * string__718 * string__718 * string__719 * string__719 * string__719 * string__719 * string__719 * string__719 * string__719 * string__723 * string__723 * string__723 * string__723 * string__723 * string__723 * string__723 * string__721 * string__721 * string__721 * string__721 * string__721 * string__721 * string__721 * another_str * another_str * another_str * another_str * another_str * another_str * another_str * another_str * another_str *
*********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
*        0 *        0 *           0 *    50331648 * test_string *           2 *           1 *          13 * 5.76460e+18 *           0 *    50331648 * string__722 *           2 *           1 *         606 * 5.83666e+18 *           0 *    50331648 * string__720 *           2 *           1 *         575 * 5.83666e+18 *           0 *    50331648 * HCAL_SlowDa *           2 *           1 *          36 * 5.76460e+18 *           0 *    50331648 * string__718 *           2 *           1 *         529 * 5.83666e+18 *           0 *    50331648 * string__719 *           2 *           1 *         529 * 5.83666e+18 *           0 *    50331648 * string__723 *           2 *           1 *         529 * 5.83666e+18 *           0 *    50331648 * string__721 *           2 *           1 *         529 * 5.83666e+18 *           0 *    50331648 *      212135 *       15080 *           1 *           1 *        3340 *        1057 * 1.399999976 *
*        0 *        1 *           0 *    50331648 *             *           2 *           1 *          13 *           0 *           0 *    50331648 *             *           2 *           1 *         606 *       53440 *           0 *    50331648 *             *           2 *           1 *         575 *       53440 *           0 *    50331648 *             *           2 *           1 *          36 *           0 *           0 *    50331648 *             *           2 *           1 *         529 *       53440 *           0 *    50331648 *             *           2 *           1 *         529 *       53440 *           0 *    50331648 *             *           2 *           1 *         529 *       53440 *           0 *    50331648 *             *           2 *           1 *         529 *       53440 *           0 *    50331648 *      212135 *             *           1 *           1 *        3340 *        1057 * 1.399999976 *
*        0 *        2 *           0 *    50331648 *             *           2 *           1 *          13 *  4294970636 *           0 *    50331648 *             *           2 *           1 *         606 * 1.09780e+16 *           0 *    50331648 *             *           2 *           1 *         575 * 1.09780e+16 *           0 *    50331648 *             *           2 *           1 *          36 * 2.70217e+16 *           0 *    50331648 *             *           2 *           1 *         529 * 1.09780e+16 *           0 *    50331648 *             *           2 *           1 *         529 * 1.09780e+16 *           0 *    50331648 *             *           2 *           1 *         529 * 1.09780e+16 *           0 *    50331648 *             *           2 *           1 *         529 * 1.09780e+16 *           0 *    50331648 *      212135 *             *           1 *           1 *        3340 *        1057 * 1.399999976 *
*        0 *        3 *           0 *    50331648 *             *           2 *           1 *          13 *   352321545 *           0 *    50331648 *             *           2 *           1 *         606 * 2.30610e+18 *           0 *    50331648 *             *           2 *           1 *         575 * 2.30610e+18 *           0 *    50331648 *             *           2 *           1 *          36 * 7.30102e+18 *           0 *    50331648 *             *           2 *           1 *         529 * 1.15294e+19 *           0 *    50331648 *             *           2 *           1 *         529 * 1.15294e+19 *           0 *    50331648 *             *           2 *           1 *         529 * 1.15294e+19 *           0 *    50331648 *             *           2 *           1 *         529 * 1.15294e+19 *           0 *    50331648 *      212135 *             *           1 *           1 *        3340 *        1057 * 1.399999976 *
*        0 *        4 *           0 *    50331648 *             *           2 *           1 *          13 *           0 *           0 *    50331648 *             *           2 *           1 *         606 * 1.15294e+19 *           0 *    50331648 *             *           2 *           1 *         575 * 1.15294e+19 *           0 *    50331648 *             *           2 *           1 *          36 * 2.82590e+16 *           0 *    50331648 *             *           2 *           1 *         529 * 1.15294e+19 *           0 *    50331648 *             *           2 *           1 *         529 * 1.15294e+19 *           0 *    50331648 *             *           2 *           1 *         529 * 1.15294e+19 *           0 *    50331648 *             *           2 *           1 *         529 * 1.15294e+19 *           0 *    50331648 *      212135 *             *           1 *           1 *        3340 *        1057 * 1.399999976 *

如果我们假设实际数据字段本身都不包含星号,则读取每一行的最简单方法是使用正则表达式来分割行。

要输出,我仍然使用csv module,因为这会使未来的处理变得更加容易:

import csv
import re
from itertools import islice

row_split = re.compile('\s*\*\s*')

with open(someinputfile, 'rb') as infile, open(outputfile, 'wb') as outfile:
    writer = csv.writer(outfile, delimiter='\t')

    next(islice(infile, 3, 3), None) # skip the first 3 lines in the input file

    for line in infile:
        row = row_split.split(line)[1:-1]
        if not row: continue
        writer.writerow(row[8::7])

这会跳过空行,并且每隔7列只写一次(从第9列开始计算)并跳过其余的行。

第一行是:

['5.76460e+18', '5.83666e+18', '5.83666e+18', '5.76460e+18', '5.83666e+18', '5.83666e+18', '5.83666e+18', '5.83666e+18', '3340']

答案 1 :(得分:0)

这是删除空行:

filtered = filter(lambda x: not re.match(r'^\s*$', x), original)

删除特定列(我假设您的数据存储在文本文件中):

f = open("textfile.txt","r")
lines = f.readlines()
f.close()
f = open("newfile.txt","w")

回写你的行,除了你要删除的行:

list = [0, 1, 6, 13, 20] # remove first,second as well as 7th, 14th and 21th line
for i,line in enumerate(lines):
  if i not in list:
    f.write(line)

最后,再次关闭文件。

f.close()