0

I have the following data copied to an xml file from an excel sheet,I need to create a table out of this data and out the table to microsoft outlook,initially I was thinking of creating lists of everycolumn,constuct HTMl code out of it and out put to excel...how do split every line because title data can be anything,mixture of spaces etc...anyone has done this before ,appreciate good suggestions,waiting for feedback

CR  FA  CL  TITLE
12345   Dta 656885  Age out SRR values from buffer in Beacon miss scenario
16534   fta 656886  To Record stack Event Logging
76849   cta 654661  To Record HAL and FLM SW Event Logging
45678   CT  656928  CR1234: BT doesn’t work that Riva neither sends HCI Evt for HID ACL data nor response to CI after entering into real sniffer rat mode.
1
  • 1
    I don't quite follow what role Python is playing here. Excel, for all its flaws, is pretty good at making nice tables. You also say both that the table is going to Outlook and that you're going to build an HTML (an HTML table?) and output to Excel, which is a little confusing. Commented Nov 7, 2012 at 19:17

4 Answers 4

2

Use csv.DictReader to get your data into a dictionary:

>>> import csv
>>> with open('t.txt') as f:
...    reader = csv.DictReader(f,dialect='excel-tab')
...    rows = list(reader)
... 
>>> rows[0]
{'TITLE': 'Age out SRR values from buffer in Beacon miss scenario',
 'CR': '12345', 'CL': '656885', 'FA': 'Dta'}
Sign up to request clarification or add additional context in comments.

2 Comments

@Burkhan -import re input="""CR FA CL TITLE 12345 Dta 656885 Age out SRR values from buffer in Beacon miss scenario 16534 fta 656886 To Record stack Event Logging 76849 cta 654661 To Record HAL and FLM SW Event Logging 45678 CT 656928 CR 1234: BT doesn't work that Riva neither sends HCI Evt for HID ACL data nor response to CI after entering into real sniffer rat mode.""" for i in input.splitlines(): index = re.split(r'\W+',i,3) print "INDEX" print index
So you are saying, you have a solution? Otherwise what is the point of the comment, or even the question?
1

If only last column is more complex you could use re.split ->

import re
input="""CR FA  CL  TITLE
12345   Dta 656885  Age out SRR values from buffer in Beacon miss scenario
16534   fta 656886  To Record stack Event Logging
76849   cta 654661  To Record HAL and FLM SW Event Logging
45678   CT  656928  CR1234: BT doesn’t work that Riva neither sends HCI Evt for HID ACL data nor response to CI after entering into real sniffer rat mode."""

[re.split(r'\W+',i,3) for i in input.splitlines()]

4 Comments

can you please explain what exactly does "re.split(r'\W+',i,3) does do?I kind of understand you are splitting based on a word but not sure how does it help for the column...
nevermind I figured out,is there an easier way to construct a table out of it to export to outlook...currently logic am using is to construct html table code and output that to outlook..
from doc -> \w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]. With LOCALE, it will match the set [0-9_] plus characters defined as letters for the current locale. \W Matches the complement of \w.
but I agree better could be \s Matches any whitespace character; equivalent to [ \t\n\r\f\v].
0

If you can find a pattern for what each line has in it then you can parse the line easier. Is each line 1 record? If so see below.

for line in open('myfile','r').readlines():
    # Do whatever you need to the line variable.

2 Comments

@ajon - my question is more interms of ideas on how to construct the table...pattern is always the same as above....number,string,number,sentence...first line will represent what they mean which should be the header
@ajon - I want to read each line and split based on space and construct lists for each column out of it but this fails for the last column which is a sentence
0

make sure you use a proper separator for your csv ... (see attached tweaked data file,uses tab separation) https://dl.dropbox.com/u/18004504/data.csv

import pandas as pd
df=pd.DataFrame.from_csv("data.csv",sep="\t",parse_dates=False)


print df
for row in df.iterrows(): 
    print row
    print "-------"

1 Comment

I need to construct a table to output to outlook ..import re input="""CR FA CL TITLE 12345 Dta 656885 Age out SRR values from buffer in Beacon miss scenario 45678 CT 656928 CR 1234: BT doesn't work that Riva neither sends HCI Evt for HID ACL data nor response to CI after entering into real sniffer rat mode.""" for i in input.splitlines(): index = re.split(r'\W+',i,3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.