new version of my last attempt:
import re
a = [['0 Client Name:'],
['0 Client ID:'],
['0 Industry:'],
['0 SEC:'],
['0 Industry Sector:']]
for i in range(len(a)):
b = a[i][0]
b = b.split(' ')
print(b[1])
output:
Client Name:
Client ID:
Industry:
SEC:
Industry Sector:
Try the following, altough not very clean but gets the job done.
l = [['0 Client Name:'],
['0 Client ID:'],
['0 Industry:'],
['0 SEC:'],
['0 Industry Sector:']]
newl = []
for i in range(len(l)):
newl.append(l[i][0].split(" ")[2])
print(newl)
'''
['Client Name:', 'Client ID:', 'Industry:', 'SEC:', 'Industry Sector:']
'''
Consider using literal_eval:
from ast import literal_eval
txt = ["[21049090, 'AARTIIND22AUGFUT', 'AARTIIND', 850, 1793]", "[21049346, 'ABB22AUGFUT', 'ABB', 250, 3329]"]
result = [literal_eval(i) for i in txt]
print(result)
Output:
[[21049090, 'AARTIIND22AUGFUT', 'AARTIIND', 850, 1793], [21049346, 'ABB22AUGFUT', 'ABB', 250, 3329]]
Edit:
Or eval(). Refer to Tim Biegeleisen's answer.
Using the eval() function along with a list comprehension we can try:
inp = ["[21049090, 'AARTIIND22AUGFUT', 'AARTIIND', 850, 1793]", "[21049346, 'ABB22AUGFUT', 'ABB', 250, 3329]"]
output = [eval(x) for x in inp]
print(output)
This prints:
[
[21049090, 'AARTIIND22AUGFUT', 'AARTIIND', 850, 1793],
[21049346, 'ABB22AUGFUT', 'ABB', 250, 3329]
]
The simplest way, of course, it is just write the assignment statement:
new_list=['title','plays']
But you probably intended to ask a more general question, like "How can I extract the 2nd item from the first two tuples in a list?" Like so:
new_list = [alist[0][1], alist[1][1]]
Or maybe you meant, "How can I extract the 2nd item from each tuple in a list?" Like so:
new_list = [t[1] for t in alist]
alist = [(0, 'title', 'TEXT', 0, None, 0),(0, 'plays', 'integer', 0, None, 0)]
new_list = [alist[0][1], alist[1][1]]
to check,
print(new_list)
Explain
This line:
alist = [(0, 'title', 'TEXT', 0, None, 0), (0, 'plays', 'integer', 0, None, 0)]
Above is actually a tuple inside list. So inside the the alist, there are two tuples. Inside each tuple, there are 6 objects.
So alist[0] means, you are calling the first tuple inside the alist.
and alist[0][1] means your are calling second element of the first tuple. Like this, you can think about alist[1][1] also.
If the format is exactly the same you've provided, you'd better go with using re:
import re
file_info = ['{file:file1, directory:dir1}', '{file:file2, directory:directory2}']
pattern = re.compile(r'\w+:(\w+)')
for item in file_info:
print re.findall(pattern, item)
or, using string replace(), strip() and split() (a bit hackish and fragile):
file_info = ['{file:file1, directory:dir1}', '{file:file2, directory:directory2}']
for item in file_info:
item = item.strip('}{').replace('file:', '').replace('directory:', '')
print item.split(', ')
both code snippets print:
['file1', 'dir1']
['file2', 'directory2']
If the file_info items are just dumped json items (watch the double quotes), you can use json to load them into dictionaries:
import json
file_info = ['{"file":"file1", "directory":"dir1"}', '{"file":"file2", "directory":"directory2"}']
for item in file_info:
item = json.loads(item)
print item['file'], item['directory']
or, literal_eval():
from ast import literal_eval
file_info = ['{"file":"file1", "directory":"dir1"}', '{"file":"file2", "directory":"directory2"}']
for item in file_info:
item = literal_eval(item)
print item['file'], item['directory']
both code snippets print:
file1 dir1
file2 directory2
Hope that helps.
I would do:
import re
regx = re.compile('{\s*file\s*:\s*([^,\s]+)\s*'
','
'\s*directory\s*:\s*([^}\s]+)\s*}')
file_info = ['{file:C:\\samples\\123.exe, directory : C:\\}',
'{ file: C:\\samples\\345.exe,directory:C:\\}'
]
for item in file_info:
print '%r\n%s\n' % (item,
regx.search(item).groups())
result
'{file:C:\\samples\\123.exe, directory : C:\\}'
('C:\\samples\\123.exe', 'C:\\')
'{ file: C:\\samples\\345.exe,directory:C:\\}'
('C:\\samples\\345.exe', 'C:\\')