You can use str.startswith() to check if the string starts with '[' , and use ast.literal_eval() only if that is true. Example -
with open("test.txt") as f:
registros = [ast.literal_eval(x) for x in f if x.startswith('[')]
Demo -
>>> s = """bruce
... chung
... [email protected]
... 8893243
... costarricense
... divisa del informe colones
... ['nokia', 5277100.0, 'china']
... ['samsung', 10554200.0, 'turkey']
... ['apple', 52771000.0, 'argentina']""".splitlines()
>>> import ast
>>> registros = [ast.literal_eval(x) for x in s if x.startswith('[')]
>>> registros
[['nokia', 5277100.0, 'china'], ['samsung', 10554200.0, 'turkey'], ['apple', 52771000.0, 'argentina']]
For the updated requirements in the comments -
How can I also read only the user info and assign it to a variable using this method?
Assuming any line that is not a list, is part of the user info
You can use a simple for loop, keep appending to a variable if the line does not start with '[' , and if it does, use ast.literal_eval() on it, and append it to the registros list.
Example -
import ast
with open("test.txt") as f:
user_info = ''
registros = []
for line in f:
if line.startswith('['):
registros.append(ast.literal_eval(line))
else:
user_info += line
If the user_info would always just be 6 lines (as given in the comments) , you can also use -
import ast
with open("test.txt") as f:
user_info = ''
for i,line in enumerate(f):
user_info += line
if i==5:
break
registros = [ast.literal_eval(x) for x in f if x.startswith('[')]