简体   繁体   中英

for loops getting skipped + define function to re-use string splitting

I am working on a simple program that takes a list of filenames and transforms it into an import file (.impex). The import file has three sections, each of which requires their own header. After printing the header to an output file, I use a for loop to iterate over the list of filenames and extract the necessary information from them to generate the import entry row.

input a .csv containing filenames, like such:

3006419_3006420_ENG_FRONT.jpg

desired output a .impex file formatted thusly:

header-1 line-1
header-1 line-2
header-1 line-3
;E3006419_3006420_FRONT_Image_Container;

header-2 line-1
;3006419_3006420D_ENG_FRONT-92x92;cloudfronturl;3006419_3006420D_ENG_FRONT.jpg;image/jpg;;;100Wx100H;E3006419_3006420_FRONT_Image_Container

header-3 line-1
;3006420;E3006419_3006420_FRONT_Image_Container

Here is my code:

file = open(sys.argv[1])
output = open('output.impex','w+') #define our output impex file

#write variables and header for media container creation
output.write('{}\n{}\n{}\n'.format('header-1 line-1','header-1 line-2','header-1 line-3'))

#write media container creation impex rows
for line in file:
    nameAndExtension = line.split('.')
    name = nameAndExtension[0]
    extension = nameAndExtension[1]
    elements = name.split('_')
    parentSKU = elements[0]
    childSKU = elements[1]
    lang = elements[2]
    angle = elements[3]

    output.write(";E" + parentSKU + "_" + childSKU + "_" + angle + '_Image_Container;\n') 

#write header for media creation
output.write('{}\n'.format('header-2 line-1'))

#write media creation impex rows
for line in file:
    nameAndExtension = line.split('.')
    name = nameAndExtension[0]
    extension = nameAndExtension[1]
    elements = name.split('_')
    parentSKU = elements[0]
    childSKU = elements[1]
    lang = elements[2]
    angle = elements[3]

    output.write('{}\n'.format(';' + name + '-92x92;https://' + cloudfront + '.cloudfront.net/92x92/product-images/ACTIVE-HYBRIS/' + line + ';' + line + ';image/' + extension + ';;100Wx100H'))

#write header for product association
output.write('{}\n'.format('header-3 line-1'))

for line in file:
    nameAndExtension = line.split('.')
    name = nameAndExtension[0]
    extension = nameAndExtension[1]
    elements = name.split('_')
    parentSKU = elements[0]
    childSKU = elements[1]
    lang = elements[2]
    angle = elements[3]

    output.write(childSKU + ";E" + parentSKU + "_" + childSKU + "_" + angle + '_Image_Container;\n')

My output looks like this:

header-1 line-1
header-1 line-2
header-1 line-3
;E3006419_3006420_FRONT_Image_Container;
;E3006419_3006421_FRONT_Image_Container;
;E3006419_3006422_FRONT_Image_Container;
;E3006419_3006423_FRONT_Image_Container;
;E3006419_3006424_FRONT_Image_Container;
header-2 line-1
header-3 line-1

So you can see it's doing the first for-loop correctly, but not writing anything for the second or third for loops. And I know it could be more nicely done with a clean function.

Just change each for loop to open the file beforehand:

file = open(sys.argv[1])
for line in file:
    nameAndExtension = line.split('.')
    # etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM