473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parse specific text in email body to CSV file

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!
Mar 8 '08 #1
2 2090
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
Mar 9 '08 #2
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 <mi*********@gmail.com>
http://pythonwise.blogspot.com

Mar 9 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chuck amadi | last post by:
Hi , Im trying to parse a specific users mailbox (testwwws) and output the body of the messages to a file ,that file will then be loaded into a PostGresql DB at some point . I have read the...
1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
26
by: Charles Law | last post by:
Does anyone have a regex pattern to parse HTML from a stream? I have a well structured file, where each line is of the form <sometag someattribute='attr'>text</sometag> for example <SPAN...
1
by: maconbot | last post by:
hi all, please exuse my email ">" i am working on location. > hey team, thanks for the quick reply. > > i am trying to parse a pop3 account and populate it into flash. > > the how to code......
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.