简体   繁体   中英

how to get the contents in brackets in a file Python

I am working with files right now and i want to get text from a bracket this is what i mean by getting text from a brackets...

{
this is text for a
this is text for a
this is text for a
this is text for a
}

[
this is text for b
this is text for b
this is text for b
this is text for b
]

The content in a is this is text for a and the content for b is is text for b my code seems to not be printing the contents in a properly it show a&b my file.

My code:

with open('file.txt','r') as read_obj:
  for line in read_obj.readlines():
    var = line[line.find("{")+1:line.rfind("}")]
    print(var)
    
    
  • iterate over the file
  • for each line check the first character
    • if the first character is either '[' or '{' start accumulating lines
    • if the first character is either ']' or '}' stop accumulating lines

a_s = []
b_s = []
capture = False
group = None
with open(path) as f:
    for line in f:
        if capture: group.append(line)
        if line[0] in '{[':
            capture = True
            group = a_s if line[0] == '{' else b_s
        elif line[0] in '}]':
            capture = False
            group = None

print(a_s)
print(b_s)

Relies on the file to be structured exactly as shown in the example.

This is what regular expressions are made for. Python has a built-in module named re to perform regular expression queries.

In your case, simply:

import re

fname = "foo.txt"

# Read data into a single string
with open(fname, "r") as f:
    data = f.read()

# Remove newline characters from the string
data = re.sub(r"\n", "", data)

# Define pattern for type A
pattern_a = r"\{(.*?)\}"

# Print text of type A
print(re.findall(pattern_a, data))

# Define pattern for type B
pattern_b = r"\[(.*?)\]"

# Print text of type B
print(re.findall(pattern_b, data))

Output:

['this is text for athis is text for athis is text for athis is text for a']
['this is text for bthis is text for bthis is text for bthis is text for b']

Read the file and split the content to a list. Define a brackets list and exclude them through a loop and write the rest to a file.

file_obj = open("content.txt", 'r')
content_list = file_obj.read().splitlines()
brackets = ['[', ']', '{', '}']
for i in content_list:
    if i not in brackets:   
        writer = open("new_content.txt", 'a')
        writer.write(i+ '\n')
    writer.close()
f1=open('D:\\Tests 1\\t1.txt','r')
for line in f1.readlines():
    flag=0
    if line.find('{\n') or line.find('[\n'):
        flag=1
    elif line.find('}\n') or line.find(']\n'):
        flag=0
    if flag==1:
        print(line.split('\n')[0])

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