2010-05-09 17 views
6

मैं निम्नलिखित की तरह एक स्ट्रिंग है:यादृच्छिक उद्धरण के साथ Pyparsing सीएसवी स्ट्रिंग

<118>date=2010-05-09,time=16:41:27,device_id=FE-2KA3F09000049,log_id=0400147717,log_part=00,type=statistics,subtype=n/a,pri=information,session_id=o49CedRc021772,from="[email protected]",mailer="mta",client_name="example.org,[194.177.17.24]",resolved=OK,to="[email protected]",direction="in",message_length=6832079,virus="",disposition="Accept",classifier="Not,Spam",subject="=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?=" 

मैं सीएसवी मॉड्यूल का उपयोग कर की कोशिश की और यह फिट नहीं था, क्योंकि मैं अनदेखी करने के लिए क्या उद्धृत है एक तरह से नहीं मिली है। पायपार्सिंग एक बेहतर उत्तर की तरह लग रहा था लेकिन मुझे सभी व्याकरण घोषित करने का कोई तरीका नहीं मिला है।

वर्तमान में, मैं इसे पार्स करने के लिए अपनी पुरानी पर्ल स्क्रिप्ट का उपयोग कर रहा हूं, लेकिन मुझे यह पायथन में लिखा जाना चाहिए। यदि आपको मेरी पर्ल स्निपेट की ज़रूरत है तो मुझे इसे प्रदान करने में खुशी होगी।

किसी भी मदद की सराहना की जाती है।

उत्तर

5

मुझे यकीन है कि आप वास्तव में क्या देख रहे हैं नहीं कर रहा हूँ, लेकिन

import re 
data = "date=2010-05-09,time=16:41:27,device_id=FE-2KA3F09000049,log_id=0400147717,log_part=00,type=statistics,subtype=n/a,pri=information,session_id=o49CedRc021772,from=\"[email protected]\",mailer=\"mta\",client_name=\"example.org,[194.177.17.24]\",resolved=OK,to=\"[email protected]\",direction=\"in\",message_length=6832079,virus=\"\",disposition=\"Accept\",classifier=\"Not,Spam\",subject=\"=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?=\"" 
pattern = r"""(\w+)=((?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^\\,"'])+)""" 
print(re.findall(pattern, data)) 

आप

[('date', '2010-05-09'), ('time', '16:41:27'), ('device_id', 'FE-2KA3F09000049'), 
('log_id', '0400147717'), ('log_part', '00'), ('type', 'statistics'), 
('subtype', 'n/a'), ('pri', 'information'), ('session_id', 'o49CedRc021772'), 
('from', '"[email protected]"'), ('mailer', '"mta"'), 
('client_name', '"example.org,[194.177.17.24]"'), ('resolved', 'OK'), 
('to', '"[email protected]e.org"'), ('direction', '"in"'), 
('message_length', '6832079'), ('virus', '""'), ('disposition', '"Accept"'), 
('classifier', '"Not,Spam"'), 
('subject', '"=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?="') 
] 

आप (mystring.strip("'\"") का प्रयोग करके) बाद में उद्धृत तार को साफ करने के लिए चाहते हो सकता देता है।

EDIT: यह रेगेक्स अब भी उद्धृत तारों (a="She said \"Hi!\"") के अंदर से बच निकले उद्धरणों को सही तरीके से संभालता है। regex के

स्पष्टीकरण:

(\w+)=((?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^\\,"'])+) 

(\w+): पहचानकर्ता का मिलान करें और कोई backreference में कब्जा। 1

=: मैच एक =

(: निम्नलिखित कैद backreference में कोई। 2:

(?::

"(?:\\.|[^\\"])*": निम्न में से एक एक दोहरे उद्धरण, या तो शून्य या निम्न में से अधिक के बाद: एक भाग निकले चरित्र या एक गैर उद्धरण/गैर बैकस्लैश चरित्र, एक और द्वारा पीछा किया दोहरे उद्धरण

|: या

'(?:\\.|[^\\'])*':, ऊपर देखें सिर्फ एकल उद्धरण के लिए।

|: या

[^\\,"']: एक चरित्र न एक बैकस्लैश, अल्पविराम, और न ही एक बोली है।

)+: जितनी बार संभव हो सके कम से कम एक बार दोहराएं।

): कैप्चरिंग समूह संख्या का अंत। 2.

+0

धन्यवाद इस मैं क्या जरूरत थी। – gtfx

+0

इस तरह आप regex करते हैं !! :) – jathanism

6

विज्ञापन-हाक रेगेक्स का उपयोग करने के बजाय मौजूदा पार्सर का लाभ उठाना बेहतर हो सकता है।

parse_http_list(s) 
    Parse lists as described by RFC 2068 Section 2. 

    In particular, parse comma-separated lists where the elements of 
    the list may include quoted-strings. A quoted-string could 
    contain a comma. A non-quoted string could have quotes in the 
    middle. Neither commas nor quotes count if they are escaped. 
    Only double-quotes count, not single-quotes. 

parse_keqv_list(l) 
    Parse list of key=value strings where keys are not duplicated. 

उदाहरण:

>>> pprint.pprint(urllib2.parse_keqv_list(urllib2.parse_http_list(s))) 
{'<118>date': '2010-05-09', 
'classifier': 'Not,Spam', 
'client_name': 'example.org,[194.177.17.24]', 
'device_id': 'FE-2KA3F09000049', 
'direction': 'in', 
'disposition': 'Accept', 
'from': '[email protected]', 
'log_id': '0400147717', 
'log_part': '00', 
'mailer': 'mta', 
'message_length': '6832079', 
'pri': 'information', 
'resolved': 'OK', 
'session_id': 'o49CedRc021772', 
'subject':'=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?=', 
'subtype': 'n/a', 
'time': '16:41:27', 
'to': '[email protected]', 
'type': 'statistics', 
'virus': ''} 
+0

क्रेडिट @Piotr Czapla पर जाएं http://stackoverflow.com/questions/1349367/parse-an-http-request- प्राधिकरण-header-with-python/1349626#1349626 – jfs

+0

ठीक वाह, यह वास्तव में काफी मणि है एक समाधान के। धन्यवाद। – jathanism

+0

उत्कृष्ट, खासकर जब से यह पहले से ही अनावश्यक उद्धरणों को स्ट्रिप्स करता है। खैर, यह आपके लिए पाइथन की सुंदरता है - बैटरी शामिल हैं। (हालांकि मेरा regex भी बुरा नहीं है :)) –

संबंधित मुद्दे