Skip to content Skip to sidebar Skip to footer

What Is Wrong In The Code Written Inpython

Given that the infile contains: aaaaaaa'pic01.jpg'bbbwrtwbbbsize 110KB aawerwefrewqa'pic02.jpg'bbbertebbbsize 100KB atyrtyruraa'pic03.jpg'bbbwtrwtbbbsize 190KB How to obtain the o

Solution 1:

Your code have only one major problem: if item_set2.endswith ('KB') check doesn't work since there is a new-line char at the end of each line. Replace it with (note strip() call):

if item_set2.strip().endswith('KB'):

Also, you don't need + '\n' since item_set2 already contains a new-line at the end:

outfile.write (item_set1 + ' ' + item_set2.strip())

FYI, you can use regex with saving groups to extract the data:

import re


withopen('test.txt', 'r') as infile, open('outfile.txt', 'w') as outfile:
    for line in infile:
        match = re.search(r'"(.*)"\w+\s(\w+)', line)
        outfile.write(' '.join(match.groups()) + "\n")

The contents of outfile.txt after running the code:

pic01.jpg 110KB
pic02.jpg 100KB
pic03.jpg 190KB

Solution 2:

A solution without the need to import re. The condition can be improved into a one-line condition.

withopen('test.txt', 'r') as infile, open('outfile.txt', 'w') as outfile:
    for line in infile:
        filename = line.strip().split('"')[1]
        size = line.rsplit(None, 1)[-1]
        if filename.endswith('.jpg') and size.endswith('KB'):
            outfile.write('%s %s\n' % (filename, size))

Solution 3:

You should use regex, which will simplify your code. With something like:

import re
withopen ('test.txt', 'r') as infile, open ('outfile.txt', 'w') as outfile:
    for line in infile:
        obj = re.match('.+"(.+\.jpg)".+\s(\d+KB)', line)
        if obj:
             outfile.write (obj.group(1) + ' ' + obj.group(2) + '\n') 

outfile.txt returned by this script:

pic01.jpg 110KB
pic02.jpg 100KB
pic03.jpg 190KB

Solution 4:

First, split the line at spaces and take the second item (in 0 based list, first item), This will give the size part.

Next, split the first item at " and take the second item. That will give the filename.

Check the online Demo, if you want to know how it splits.

withopen ('test.txt', 'r') as infile, open ('outfile.txt', 'w') as outfile:
    for line in infile:
        Parts = line.split()
        outfile.write (Parts[0].split('"')[1] + " " + Parts[1] + "\n")

Output:

pic01.jpg 110KB
pic02.jpg 100KB
pic03.jpg 190KB

Online Demo:

http://ideone.com/EOcuXL

Solution 5:

Using sed:

$ sed 's/.*"\(.*\)".*size \(.*\)/\1 \2/' foo.txt
pic01.jpg 110KB
pic02.jpg 100KB
pic03.jpg 190KB

Post a Comment for "What Is Wrong In The Code Written Inpython"