我正在阅读一份文件,其输出如下:
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
我想在新行上合并每3行,如下所示:
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
我尝试过以下代码:
product=[]
quantity=[]
price=[]
count=1
with open('test.txt','r')as document:
for 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)
此代码仅根据需要返回文档的前三行。我在这个网站上尝试了其他解决方案,但它们都将整个文档合并为一个长字符串。
答案 0 :(得分:3)
itertools
recipe documentation中的此类任务完全匹配:grouper
from itertools import zip_longest
# in case you use python 2 use "from itertools import izip_longest as zip_longest
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
然后你可以使用:
with open('test.txt','r') as document:
res = [', '.join(group) for group in grouper(map(str.strip, document), 3)]
为了说明它是如何工作的,我将字符串列为行列表:
astring = """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""".split('\n')
[','.join(group) for group in grouper(astring, 3)]
#['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']
答案 1 :(得分:2)
你可以试试这个:
data = [i.strip('\n') for i in open('filename.txt')]
new_data = [' ,'.join(data[i:i+3]) for i in range(0, len(data), 3)]
f = open('filename.txt', 'w')
for i in new_data:
f.write("{}\n".format(i))
f.close()