2017-12-31 281 views
0

私はhtmlファイルを書き込む小さなスクリプトを実行しようとしていますが、このファイルには異なる製品に関する情報が2行含まれているテーブルが必要です。しかし、今、私はこのテーブルが以前の入力データに応じて何度も繰り返される必要があるので、私はhtmlコードを含む関数を掛けることができると思ったが、うまくいきませんでした。メートルの助けの少しは悪いことではないでしょうので、ここでやって...前回の入力に応じた関数の乗算

これは私が欲しいものです:

入力--->どのように多くのテーブル:3

だから、HTML出力ファイルはここでは、この

<-- TABLE 1 --> 
<table> 
    <tr> 
    <td colspan="4" height="30"></td> 
    </tr> 
    <tr> 
    <td width="50" class="width6p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></a></td> 
    <td width="30" class="width3p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></a></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    </tr> 
    <tr> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    </tr> 
</table> 
<-- TABLE 2 --> 
<table> 
    <tr> 
    <td colspan="4" height="30"></td> 
    </tr> 
    <tr> 
    <td width="50" class="width6p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></a></td> 
    <td width="30" class="width3p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></a></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    </tr> 
    <tr> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    </tr> 
</table> 
<-- TABLE 3 --> 
<table> 
    <tr> 
    <td colspan="4" height="30"></td> 
    </tr> 
    <tr> 
    <td width="50" class="width6p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></a></td> 
    <td width="30" class="width3p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></a></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    </tr> 
    <tr> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    </tr> 
</table> 

のようになります私のPythonコードが

ある
import locale 
import requests 
import urlparse 
import json 

def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod): 

    f = open('mkt-output.html','w') 
    f.write(bloque_prod) 
    f.close() 

if __name__ == '__main__': 
    sku_01 = raw_input('Ingrese SKU: ') 
    desc_01 = raw_input('Descripcion de SKU: ') 
    sku_precio_1 = raw_input('Precio de SKU: ') 
    sku_02 = raw_input('Ingrese SKU: ') 
    desc_02 = raw_input('Descripcion de SKU: ') 
    sku_precio_2 = raw_input('Precio de SKU: ') 
    bloque_prod = """<table> 
    <tr> 
    <td colspan="4" height="30"></td> 
    </tr> 
    <tr> 
    <td width="50" class="width6p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/product/{}" target="_blank"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></a></td> 
    <td width="30" class="width3p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/product/{}" target="_blank"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></a></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">{}</span ><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: {}-{}</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">{}</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">{}</span><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: {}-{}</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">{}</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    </tr> 
    <tr> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/product/{}" target="_blank"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/product/{}" target="_blank"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    </tr> 
</table>""".format(sku_01, 
    sku_01, 
    desc_01, 
    sku_02, 
    sku_02, 
    desc_02, 
    ' '.join(desc_01.split()[0:3]), 
    ' '.join(desc_01.split()[3:-1]), 
    desc_01.split()[-1], 
    sku_01[0:-1], 
    sku_01[-1], 
    sku_precio_1, 
    ' '.join(desc_02.split()[0:3]), 
    ' '.join(desc_02.split()[3:-1]), 
    desc_02.split()[-1], 
    sku_02[0:-1], 
    sku_02[-1], 
    sku_precio_2, 
    sku_01, 
    sku_02) 

html(sku_01, desc_01, sku_precio_1, sku_02, desc_02, sku_precio_2, bloque_prod) 
+1

'format()'によって返された文字列に要求された繰り返し回数を掛けます。 –

答えて

0

あなたがHTMLファイルに書き込まれた重複データが必要な場合は、単にhtml()関数writeを持つことができますそれを乗算することにより、変数bloque_prod複数回:

def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod,tables): 

    f = open('mkt-output.html','w') 
    f.write(bloque_prod * tables) 
    f.close() 

注変数の追加表の重複数はです。

その後、tables__main()__に変数定義:

tables = input('Tables: ') 

を...そしてあなたはhtml()

html(sku_01, desc_01, sku_precio_1, sku_02, desc_02, sku_precio_2, bloque_prod,tables) 

を呼び出して、最後の行にtablesを追加しますが、別のテーブルを作成するために探していますか?それは現在印刷されているテーブル注釈を付けることが必要かどう

また、あなたは中のif文追加することができます。

def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod,tables): 
    f = open('mkt-output.html','w') 
    rawHTML = "" 
    for table in range(0, tables): 
     rawHTML += ("\n<-- TABLE " + str(table) + " -->\n" + bloque_prod) 
    f.write(rawHTML) 
    f.close() 

をあなたは1から開始する番号を望んでいた場合(、あなただけstr(table)を変更したいです〜str(table + 1)

- EDIT--異なる値の表を作成しようとしているようです。私はこれを行うためにプログラムを書き直すだろう:

import locale 
import requests 
import urlparse 
import json 

def createTable(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2): 
    bloque_prod = """<table> 
    <tr> 
    <td colspan="4" height="30"></td> 
    </tr> 
    <tr> 
    <td width="50" class="width6p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/product/{}" target="_blank"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></a></td> 
    <td width="30" class="width3p"></td> 
    <td width="260" class="width44p"><a href="http://www.site/product/{}" target="_blank"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></a></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">{}</span ><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: {}-{}</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">{}</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    <td></td> 
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;"> 
     <span style="font-weight: bold; color:#008EAA" class="font14">{}</span><br /> 
     <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/> 
     <span style="font-size:12px;" class="font12">SKU: {}-{}</span><br /> 
     <span style="font-size:18px;" class="font14">$ </span> 
     <span style="font-size:24px; line-height:30px;" class="font20">{}</span> 
     <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br> 
    </td> 
    </tr> 
    <tr> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/product/{}" target="_blank"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    <td></td> 
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/product/{}" target="_blank"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td> 
    </tr> 
</table>""".format(sku_01, 
    sku_01, 
    desc_01, 
    sku_02, 
    sku_02, 
    desc_02, 
    ' '.join(desc_01.split()[0:3]), 
    ' '.join(desc_01.split()[3:-1]), 
    desc_01.split()[-1], 
    sku_01[0:-1], 
    sku_01[-1], 
    sku_precio_1, 
    ' '.join(desc_02.split()[0:3]), 
    ' '.join(desc_02.split()[3:-1]), 
    desc_02.split()[-1], 
    sku_02[0:-1], 
    sku_02[-1], 
    sku_precio_2, 
    sku_01, 
    sku_02) 
    return bloque_prod 

if __name__ == "__main__": 
    f = open('mkt-output.html','w+') # Open file in w+ mode so we can append to the end 
    for table in range(0,input("Tables: ")): 
     print ("--Table "+str(table)+"--") 
     sku_01 = raw_input('Ingrese SKU: ') 
     desc_01 = raw_input('Descripcion de SKU: ') 
     sku_precio_1 = raw_input('Precio de SKU: ') 
     sku_02 = raw_input('Ingrese SKU: ') 
     desc_02 = raw_input('Descripcion de SKU: ') 
     sku_precio_2 = raw_input('Precio de SKU: ') 
     f.write(createTable(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2)) 
    f.close() 

私は助けてくれることを望む。

+0

これを試してみますが、これは指定した時間と同じ情報でテーブルを出力すると思います。具体的には、入力時に指定した回数だけ繰り返されるが、別の情報でテーブルが繰り返されるようにするため、テーブルの同じテンプレートに異なる商品を追加することができます。 – Reezppo

+0

対応のために私の答えが更新されました。 –

+0

パーフェクト...あなたにはすごく感謝しています.... – Reezppo

関連する問題