正则匹配:用Python将复制到txt中的stata结果保存到Excel中

待解决的问题

1、re.match,re.findall 不可用
2、迭代变量第二次循环没有被执行

import os
import openpyxl
import re
input= r'D:\Users\Desktop'
os.chdir(input)
output='表单.xlsx'
if output in set(os.listdir()):
    os.remove(output)
file='新建文本文档.txt'
f=open(file,'r')
mybook = openpyxl.Workbook()
mysheet=mybook["Sheet"]
mysheet.cell(1,1,1)

j=0
for line in f.readlines():
    j=j+1
    line= re.sub('\|', '', line)
    line = re.sub('#c.', '*', line)
    line = re.sub('\n', '', line)
    print("line:", line)
    r = re.compile("(-)?(.)?(\w)+(.)?(\w)*e?\+?\w*\**", re.S)
    result = re.finditer(r, line)
    #result=re.match(r, line,flags=0)
    #result=re.findall(r, line, flags=0)
    if result==None:
        continue
    i=0
    for p in result: #result循环一次就消失的怪像?所谓的迭代对象?
        i=i+1
        tmp=p.group(0)
        mysheet.cell(row=j,column=i,value=tmp)
        mysheet[chr(i+64)+str(j)]=p.group(0)
mybook.save(output)
f.close()

手动调整后的txt示例(待转化)

Registration |  -6.417736   1.228806    -5.22   0.000                -2.622475
                 Top10_Herf |  -.0977938   .0426266    -2.29   0.022                 -.014151
               Liquid_Share |   .0044309   .0018969     2.34   0.020                 .0216001                            |
  Registration#c.Top10_Herf |                           -.3591656   .5819731    -0.62   0.537                -.0505742                            |
Registration#c.Liquid_Share |                           -.030095   .0210591    -1.43   0.153                -.2519177                            |
                  Inbuy_alt |   .0552509    .119453     0.46   0.644                 .0174605
                   Turnover |   .0015581   .0023998     0.65   0.516                 .0474082
                Sh_Turnover |    .050103   .0460894     1.09   0.277                 .0096543                            |
   Registration#c.Inbuy_alt |                         5.331424   .8136673     6.55   0.000                 .5053153                            |
    Registration#c.Turnover |                            .0892668   .0116077     7.69   0.000                 2.749511                            |
 Registration#c.Sh_Turnover |                           1.666614   .3137146     5.31   0.000                 .5562641                            |
          Institution_share |   -.004442    .002119    -2.10   0.036                 -.050328
              online_chosen |   -.375654    .248488    -1.51   0.131                -.0200624
             offline_chosen |   -.000391   .0265384    -0.01   0.988                -.0001449
         offline_proportion |  -.4638684    .883405    -0.53   0.600                -.0901593
               Fee_PerShare |  -.1287595   .0251989    -5.11   0.000                -.1851379
                Underwriter |  -.0827192    .054499    -1.52   0.129                -.0343665
                 Insell_alt |  -.9117545   1.011517    -0.90   0.368                -.0306057
                      Sh_PE |  -.0526972   .0179867    -2.93   0.003                -.0608822
               Sh_liquidity |   .0002373   .0001583     1.50   0.134                 .0271744
                         PE |  -.0006482    .000312    -2.08   0.038                -.0395622
               1.Date_After |  -.0215152   .0284029    -0.76   0.449                -.0085905

猜你喜欢

转载自blog.csdn.net/qq_37639139/article/details/122295185