Задача превратить текс . генерируемый 1С в sql формат используя perl или python

Tsql теория > Задача превратить текс . генерируемый 1С в sql формат используя perl или python
15.04.2015 13:53:50



Статья:

Задача превратить текс . генерируемый  1С в sql формат используя perl или python

Пример исходный  текст;

F=SP11513  | (P)Работа  |C  |13  |0
F=SP11514  |( P)План  |N  |12  |2 
F=SP11515  | (P)ФактСт  |N  |12  |2
F=SP11516  | (P)Факт  |N  |12 | 2
F=SP11517  | (P)ПроцВыполнения  |N  |5  |2
F=SP11518  | (P)ДатаНачФакт  |D  |0   |0
F=SP11519  |( P)ДатаКонФакт  |D  |0  | 0

 

В типа такого;

,[SP11513] AS [Работа]
,[SP11514] AS [План]
,[SP11515] AS [ФактСт]
,[SP11516] AS [Факт]
,[SP11517] AS [ПроцВыполнения]
,[SP11518] AS [ДатаНачФакт]
,[SP11519] AS [ДатаКонФакт]

PERL

#!/usr/bin/perl

@array;

$i_ish=0;

$i_new=0;

$i_new_num=0;

open(fin,"input.txt");

 

undef $/;

$_=<fin>;

close(fin);

#Замена подстрок по предварительно внесённым шаблонам соотвествия

s/F=/,[/gms;

s/\|\(P\)/AS \[/gms;

s/\|C\s+\|9\s+\|0/\@\]/gms;

s/\|M\s+\|\d+\s+\|\d+/\=/gms;

s/\|N\s+\|\d+\s+\|\d+/\=/gms;

s/\|C\s+\|\d+\s+\|\d+/\=/gms;

s/\|S\s+\|\d+\s+\|\d+/\=/gms;

s/\|D\s+\|\d+\s+\|\d+/\=/gms;

 

 

   chop;

   @F = split ',';

   #обработка с ID

   while (($key, $value) = each(@F)) {

        #  print("$key = $value\n");

                               $s=$value;

                               $i_ish=$i_ish+1;

        if ($s =~ /\[SP(\d+)(.*)\[(.*)\@/) { 

                               push @array,",[SP$1] $2 [$3 $5_ID]";                   

                               #_NUM

                               push @array,",dbo.Convert36To10([SP$1]) $2 [$3 $5_ID_NUM]";                         

                               $i_new=$i_new+1;

                               $i_new_num=$i_new_num+1;

                               }

 

                                 }

                #без ID  

                  while (($key, $value) = each(@F)) {

                               $s=$value;

        if ($s =~ /\[SP(\d+)(.*)\[(.*)\=/) { 

                               push @array,",[SP$1] $2 [$3 $5]";

                               $i_new=$i_new+1;

                               }

 

                                 }

                                

                #Вывод данных

                while (($key, $value) = each(@array)) {

                $line=$value;

                #удаление  лишних пробелов

                $line =~ s/^\s+//gi;

    $line =~ s/\s+$//gi;

    my ($p1, $p2, $p3,$p4) = split('\s+', $line);

    print "$p1     $p2 $p3$p4\n";

    #           print("$line\n");

                }

                $i_ish=$i_ish-1;

                print "\n-------------------------\n STATISTICA:\nOLD=$i_ish\nNEW=$i_new\nNEW_NUM=$i_new_num\n-------------------------\n";

 

 

python

import re

i=0

j=0

lens_id=''

m=''

myfile = open("input.txt", "rU") #чтение из файла

for line in myfile.readlines(): #построчно читаем файл и выводим на экран

                i=i+1

                strok= re.sub(r'F=SP',r',[SP',line)

                strok= re.sub(r',\[SP(\d+)',r',[SP\1]',strok)

                strok= re.sub(r'\|\(P\)',r'AS[',strok)

                strok= re.sub(r'\|C\s+\|9\s+\|0',r'_ID]',strok)

                strok= re.sub(r'\|C\s+\|\d+\s+\|\d+',r']',strok)

                strok= re.sub(r'\|N\s+\|\d+\s+\|\d+',r']',strok)

                strok= re.sub(r'\|D\s+\|\d+\s+\|\d+',r']',strok)

                strok= re.sub(r'\|M\s+\|\d+\s+\|\d+',r']',strok)

                strok= re.sub(r'\|S\s+\|\d+\s+\|\d+',r']',strok)

                strok= re.sub(r'\s+_ID\]',r'_ID]',strok)

                strok= re.sub(r'\s+\]',r']',strok)

                if "_ID" in strok:

                               l=''

                               strokID= re.sub(r',\[SP(\d+)\]\s+AS\[(.*)_ID\]',r',dbo.Convert36To10([SP\1])  AS[\2_ID_NUM]',strok)

                               lens_id=re.sub(r',\[SP(\d+)\]\s+AS(.*)\_ID\]',r'\1',strok)                                                         

                               #выравнивание                            

                               if len(lens_id)==11:

                                               strokID=re.sub(r'(.*)(\s+)AS(.*)',r'\1       AS \3',strokID)

                                               l=re.sub(r'(.*)\](\s+)AS(.*)',r'\1]                           AS \3',strok)

                               elif len(lens_id)==14:

                                               strokID=re.sub(r'(.*)(\s+)AS(.*)',r'\1    AS \3',strokID)

                                               l=re.sub(r'(.*)\](\s+)AS(.*)',r'\1]                        AS \3',strok)

                               elif len(lens_id)==12:

                                               strokID=re.sub(r'(.*)(\s+)AS(.*)',r'\1      AS \3',strokID)

                                               l=re.sub(r'(.*)\](\s+)AS(.*)',r'\1]                          AS \3',strok)

                               elif len(lens_id)==13:

                                               strokID=re.sub(r'(.*)(\s+)AS(.*)',r'\1     AS \3',strokID)

                                               l=re.sub(r'(.*)\](\s+)AS(.*)',r'\1]                        AS \3',strok)

                               else:

                                               strokID=re.sub(r'(.*)(\s+)AS(.*)',r'\1     AS \3',strokID)

                                               l=re.sub(r'(.*)\](\s+)AS(.*)',r'\1]                        AS \3',strok)     

                               strok=l+strokID

                               j=j+1

                else:

                               #выравнивание

                               strok=re.sub(r'(.*)(\s+)AS(.*)',r'\1         AS \3',strok)

                m=m+strok

 

#подготовка к записи

m=m+ '-----------------------------------------\n'

m=m+ 'Статистика\n'

m=m+ '-----------------------------------------\n'

m=m+ 'Всего = %s' % i 

m=m+ '\nВсего_NUM = %s' % j              

#m=m+ '\nДлина = %s | %d' % (lens_id,len(lens_id))

#запись результатов в файл

f=open('out.sql', 'w')

f.write(m)         

f.close()

#print m

 

#print '%s has %d days.' % (Months[cntr],DaysInMonth[cntr])