iteration - How to combine every 3 lines from a read document in python -


i reading document output follows:

pretzel crisps original/thin/crunchy pretzel crackers pretzels, 7.2 oz  1 × $2.14  $2.14  bagel bites cheese & pepperoni mini bagels, 40 count, 31.1 oz  1 × $7.98  $7.98  superpretzel cheddar cheese filled soft pretzel sticks softstix, 9 oz  1 × $2.56  $2.56 

i combine every 3 lines on new line follows:

pretzel crisps original/thin/crunchy pretzel crackers pretzels 7.2 oz, 1 × $2.14, $2.14  bagel bites cheese & pepperoni mini bagels 40 count 31.1 oz, 1 × $7.98, $7.98  superpretzel cheddar cheese filled soft pretzel sticks softstix 9 oz, 1 × $2.56, $2.56 

i have tried following code:

product=[] quantity=[] price=[] count=1  open('test.txt','r')as document:       line in document:         line=line.replace('\n','')         if count == 1:             line=line.replace(',','')             product.append(line)         if count == 2:             quantity.append(line)         if count == 3:             price.append(line)         count+=1     all=list(zip(product,quantity,price))     print(all)   

this code return first 3 lines of document desired. i've tried other solutions on site combine entire document 1 long string.

you can try this:

data = [i.strip('\n') in open('filename.txt')]  new_data = [' ,'.join(data[i:i+3]) in range(0, len(data), 3)]  f = open('filename.txt', 'w') in new_data:    f.write("{}\n".format(i))  f.close() 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -