dsfdf854456 2014-05-07 18:05
浏览 33
已采纳

转换复杂的文本列表

I have a large word list in a simple text document (80K words) that goes like this:

needy
    needier, neediest, neediness
need
nefarious
    nefariously, nefariousness
negate
    negated, negates, negating

I would like to transform it to something like this:

needy needy
needy needier
needy neediest
needy neediness
need need
nefarious nefarious    
nefarious nefariously
nefarious nefariousness
negate negate  
negate negated
negate negates
negate negating

How would you advise me to do? I can do some php mysql, or I can work on Excel.

  • 写回答

1条回答 默认 最新

  • doutany76678 2014-05-07 18:15
    关注

    Explanation:

    The code opens your text file and grabs them into an array, splits them into chunks of 2's. It is iterated through a loop and the second array element is exploded using comma and concatenated to the first element and then added to a new array.

    <?php
    echo "<pre>";
    $arr = file('stats.txt',FILE_IGNORE_NEW_LINES); //<--- Point the filename of your text document
    $arr = array_chunk($arr,2);
    $new_arr = array();
    foreach($arr as $k=>$arr1)
    {
        $v = explode(',',$arr1[1]);
        foreach($v as $val)
        {
            $new_arr[]=$arr1[0]." ".trim($val);
        }
    }
    print_r($new_arr);
    

    <kbd>Demonstration</kbd>

    OUTPUT :

    Array
    (
        [0] => needy needier
        [1] => needy neediest
        [2] => needy neediness
        [3] => nefarious nefariously
        [4] => nefarious nefariousness
        [5] => negate negated
        [6] => negate negates
        [7] => negate negating
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?