We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,534 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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)
3
Contributors
8
Replies
17 Hours
Discussion Span
1 Year Ago
Last Updated
9
Views
Question
Answered
tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

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
Skill Endorsements: 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,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

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
Skill Endorsements: 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
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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
Skill Endorsements: 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 of os.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
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

tq

tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by thines01 and Gribouillis

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0957 seconds using 2.69MB