【问题标题】:auto increment numbering of multi line which are blank空白的多行的自动递增编号
【发布时间】:2012-08-11 11:12:42
【问题描述】:

我有一个这样的文件

apple

ae-pal

noun.

a fruit

ball

b'al

noun.

playing material
round shaped

等等。所以它以单词开头,然后是一个空行和发音(我知道上面的那些是愚蠢的:P)。然后是词性和意义。每学期后有空行。 我最终想要的是进行递归调用,以便它拍摄第一个单词并放置在数据库中的一个表中(mysql,可能是),然后第二个进入同一个表的对应行,依此类推。

首先我想给这些空间编号。比如 1 2 3 4 等等。这样我就可以将所有 1、5、9(即 2*x+1)放在一个地方,将 2*x 放在另一个地方,这样我就可以达到我的目的,我可以将它们推送到数据库中,最终得到我的字典。

我可以找到一种用数字替换空行的方法,但不知道如何使它们增加数字。我想知道如何使用 sed、awk 甚至 python 来实现这一点。毫无疑问,正则表达式将会出现。

伪代码

is line empty ? 
   yes ? give a number  x (x =1)
   increase x by 1
   no ? go to next line
   repeat till eof.

希望我说的够清楚了!

【问题讨论】:

  • 要回答您的问题,请使用enumerate(line for line in open(...) if line),尽管有更好的方法可以批量拆分文件。

标签: python regex sed awk


【解决方案1】:

这可能对你有用:

awk '/^$/{print ++c;next};1' file

或 GNU sed:

touch /tmp/c
addone () { c=$(</tmp/c); ((c+=1)); echo $c | tee /tmp/c; }
export -f addone
sed '/^$/s//addone/e' file
rm /tmp/c

另一种方法可能是将所有空行转换为制表符,每四个制表符转换为换行符。

sed ':a;$!{N;ba};s/\n\n/\t/g;y/\n/ /;' file | sed 's/\t/\n/4;P;D'

【讨论】:

    【解决方案2】:

    您可以使用iterable,因为它仅在调用next() 时才会产生

    with open('data.txt') as f:
        lines=[x.strip() for x in f]
        spaces=lines.count('')   #count the number of empty lines
        odd_spaces=spaces//2+1   #odd lines 1,3,5,7...
        even_spaces=spaces-odd_spaces #even lines 2,4,6,...
    
        it=iter(range(1,spaces+1)) #create an iterable
        try:
            lines=[x if x!='' else next(it) for x in lines]  #if line is empty then call next(it)
        except StopIteration:
            pass
        for x in lines:
            print(x)
    
        fil=[4*x+1 for x in range(0,spaces+1) if 4*x+1<spaces] #4x+1
        print(fil)
        row=[lines[lines.index(x)-1] for x in fil]
        print(row)
    
        fil=[2*x+1 for x in range(0,spaces+1) if 2*x+1<spaces] #2x+1
        print(fil)
        row=[lines[lines.index(x)-1] for x in fil]
        print(row)
    

    输出:

    apple
    1
    ae-pal
    2
    noun.
    3
    a fruit
    4
    ball
    5
    b'al
    6
    noun.
    7
    playing material
    round shaped
    [1, 5]
    ['apple', 'ball']
    [1, 3, 5]
    ['apple', 'noun.', 'ball']
    

    【讨论】:

    • 我现在正在考虑如何将它们插入数据库。每次出现的实例都应该排在一行。例如,这里的“apple”和“ball”应该排成一行。 htat 是第 4*x+1 行元素。有什么建议吗?
    • @CandyGupta 我编辑了我的解决方案并添加了2x+14x+1,我想这就是你想要的。
    【解决方案3】:
    (line for line in open(...) if line)
    

    是对文件非空行的迭代。使用this recipe 进行四次迭代:

    def grouper(iterable, n, fillvalue=None):
        args = [iter(iterable)] * n
        return izip_longest(*args, fillvalue=fillvalue)
    
    nonempty_lines = (line for line in open(...) if line)
    grouper(nonempty_lines, 4)
    

    【讨论】:

    • 试试看能不能成功。
    【解决方案4】:

    你为什么不运行一个循环计算空行然后插入数据库 正则表达式很重要吗?

    给你,一个快速而肮脏的 php 实现

    <?php
    
    $filename = $argv[1];
    
    if(file_exists($filename) && is_readable($filename)) {
    
        $fh = fopen ($filename, "r");
        $count = 0;
        $el = 0;
        $items = array();
        while(!feof($fh)) {
            $line = fgets($fh);
            if($line == "\n")
            {
                $count++;
                if($count == 4)
                {
                    $el ++;
                    $count = 0;
                }
                continue;
            }
            $items[$el][$count] .= $line;
        }
        fclose($fh);
    }
    var_dump($items);
    
    ?>
    

    在命令行中以 php script.php 文件名运行它 这就是我得到的

    array(4) {
      [0] =>
      array(4) {
        [0] =>
        string(6) "apple\n"
        [1] =>
        string(7) "ae-pal\n"
        [2] =>
        string(6) "noun.\n"
        [3] =>
        string(8) "a fruit\n"
      }
      [1] =>
      array(4) {
        [0] =>
        string(5) "ball\n"
        [1] =>
        string(5) "b'al\n"
        [2] =>
        string(6) "noun.\n"
        [3] =>
        string(30) "playing material\nround shaped\n"
      }
      [2] =>
      array(4) {
        [0] =>
        string(5) "pink\n"
        [1] =>
        string(7) "pe-ank\n"
        [2] =>
        string(6) "color\n"
        [3] =>
        string(14) "girlish\ncolor\n"
      }
      [3] =>
      array(1) {
        [0] =>
        string(0) ""
      }
    }
    

    【讨论】:

    • 这里是第四个实例的单词的含义不只是一行。它在多行中,后面没有空行。并且这些需要作为一个条目放入数据库中。我不知道怎么进去。
    • 在多行第四个实例之后你确实有一个空行对吗?您可以检查该行是否为空?
    猜你喜欢
    • 2016-07-26
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2017-04-21
    • 2010-11-01
    • 2013-05-11
    • 2012-02-23
    相关资源
    最近更新 更多