954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File Parsing

hi,

i would like to parse an input file (input.txt) into output file (output.txt) which i'll into excel. appreciate any advice on how to parse the input.txt into output.txt.

thanks
johnny

Attachments input.txt (0.84KB) output.txt (0.52KB)
tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

What are the rules on the data you want kept?
From what I see it's:
1) Delete the header
2) Ignore ALL punctuation (except asterisk * )
3) Ignore all text after the last parenthesis
4) If it contains "output" also ignore the last field

Is that correct?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

hi, the rules are:
1) either remove the header or keep it as below format
2) Ignore ALL punctuation (except asterisk * )
3) Ignore all text after the last parenthesis
the end result is:
num cell port function safe ccell
17 BC_1 CLK input X
16 BC_1 OC_NEG input X
16 BC_1 * control 1
15 BC_1 D1 input X
14 BC_1 D2 input X
13 BC_1 D3 input X
12 BC_1 D4 input X
11 BC_1 D5 input X
10 BC_1 D6 input X
9 BC_1 D7 input X
8 BC_1 D8 input X
7 BC_1 Q1 output3 X 16 1
6 BC_1 Q2 output3 X 16 1
5 BC_1 Q3 output3 X 16 1
4 BC_1 Q4 output3 X 16 1
3 BC_1 Q5 output3 X 16 1
2 BC_1 Q6 output3 X 16 1
1 BC_1 Q7 output3 X 16 1
0 BC_1 Q8 output3 X 16 1

i tried but it gave index error due to last sentence is longer than the initial sentence. not sure how to proceed, pls help. tq

import re
lines=open("input.txt",'r').readlines()

for line in lines:
    a=re.findall(r'\w+',line)
    print re.findall(r'\w+',line)
    print a[0],a[1],a[2],a[3],a[4],a[5],a[6]
tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Well, this is not very elegant, but it's functional:

import re

fileIn = open("input.txt", "rb")
fileOut = open("output.txt", "wb")

for strData in fileIn:
    strData = strData.split('-')[0]
    if("input" in strData):
        a=re.split("\W+", strData)
        fileOut.write(a[1]+' '+a[2]+' '+a[3]+' '+a[4]+' '+a[5]+' '+a[6]+'\n')
   
    if("output" in strData):
        a=re.split("\W+", strData)
        fileOut.write(a[1]+' '+a[2]+' '+a[3]+' '+a[4]+' '+a[5]+' '+a[6]+' '+a[7]+' '+a[8]+'\n')
       
fileOut.close()
fileIn.close()
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

great. the output is right. it's strange though when i open using output.txt using notepad, it shows:
17 BC_1 CLK input X 16 BC_1 OC_NEG input X 15 BC_1 D 1

but when i use other editor like wordpad it shows (which is what i want)
17 BC_1 CLK input X
16 BC_1 OC_NEG input X
15 BC_1 D 1

i also notice that in the code already inserted \n so it should write to a new line,not sure why notepad is showing differently.

anyway this certainly works. do you mind explaining the logic of your code, i'm trying to understand why there are 2 ifs. and the meaning of a=re.split("\W+", ?

thanks a lot

tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

great. the output is right. it's strange though when i open using output.txt using notepad, it shows: 17 BC_1 CLK input X 16 BC_1 OC_NEG input X 15 BC_1 D 1

but when i use other editor like wordpad it shows (which is what i want) 17 BC_1 CLK input X 16 BC_1 OC_NEG input X 15 BC_1 D 1

i also notice that in the code already inserted \n so it should write to a new line,not sure why notepad is showing differently.

anyway this certainly works. do you mind explaining the logic of your code, i'm trying to understand why there are 2 ifs. and the meaning of a=re.split("\W+", ?

thanks a lot


About the end of line issue, try to open the output file in mode "w" instead of "wb" to see if it changes something.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

you are right, it works after i use the 'w' option instead of 'wb'. any comment why notepad view differently? tq

tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
you are right, it works after i use the 'w' option instead of 'wb'. any comment why notepad view differently? tq


See the doc ofos.linesep for more information.

Also a[1]+' '+a[2]+' '+a[3]+' '+a[4]+' '+a[5]+' '+a[6] is best written ' '.join(a[1:7])

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 
tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: