鉴于填充物包含:
aaaaaaa"pic01.jpg"bbbwrtwbbbsize 110KB
aawerwefrewqa"pic02.jpg"bbbertebbbsize 100KB
atyrtyruraa"pic03.jpg"bbbwtrwtbbbsize 190KB
如何获得输出文件:
pic01.jpg 110KB
pic02.jpg 100KB
pic03.jpg 190KB
我的代码是:
with open ('test.txt', 'r') as infile, open ('outfile.txt', 'w') as outfile:
for line in infile:
lines_set1 = line.split ('"')
lines_set2 = line.split (' ')
for item_set1 in lines_set1:
for item_set2 in lines_set2:
if item_set1.endswith ('.jpg'):
if item_set2.endswith ('KB'):
outfile.write (item_set1 + ' ' + item_set2 + '\n')
我的代码有什么问题,请帮忙! 问题已经解决了: what is wrong in the code written inpython
您可以使用regex和
str.rsplit
在这里,您的代码对于这个简单的任务来说似乎是一个多余的东西:或者
现在使用字符串格式:
通常,您可以不用regex解决字符串操作问题,因为Python有一个惊人的字符串库。在您的例子中,只需使用不同的分隔符(引号和空格)调用
str.split
两次就可以解决问题演示
正则表达式会更简单:
相关问题 更多 >
编程相关推荐