简体   繁体   English

for 循环被跳过 + 定义函数以重用字符串拆分

[英]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).我正在开发一个简单的程序,它获取文件名列表并将其转换为导入文件 (.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.将标题打印到输出文件后,我使用 for 循环遍历文件名列表并从中提取必要的信息以生成导入条目行。

input a .csv containing filenames, like such:输入包含文件名的 .csv,例如:

3006419_3006420_ENG_FRONT.jpg

desired output a .impex file formatted thusly:所需的输出 .impex 文件格式如下:

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.所以你可以看到它正确地执行了第一个 for 循环,但没有为第二个或第三个 for 循环编写任何内容。 And I know it could be more nicely done with a clean function.而且我知道使用干净的功能可以做得更好。

Just change each for loop to open the file beforehand:只需更改每个for循环以预先打开文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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