I'm trying to create a list from a text file that I am reading into Python. The text file contains a bunch of square brackets throughout the file [some text here]. What I am trying to do is first count how many of those square bracket pairings I have [] and then add whatever text is inside of them into a list.
Here is a super simplified version of the text file I am trying to use with the brackets:
"[name] is going to the store! It's going to be at [place] on [day-of-the-week]."
Here is what I have:
bracket_counter = 0
file_name = "example.txt"
readFile = open(file_name)
text_lines = readFile.readlines()
for line in text_lines:
val = line.split('[', 1)[1].split(']')[0]
print(val)
if line has [ and ]:
bracket_counter = 1
I'm super new to Python. I don't know if I should be using regular expressions for this or if that is overcomplicating things.
Thanks for your help!
CodePudding user response:
You can of course use regular expressions for that. In the following example, you're extracting all content within square brackets and store them in the variable words
.
words = re.findall(r'\[([^\]] )\]', line)
Don't forget to import re
at the top of your program.
Explanation of the regex:
\[
and\]
match square brackets. As these are used in regex as well, you have to escape them with a backslash(...)
is a capturing group, all regex parts within normal brackets will be returned as a finding (for e.g. in the listwords
)[^\]]
will match all characters except the]
To put it all together:
This regex looks for an opening square bracket, then matches all characters until a closing square bracket appears and returns the matches within a list.
CodePudding user response:
Python doesn't have a has
keyword.
You're probably looking for something like:
('[' in line) or (']' in line)
which will evaluate to True if the line includes [ or ].
CodePudding user response:
If there's a lonely soul out there like me who wondered how this could be done without using regex, I guess this could be a solution also:
results = []
with open("example.txt") as read:
for line in read:
start = None; end = None
for i,v in enumerate(line):
if v == "[": start = i
if v == "]": end = i
if start is not None and end is not None:
results.append(line[start 1:end])
start = None; end = None
print(results)
Result:
['name', 'place', 'day-of-the-week']