2015-06-18 10 views
5

Chcę analizowania ciąg aby wyodrębnić wszystkie podciągi w nawiasach klamrowych:Wyciąg podciągi w Pythonie

'The value of x is {x}, and the list is {y} of len {}' 

powinna produkować:

(x, y) 

Następnie chcę sformatować ciąg do wydrukowania początkowa ciąg z wartościami:

str.format('The value of x is {x}, and the list is {y} of len {}', x, y, len(y)) 

Jak mogę to zrobić?

Example usage: 
def somefunc(): 
    x = 123 
    y = ['a', 'b'] 
    MyFormat('The value of x is {x}, and the list is {y} of len {}',len(y)) 

output: 
    The value of x is 123, and the list is ['a', 'b'] of len 2 
+1

Więc po ekstrakcji x i y, masz zamiar go zmienić? Przed ponownym wydrukiem – Bharadwaj

+1

Jaki jest cel etapu pośredniego? To wcale nie pomaga w następnej części! – jonrsharpe

+1

Twoje pytanie nie jest jasne !! czy możesz pokazać nam [Minimalny, kompletny i sprawdzalny przykład] (http://stackoverflow.com/help/mcve) o tym, co chcesz zrobić? lub o kodzie, który próbowałeś do tej pory? – Kasramvd

Odpowiedz

6

Można użyć string.Formatter.parse:

Loop over the format_string and return an iterable of tuples (literal_text, field_name, format_spec, conversion). This is used by vformat() to break the string into either literal text, or replacement fields.

The values in the tuple conceptually represent a span of literal text followed by a single replacement field. If there is no literal text (which can happen if two replacement fields occur consecutively), then literal_text will be a zero-length string. If there is no replacement field, then the values of field_name, format_spec and conversion will be None.

from string import Formatter 

s = 'The value of x is {x}, and the list is {y} of len {}' 

print([t[1] for t in Formatter().parse(s) if t[1]]) 
['x', 'y'] 

Nie wiem, jak to naprawdę pomaga, co chce zrobić, jak można po prostu przejść xiy do str.format w funkcji lub użyj ** miejscowi:

def somefunc(): 
    x = 123 
    y = ['a', 'b'] 
    print('The value of x is {x}, and the list is {y} of len {}'.format(len(y),**locals())) 

Jeśli chcesz wydrukować nazwane argumenty, możesz dodać wyjście Formattera:

def somefunc(): 
    x = 123 
    y = ['a', 'b'] 
    print("The named args are {}".format([t[1] for t in Formatter().parse(s) if t[1]])) 
    print('The value of x is {x}, and the list is {y} of len {}'.format(len(y), **locals())) 

Które wyjście:

The named args are ['x', 'y'] 
The value of x is 123, and the list is ['a', 'b'] of len 2 
+0

Próbowałem Twojego rozwiązania w następujący sposób: _print (s.format (** locals())) _ i otrzymałem następujący komunikat o błędzie _IndexError: indeks krotki poza zasięgiem –

+0

co to jest s w pliku 's.format'? –

0

Można użyć re.findall

>>> import re 
>>> s = 'The value of x is {x}, and the list is {y} of len {}' 
>>> re.findall(r'\{([^{}]+)\}', s) 
['x', 'y'] 
>>> tuple(re.findall(r'\{([^{}]+)\}', s)) 
('x', 'y') 
0

Co robisz po wyodrębnić wartość?

import re 
st = "The value of x is {x}, and the list is {y} of len {}" 
exp = re.compile(r"\{(.+?)\}") 

print(tuple(exp.findall(st))) 

Wyjście jest

('x', 'y')