简体   繁体   中英

Format Text File in python

Sample Text File:

["abc","123","apple","red","<a href='link1'>zzz</a>"],

["abc","124","orange","blue","<a href='link1'>zzz</a>"],

["abc","125","almond","black","<a href='link1'>zzz</a>"],

["abc","126","mango","pink","<a href='link1'>zzz</a>"]

Expected Output:

abc 123 apple red 'link1'>zzz

abc 124 orange blue 'link1'>zzz

abc 125 almond black 'link1'>zzz

abc 126 mango pink 'link1'>zzz

I just want the file to be free of braces, commas separated by white spaces, and obtain only the link of the last element in the line.

I tried using Lists in Python.

I dont know how to proceed. Guess, I am going wrong somewhere. Help would be appreciated. Thanks in advance :)

import sys
import re

Lines = [Line.strip() for Line in open (sys.argv[1],'r').readlines()]



for EachLine in Lines:
    Parts = EachLine.split(",")
    for EachPart in Parts:

        EachPart = re.sub(r'[', '', EachPart)
        EachPart = re.sub(r']', '', EachPart)

If you plan to remove [ and ] with a regex, you need to escape the square brackets to match them as literal symbols. They are "special" regex characters denoting the character class boundaries and thus, need special treatment.

Here is a sample regex replacement:

EachPart = re.sub(r'[\[\]]', '', EachPart)

See demo

However, you can remove them with str.replace(old, new[, max]) that does not require a regex:

EachPart = EachPart.replace('[', '').replace(']', '')

See demo

This could be done using the following script:

import csv
import re

with open('input.txt', 'r') as f_input, open('output.txt', 'w') as f_output:
    csv_input = csv.reader(f_input, delimiter='"')
    for cols in csv_input:
        if cols:
            cols = [x for x in cols[1:-1:2]]
            link = re.search(r"('.*?)<", cols[-1])
            if link:
                cols[-1] = link.group(1)

            f_output.write('{}\n'.format(' '.join(cols)))

This will give you output.txt containing:

abc 123 apple red 'link1'>zzz
abc 124 orange blue 'link1'>zzz
abc 125 almond black 'link1'>zzz
abc 126 mango pink 'link1'>zzz

Update - There is a simplified version of this code running here on repl.it to show the correct output. Input comes from a string, and output is displayed. Just click the Run button.

Update - Updated to skip blank lines

There is no need to use regex to remove []

Code:

import ast
with open("check.txt") as inp:
    for line in inp:
        check=ast.literal_eval(line.strip().strip(","))        
        print " ".join(check)

Output:

abc 123 apple red <a href='link1'</a>
abc 124 orange blue <a href='link2'</a>
abc 125 almond black <a href='link3'</a>
abc 126 mango pink <a href='link4'</a>

But to get only the value of href I used regex

Code1:

import re
import ast
with open("check.txt") as inp:
    for line in inp:
        check=ast.literal_eval(line.strip().strip(",")) 
        if re.search("'([^']*?)'",check[4]):
            check[4]=re.search("'([^']*?)'",check[4]).group(1)
        print " ".join(check)

output:

abc 123 apple red link1
abc 124 orange blue link2
abc 125 almond black link3
abc 126 mango pink link4

As per you requirement

 a="<a href='link1'>zzz</a>"
 print re.search("'([^<]*?)<",a).group(1)

output:

link1'>zzz

Code2:

import re
import ast
with open("check.txt") as inp:
    for line in inp:
        check=ast.literal_eval(line.strip().strip(",")) 
        if re.search("'([^<]*?)<",a):
            check[4]=re.search("'([^<]*?)<",a).group(1)
        print " ".join(check)

Since your data is valid python data structures you can read it in using ast.literal_eval :

>>> import ast
>>> ast.literal_eval('''["abc","123","apple","red","<a href='link1'</a>"]''')
['abc', '123', 'apple', 'red', "<a href='link1'</a>"]

You can also slice the link out of the string by taking everything after the 9th character and up until the 5th to last:

>>> s = "<a href='link1'</a>"
>>> s[9:-5]
'link1'

Putting it together:

with open(outfile, 'w') as output:
    with open(filename) as lines:
        for line in lines:
            values = ast.literal_eval(line)
            values[4] = values[4][9:-5]
            output.write(' '.join(values))

Each line may be processed as follows:

>>>line = ["abc","123","apple","red","<a href='link1'>zzz</a>"]

>>>' '.join([k if 'href=' not in k else k[9:-4] for k in line])
"abc 123 apple red link1'>zzz"

Add brackets around the file's contents and you have a valid JSON object:

import json
with open(filename) as lines:
    output = json.loads("[" + lines.read() + "]")

Now you can process the lines, for example removing the anchor around the link:

import re
for line in output:
    line[4] = re.search(r"'([^']*)'", line[4]).group(1)
    print " ".join(line)

What about this code

from __future__ import print_function, unicode_literals
import ast
import io
import re
import traceback

input_str = """["abc","123","apple","red","<a href='link1'</a>"],

["abc","124","orange","blue","<a href='link2'</a>"],

["abc","125","almond","black","<a href='link3'</a>"],

["abc","126","mango","pink","<a href='link4'</a>"]"""

filelikeobj = io.StringIO(input_str)

for line in filelikeobj:
    line = line.strip().rstrip(",")
    if line:
        try:
            line_list = ast.literal_eval(line)
        except SyntaxError:
            traceback.print_exc()
            continue
        for li in line_list[:-1]:
            print(li, end=" ")

        s = re.search("href\s*=\s*['\"](.*)['\"]", line_list[-1], re.I)
        if s:
            print(s.group(1), end="")
        print()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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