3 Replies - 9934 Views - Last Post: 24 February 2009 - 05:55 AM Rate Topic: -----

#1 sunil99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-February 09

i need to parse text file data in to csv by using python script

Posted 20 February 2009 - 02:49 AM

file = open(a.txt, 'r')
logfile = open(b.txt, 'w')
searchstrings = ('Name1','Name2')
for line in file.readlines():
    for word in searchstrings:
         if word in line:
            logfile.writelines(line)
file.close()


i am not able to convert the text file in to csv file can you please suggest a way or how to do it how to go forward

my text file looks like this

Name1 DRAWING
X:0,U,W
6846

Name2 CAL_ID
X:0,U,W
6930

i want them to display in a file as name1 name 2 values etc in csv file
i tried till this but not able to acheive it

MOD EDIT: Added code tags.

Is This A Good Question/Topic? 0
  • +

Replies To: i need to parse text file data in to csv by using python script

#2 Delta62  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 19-February 09

Re: i need to parse text file data in to csv by using python script

Posted 20 February 2009 - 01:55 PM

I am a bit confused here. How exactly should these files appear once they have been put into CSV format? Do you want each token to be followed by a comma?
Was This Post Helpful? 0
  • +
  • -

#3 sunil99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-February 09

Re: i need to parse text file data in to csv by using python script

Posted 23 February 2009 - 08:53 PM

View PostDelta62, on 20 Feb, 2009 - 12:55 PM, said:

I am a bit confused here. How exactly should these files appear once they have been put into CSV format? Do you want each token to be followed by a comma?

i want each of them to be displayed in rows and columns like following
under the following headings

name variable type default value

Name1 DRAWING X:0,U,W 6846

Name2 CAL_ID X:0,U,W 6930

can you please suggets a way or how to proceed forward from the following code
please help me as soon as possible
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: i need to parse text file data in to csv by using python script

Posted 24 February 2009 - 05:55 AM

Some pseudocode which might help:

while not eof of input file
	readline into line
	if line in searchstrings
		append line to output file and add space
		while true
			readline into line
			if eof break from loop
			if line is empty write newline to output file and break from loop
			write line to output file and add space


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1