Code: Alles auswählen
s = '("w1" "w2" ("w3" "w4") ("w5") "w6" ("w7"))'
//und noch eine frage
was macht die -1 da:
Code: Alles auswählen
index[1:-1]
Code: Alles auswählen
s = '("w1" "w2" ("w3" "w4") ("w5") "w6" ("w7"))'
Code: Alles auswählen
index[1:-1]
Kommata nach Ausführungszeichen einfügen und dann in `eval` werfen zum Beispiel.nvidia hat geschrieben:Hallo,wie kann ich diesen String zum Tuple an den "(" oder ")" transformieren?Code: Alles auswählen
s = '("w1" "w2" ("w3" "w4") ("w5") "w6" ("w7"))'
Das nach dem Doppelpunkt gibt das Ende des Slices an; ein negatives Indexwert steht in Python für "von hinten zählen".//und noch eine frage
was macht die -1 da:Code: Alles auswählen
index[1:-1]
Code: Alles auswählen
In [1]: s = '("w1" "w2" ("w3" "w4") ("w5") "w6" ("w7"))'
In [2]: s[1:-1]
Out[2]: '"w1" "w2" ("w3" "w4") ("w5") "w6" ("w7")'
Code: Alles auswählen
from pyparsing import QuotedString, Forward, Group, Suppress, ZeroOrMore
def main():
TUPLE = Forward()
TUPLE << Group(
Suppress('(') + ZeroOrMore(QuotedString('"') | TUPLE) + Suppress(')')
).setParseAction(lambda toks: tuple(toks[0]))
ast = TUPLE.parseString('("w1" "w2" ("w3" "w4") ("w5") "w6" ("w7"))')
print ast[0]
if __name__ == '__main__':
main()
Code: Alles auswählen
#!/usr/bin/env python
# coding: utf-8
import re
TOKENS = re.compile(r'(?P<bl>\()|(?P<br>\))|(?P<data>\w+)|\s')
def tokenize(data):
for token in re.finditer(TOKENS, data):
yield token
def loads(data):
stack = [[]]
for token in tokenize(data):
if token.group("bl"):
stack.append(list())
elif token.group("br"):
tmp = tuple(stack.pop())
stack[-1].append(tmp)
elif token.group("data"):
stack[-1].append(token.group())
else:
continue
return stack.pop().pop()
if __name__ == "__main__":
data = '("w1" "w2" ("w3" "w4") ("w5") "w6" ("w7"))'
print loads(data)
Code: Alles auswählen
import re
from pprint import pprint
SOURCE = '''\
(
("TEXT" "PLAIN" ("CHARSET" "utf-8") NIL NIL "QUOTED-PRINTABLE" 871 27 NIL NIL NIL)
(
("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "QUOTED-PRINTABLE" 2275 29 NIL NIL NIL)
("APPLICATION" "VND.OASIS.OPENDOCUMENT.TEXT" ("NAME" "Ethische Problemfelder.odt") NIL NIL "BASE64" 875794 NIL ("ATTACHMENT" ("FILENAME" "Ethische Problemfelder.odt")) NIL)
("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "7BIT" 92 0 NIL NIL NIL) "MIXED" ("BOUNDARY" "Apple-Mail-4-784936864") NIL NIL
) "ALTERNATIVE" ("BOUNDARY" "Apple-Mail-3-784936862") NIL NIL
)'''
def pattern_dispatch(patterns_and_actions, source):
regex = re.compile('|'.join('(%s)' % r for r, _ in patterns_and_actions))
actions = [a for _, a in patterns_and_actions]
i = 0
for match in iter(lambda: regex.match(source, i), None):
actions[match.lastindex - 1](match.group())
i = match.end()
return i
def parse(source):
stack = [[]]
pattern_dispatch(
[
(r'\(', lambda _: stack.append(list())),
(r'\)', lambda _: stack[-2].append(tuple(stack.pop()))),
(r'NIL', lambda _: stack[-1].append(None)),
(r'[0-9]+', lambda t: stack[-1].append(int(t))),
(r'"[^"]*"', lambda t: stack[-1].append(t[1:-1])),
(r'\s+', lambda _: None),
],
source
)
return stack[0][0]
def main():
pprint(parse(SOURCE))