简体   繁体   English

从列表中的字符串中提取值-python

[英]Extract values from within strings in a list - python

I have a list in my python code with the following structure: 我的python代码中有一个具有以下结构的列表:

file_info = ['{file:C:\\samples\\123.exe, directory:C:\\}','{file:C:\\samples\\345.exe, directory:C:\\}',...]

I want to extract just the file and directory values for every value of the list and print it. 我只想提取列表中每个值的文件和目录值并打印出来。 With the following code, I am able to extract the directory values: 使用以下代码,我能够提取目录值:

for item in file_info:

    print item.split('directory:')[1].strip('}')

But I am not able to figure out a way to extract the 'file' values. 但是我无法找出一种提取“文件”值的方法。 The following doesn't work: 以下内容不起作用:

print item.split('file:')[1].strip(', directory:C:\}')

Suggestions? 有什么建议吗? If there is any better method to extract the file and directory values other than this, that would be great too. 如果除此之外还有其他更好的方法来提取文件和目录值,那也很好。 Thanks in advance. 提前致谢。

If the format is exactly the same you've provided, you'd better go with using re : 如果格式与您提供的格式完全相同,则最好使用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): 或者,使用字符串replace()strip()split() (有点hackish和脆弱):

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: 如果file_info项只是转储的json项(请注意双引号),则可以使用json将其加载到字典中:

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() : 或者, 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:\\')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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