[Tutor] Strings and file saving

Tim Condit timc@ans.net
Fri, 18 Aug 2000 15:27:36 -0400 (EDT)


Hi Dan, 

I took out all the comments, ran it again, and it sort of works for me:

import sys
import string

in_file = open("data_file","r")
out_file = open("junk2.txt","w+")

for line in in_file.readlines():
        if string.find(line,'$')>= 0:
                a = line
        out_file.write(a)
out_file.close()
in_file.close()

----------------------------------------

[timc@neutrino ~] cat data_file 
hi there I'm a line with a $ sign.

and I'm not. 

----------------------------------------

[timc@neutrino ~] python dan.py
[timc@neutrino ~] cat junk2.txt 
hi there I'm a line with a $ sign.
hi there I'm a line with a $ sign.
hi there I'm a line with a $ sign.

----------------------------------------

Okay, so on first glance, what appears to be happening is that all three
lines are matching.. I don't have time to look at it much closer at the
moment, though. Odd that I got a different result than you with no
modification.

good luck,
Tim Condit
UUNet Network Quality Services
734-214-7548
tcondit@uu.net



On Fri, 18 Aug 2000, Daniel D. Laskey, CPA wrote:

> HEY YOU GUYS!!
> 
> Objective: 
> 1)  Read this ASCII file that is space deliminated;
> 2)  Pull out only the lines that have a '$' characters in the line;
> 3)  Save the data to a new file.
> 
> #  ----------- Code -----------  
> # import sys and string
> import sys
> import string
> 
> # get the appropriate data file
> in_file = open("data_file","r")
> 
> # create the file to export the data to
> out_file = open("junk2.txt","w+")
> 
> # read each line in the file	
> for line in in_file.readlines():
> 	if string.find(line,'$')>= 0:
> #		# "print line" works well
> #		#print line
> 		a = line
> #	# write the new file you created to the file "junk2.txt" 
> 	out_file.write(a)
> out_file.close()
> in_file.close()
> #  ----------- End of Code --------------
> 
> Traceback (innermost last):
>   File "today6.py", line 19, in ?
>     out_file.write(a)
> NameError: a
> 
> I think I am a classic case of PDD ... Python Deficit Disorder :).
> At best I'm one of those slow learners, so bear with me, please.
> 
> Dan
> 
> ---------------------------------------------------------------------------------------
> | Daniel D. Laskey, CPA-------------------dlaskey@laskeycpa.com
> | Daniel D. Laskey Company, P.C.------231-723-8305 Voice
> |                                                                  231-723-6097 Fax
> | Certified Public Accountants
> | 507 Water Street
> | Manistee, MI  49660
> ---------------------------------------------------------------------------------------
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>