473,653 Members | 3,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2107
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.m onth_name) )
dayName = oneOf( list(calendar.d ay_name) )
date = dayName("dayOfW eek") + monthName("mont h") + integer("day") + \
"," + integer("year")
DATE = Word("=").suppr ess() + "Date:" + date("date") +
Word("=").suppr ess()
NAMEDATA = Combine("name" + integer)("name" ) + ':' + integer("value" )

for match in (IMPORTANT_TEXT | DATE | NAMEDATA).searc hString(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("Da te: ([^=]+)=", 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*********@gm ail.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
7733
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 email posts and been advised to use the email Module and mailbox Module. The blurb from a memeber of this list . Im not at work at the moment So I cant test this out , but if someone could take a look and check that im on the write track as this...
1
2393
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 entire line from the file , headers as well > > >as just the desired body messages . > > > > > >fp = file("/home/chuck/pythonScript/testbox") > > >mb = mailbox.UnixMailbox(fp, > > >email.message_from_file)
26
2478
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 CLASS='myclass'>A bit of text</SPAN>, or Just some text, without tags
1
6177
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... > http://www.derickrethans.nl/parsing_mail_with_php.php > > - is this the class? i am really confused how to plug this into my
0
8283
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8704
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.