Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Parse specific text in email body to CSV file

Reply
Thread Tools

Parse specific text in email body to CSV file

 
 
dpw.asdf@gmail.com
Guest
Posts: n/a
 
      03-08-2008
I have been searching all over for a solution to this. I am new to
Python, so I'm a little lost. Any pointers would be a great help. I
have a couple hundred emails that contain data I would like to
incorporate into a database or CSV file. I want to search the email
for specific text.

The emails basically look like this:



random text _important text:_15648 random text random text random text
random text
random text random text random text _important text:_15493 random text
random text
random text random text _important text:_11674 random text random text
random text
===============Date: Wednesday March 5, 2008================
name1: 15 name5: 14

name2: 18 name6: 105

name3: 64 name7: 2

name4: 24 name8: 13



I want information like "name1: 15" to be placed into the CSV with the
name "name1" and the value "15". The same goes for the date and
"_important text:_15493".

I would like to use this CSV or database to plot a graph with the
data.

Thanks!
 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      03-09-2008
On Mar 8, 4:20*pm, dpw.a...@gmail.com wrote:
> I have been searching all over for a solution to this. I am new to
> Python, so I'm a little lost. Any pointers would be a great help. I
> have a couple hundred emails that contain data I would like to
> incorporate into a database or CSV file. I want to search the email
> for specific text.
>
> The emails basically look like this:
>
> random text _important text:_15648 random text random text random text
> random text
> random text random text random text _important text:_15493 random text
> random text
> random text random text _important text:_11674 random text random text
> random text
> ===============Date: Wednesday March 5, 2008================
> name1: 15 * * * * * * * *name5: 14
>
> name2: 18 * * * * * * * *name6: 105
>
> name3: 64 * * * * * * * *name7: 2
>
> name4: 24 * * * * * * * *name8: 13
>
> I want information like "name1: 15" to be placed into the CSV with the
> name "name1" and the value "15". The same goes for the date and
> "_important text:_15493".
>
> I would like to use this CSV or database to plot a graph with the
> data.
>
> Thanks!


This kind of work can be done using pyparsing. Here is a starting
point for you:

from pyparsing import Word, oneOf, nums, Combine
import calendar

text = """
random text _important text:_15648 random text random text random
text
random text
random text random text random text _important text:_15493 random
text
random text
random text random text _important text:_11674 random text random
text
random text
===============Date: Wednesday March 5, 2008================
name1: 15 name5: 14

name2: 18 name6: 105

name3: 64 name7: 2

name4: 24 name8: 13
"""

integer = Word(nums)

IMPORTANT_TEXT = "_important text:_" + integer("value")
monthName = oneOf( list(calendar.month_name) )
dayName = oneOf( list(calendar.day_name) )
date = dayName("dayOfWeek") + monthName("month") + integer("day") + \
"," + integer("year")
DATE = Word("=").suppress() + "Date:" + date("date") +
Word("=").suppress()
NAMEDATA = Combine("name" + integer)("name") + ':' + integer("value")

for match in (IMPORTANT_TEXT | DATE | NAMEDATA).searchString(text):
print match.dump()

Prints:

['_important text:_', '15648']
- value: 15648
['_important text:_', '15493']
- value: 15493
['_important text:_', '11674']
- value: 11674
['Date:', 'Wednesday', 'March', '5', ',', '2008']
- date: ['Wednesday', 'March', '5', ',', '2008']
- day: 5
- dayOfWeek: Wednesday
- month: March
- year: 2008
- day: 5
- dayOfWeek: Wednesday
- month: March
- year: 2008
['name1', ':', '15']
- name: name1
- value: 15
['name5', ':', '14']
- name: name5
- value: 14
['name2', ':', '18']
- name: name2
- value: 18
['name6', ':', '105']
- name: name6
- value: 105
['name3', ':', '64']
- name: name3
- value: 64
['name7', ':', '2']
- name: name7
- value: 2
['name4', ':', '24']
- name: name4
- value: 24
['name8', ':', '13']
- name: name8
- value: 13

Find out more about pyparsing at http://pyparsing.wikispaces.com.

-- Paul
 
Reply With Quote
 
 
 
 
Miki
Guest
Posts: n/a
 
      03-09-2008
Hello,
>

I have been searching all over for a solution to this. I am new to
> Python, so I'm a little lost. Any pointers would be a great help. I
> have a couple hundred emails that contain data I would like to
> incorporate into a database or CSV file. I want to search the email
> for specific text.
>
> The emails basically look like this:
>
> random text _important text:_15648 random text random text random text
> random text
> random text random text random text _important text:_15493 random text
> random text
> random text random text _important text:_11674 random text random text
> random text
> ===============Date: Wednesday March 5, 2008================
> name1: 15 * * * * * * * *name5: 14
>
> name2: 18 * * * * * * * *name6: 105
>
> name3: 64 * * * * * * * *name7: 2
>
> name4: 24 * * * * * * * *name8: 13
>
> I want information like "name1: 15" to be placed into the CSV with the
> name "name1" and the value "15". The same goes for the date and
> "_important text:_15493".
>
> I would like to use this CSV or database to plot a graph with the
> data.

import re

for match in re.finditer("_([\w ]+):_(\d+)", text):
print match.groups()[0], match.groups()[1]

for match in re.finditer("Date: ([^=]+)=", text):
print match.groups()[0]

for match in re.finditer("(\w+): (\d+)", text):
print match.groups()[0], match.groups()[1]


Now you have two problems

HTH,
--
Miki <>
http://pythonwise.blogspot.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to parse a csv...... a csv that has CRLF in the fields sso Java 20 04-26-2009 11:04 AM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
all the text (including tags) between <body> .. </body> tarakparekh@yahoo.com Perl Misc 5 09-07-2005 11:40 PM
How to Parse a CSV formatted text file Ram Laxman C Programming 22 02-11-2004 06:19 PM
How to Parse a CSV formatted text file Ram Laxman C++ 22 02-11-2004 06:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57