<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 5.50.4937.800" name=GENERATOR></HEAD>
<BODY>
<DIV> </DIV>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Tahoma
size=2>-----Original Message-----<BR><B>From:</B> David Talaga
[mailto:dtalaga@novodynamics.com]<BR><B>Sent:</B> Thursday, April 15, 2004 10:49
AM<BR><B>To:</B> Roger Merchberger<BR><B>Subject:</B> RE: [Tutor] deleting CR
within files<BR><BR></FONT></DIV>
<P><FONT size=2>Ok, here is what we have now:<BR><BR><FONT color=#ff0000>import
os<BR>import Tkinter<BR>import dialog<BR>import sys<BR>import
re<BR><BR>root=Tkinter.Tk()<BR>f=Tkinter.Button(root, text="Find Files",
command=dialog.dialog).pack()<BR>x=Tkinter.Button(root, text="Close",
command=sys.exit).pack()<BR><BR>sub = re.sub<BR>fileName=
dialog.dialog<BR>in_file = open(fileName,'r')<BR>out_file = open(fileName +
'_cleaned', 'w')<BR>for i in in_file.readlines():<BR>
out_file.write(i[:1] + '\n')<BR> print 'File
cleaned'<BR>in_file.close()<BR>out_file.close()<BR><BR>Tkinter.mainloop()<BR></FONT><BR>Now
when I run it is says this:<BR><FONT color=#ff0000>Traceback (most recent call
last):<BR> File "<pyshell#0>", line 1, in ?<BR>
execfile('crgui.py')<BR> File "crgui.py", line 13, in
?<BR> in_file = open(fileName,'r')<BR>TypeError: coercing to
Unicode: need string or buffer, function found<BR></FONT></FONT><FONT
size=2></FONT></P>
<P><FONT size=2><FONT face=Arial>I did not know that I was using unicode up
there... I am realy at a loss. The program runs fine as far ad the
dialog box coming up and the print statement executing. I just dont know
what the error is saying. Do I need a string in place of <FONT
color=#ff0000>fileName </FONT><FONT color=#000000>and if I do how would I go
about putting a string in there and still trying to call </FONT><FONT
color=#ff0000>dialog.dialog</FONT><FONT color=#000000>? Any help or
direction would be greatly appreciated and also the option to take my first born
son. (Not realy, well, OK, if you realy thikn it's a fair
trade...)</FONT></FONT></FONT></P>
<P><FONT size=2><FONT face=Arial>David Talaga</FONT></FONT></P>
<P><FONT size=2> </P>
<P><FONT face=Arial color=#0000ff></FONT><BR>-----Original Message-----<BR>From:
Roger Merchberger [<A
href="mailto:zmerch@30below.com">mailto:zmerch@30below.com</A>]<BR>Sent:
Thursday, April 15, 2004 10:23 AM<BR>To: David Talaga<BR>Subject: Re: [Tutor]
deleting CR within files<BR><BR><BR>Rumor has it that David Talaga may have
mentioned these words:<BR>>Hi all!<BR>>Here is my next problem. I am
trying to rid some files of pesky <CR> from<BR>>files. Here is my
code. Any help would be great!<BR><BR>What platform? If it's *nix, you're
using the wrong tool for the job.<BR>There's command line utilities that can do
that, usually named 'dos2unix'<BR>and 'unix2dos' to either remove or insert \r
characters.<BR><BR>If you're in windows, there are unix-ish utilities you can
download (google<BR>for cygwin) that give you a bashprompt on WinNT4/2K/XP and
methinks 98+,<BR>but don't quote me on that last part...<BR><BR>Also, it would
help us to know what type of files you're editing - are they<BR>basic text files
where the \r is either the last or next-to-last character<BR>in the line? If so,
you're working too hard... Try this:<BR><BR>>import os, dialog #dialog is a
dialog box function<BR>><BR>>fileName = dialog<BR>><BR>>in_file =
open(fileName,'r').readlines() #I read the whole file at once<BR>>out_file =
open(fileName + '_cleaned', 'w') #I opne the file and rename it<BR>>as
filename_cleaned.<BR>>for i in in_file: ## I thikn you get the
rest<BR>> out_file.write(i[:-2] + '\n') # Use
this line if you're removing<BR>> \r\n or
\n\r<BR>> out_file.write(i[:-1]) # use this line if
you're removing \n\r<BR>> out_file.write(i[:-1] +
'\n') # use this line if you're removing \r
only<BR>>out_file.close()<BR><BR>If you need to find a \r in the middle of a
string, regular expressions<BR>(the re module) is still overkill - try
this:<BR><BR>import string<BR>teststr = "here's the string with \r in
it"<BR>wherecr = string.index(teststr,'\r')<BR>newstr = teststr[:wherecr] +
[wherecr+1:]<BR><BR>=-=-=-=-=-=-=-=-=-=-=<BR><BR>if you have multiple \r's in a
line, you'd need to add a flag & a while<BR>loop - I'll leave that up as an
exercise to the reader... ;-)<BR><BR>HTH,<BR>Roger "Merch"
Merchberger<BR><BR>--<BR>Roger "Merch" Merchberger | A new truth in
advertising slogan<BR>sysadmin, Iceberg Computers | for MicroSoft: "We're not
the
oxy...<BR>zmerch@30below.com
|
...in oxymoron!"<BR></P></FONT></BODY></HTML>