English 中文(简体)
如何读到Stephare和Abrary中的一部分。
原标题:How to read portion of a String in Python and store into List
  • 时间:2024-05-01 23:20:07
  •  标签:
  • python
  • list

我在座右铭上有着明显的变数,我需要从各个部分开始,用具体的措辞结束。

起始体为“proc sql”,而终止舱位为“quit;”在两者之间的所有线均应从名单上提取;

row =   proc sql;
        create table answer_1_stg1 as 
        select  distinct a.CUSTOMER_HIERARCHY_LVL2_CD
                ,b.brand_cd
                ,b.category_cd
                ,a.promo_mechanic_nm
                ,a.promo_mechanic_desc
                ,sum(b.invoice_qty) as sum_invoice
                ,avg(b.invoice_qty) as avg_invoice
                ,max(b.invoice_qty) as max_invoice
                ,min(b.invoice_qty) as min_invoice
        from promo as a
        left join invoice as b
        on 
        a.CUSTOMER_HIERARCHY_LVL2_CD=b.CUSTOMER_HIERARCHY_LVL2_CD and
        a.basecode=b.basecode and
        a.SALESORG_CD=b.SALESORG_CD and
        a.LOCATION_CD=b.LOCATION_CD
        and b.INVOICE_DT between a.event_start_dt and a.event_end_dt
        where year(a.event_start_dt) = 2017 and year(b.INVOICE_DT) = 2017
        group by a.CUSTOMER_HIERARCHY_LVL2_CD
                ,b.brand_cd
                ,b.category_cd
                ,a.promo_mechanic_nm
                ,a.promo_mechanic_desc;
        quit;
        
%sort(answer_1_stg1,descending sum_invoice);

proc sql;
create table answer_1_stg2 as
select distinct promo_mechanic_desc
        ,sum(sum_invoice) as total
from answer_1_stg1;
quit;   

我只需要两条插手,即“proc sql”和“quit”。 最后名单也一样:

lt = [   proc sql;
        create table answer_1_stg1 as 
        select  distinct a.CUSTOMER_HIERARCHY_LVL2_CD
                ,b.brand_cd
                ,b.category_cd
                ,a.promo_mechanic_nm
                ,a.promo_mechanic_desc
                ,sum(b.invoice_qty) as sum_invoice
                ,avg(b.invoice_qty) as avg_invoice
                ,max(b.invoice_qty) as max_invoice
                ,min(b.invoice_qty) as min_invoice
        from promo as a
        left join invoice as b
        on 
        a.CUSTOMER_HIERARCHY_LVL2_CD=b.CUSTOMER_HIERARCHY_LVL2_CD and
        a.basecode=b.basecode and
        a.SALESORG_CD=b.SALESORG_CD and
        a.LOCATION_CD=b.LOCATION_CD
        and b.INVOICE_DT between a.event_start_dt and a.event_end_dt
        where year(a.event_start_dt) = 2017 and year(b.INVOICE_DT) = 2017
        group by a.CUSTOMER_HIERARCHY_LVL2_CD
                ,b.brand_cd
                ,b.category_cd
                ,a.promo_mechanic_nm
                ,a.promo_mechanic_desc;
        quit;   ,   proc sql;
create table answer_1_stg2 as
select distinct promo_mechanic_desc
        ,sum(sum_invoice) as total
from answer_1_stg1;
quit;   ]

我试图操作以下代码,但只将每一行中的第一部印到清单中:

fm = []

for i in row.split( 
 ):
    if "proc sql" in i:
        print()
        fm.append(i.strip())
问题回答

如果你在<条码>proc sql和<条码>上签字,你需要额外的变数才能控制。

在编组之前,即:found = False

In loop:

  • when you find proc sql then set found = True (and set empty fm)
  • next use if found: fm.append(i)
  • when you find quit then set found = False (and convert fm to string, and add to lt)

必须按此顺序这样做。


其他方法包括为此使用<代码>regex,但我将ski:


最低工作守则:

row =   proc sql;
        create table answer_1_stg1 as 
        select  distinct a.CUSTOMER_HIERARCHY_LVL2_CD
                ,b.brand_cd
                ,b.category_cd
                ,a.promo_mechanic_nm
                ,a.promo_mechanic_desc
                ,sum(b.invoice_qty) as sum_invoice
                ,avg(b.invoice_qty) as avg_invoice
                ,max(b.invoice_qty) as max_invoice
                ,min(b.invoice_qty) as min_invoice
        from promo as a
        left join invoice as b
        on 
        a.CUSTOMER_HIERARCHY_LVL2_CD=b.CUSTOMER_HIERARCHY_LVL2_CD and
        a.basecode=b.basecode and
        a.SALESORG_CD=b.SALESORG_CD and
        a.LOCATION_CD=b.LOCATION_CD
        and b.INVOICE_DT between a.event_start_dt and a.event_end_dt
        where year(a.event_start_dt) = 2017 and year(b.INVOICE_DT) = 2017
        group by a.CUSTOMER_HIERARCHY_LVL2_CD
                ,b.brand_cd
                ,b.category_cd
                ,a.promo_mechanic_nm
                ,a.promo_mechanic_desc;
        quit;
        
%sort(answer_1_stg1,descending sum_invoice);

proc sql;
create table answer_1_stg2 as
select distinct promo_mechanic_desc
        ,sum(sum_invoice) as total
from answer_1_stg1;
quit;   

found = False

lt = []
fm = []

for line in row.split( 
 ):
    line = line.strip()
    if "proc sql" in line:
        found = True
        fm = []
    if found:
        fm.append(line)
    if "quit" in line:
        found = False
        lt.append( "
".join(fm) )
      
for item in lt:
    print(item)
    print( ---- )      




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签