On Dec 16, 1:06 am, Rob Schneider
Hello, Rob.
I just tried your Python routine but got an error message stating the
following:
/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/
Python.app/Contents/MacOS/Python: can't open file 'processtext.py':
[Errno 2] No such file or directory
At first I thought it had something to do with the fact that it might
be looking for the file 'processtext.py' in the main directory while I
had it on my desktop. So I transferred the files to the main directory
but nothing changed - the result was the same error message.
Is there something I am missing ?
Thank you,
It means that you launched Python and asked it to load the
'processtext.py' file and it can't find it. Probably because when you
launched Python the file processtext.py and the input file was not in
the current directory. It has to be in the 'current directory' simply
because you did not give a full path to the actual file location. The
computer has to be told what to do. It cannot read the user's mind.
I don't know what you mean by "main directory".
'processtext.py' is the name I suggested you give to the little
programme file. Is this the file name you gave it?
The error message suggests that it can't find the file; so on that that
basis let's go through the steps to take. I will be *very* explicit.
While there are a lot of words here, it is simple.
1. Launch the Mac program "TextEdit" using Finder and from the
Applications Folder.
2. Copy/paste the program I provided previously into TextEdit. Edit
the name of the input file name and output file name you will use. I
suggest "input.txt" and "output.txt". Use whatever you like. Ensure
that the indented portions of the program are, as mentioned previously,
all four spaces (or if not four then must all be the same, e.g. three)..
Do not use tabs, do not use inconsistent indents.
3. Save the file into the "Documents" folder as 'processtext.py'.. (You
can put it in any folder you like, including Desktop ... I just say
Documents just so that I can write this procedure and pick an existing
folder to avoid creating a new folder which adds more steps to the
procedure).
4. Copy the file which holds your text that you wish to transform into
the "Documents" folder. Use Finder.
5. Open the Mac "Terminal" program. You'll probably find it on the
"Dock" but not there, launch it via Finder in the Applications/Utilities
folder (help at Google "launch mac terminal")
6. Unless you changed something, the Mac will put you into terminal
mode in the /Users/[youruserid] folder. Change to the Documents folder
by issuing the command (without the quote marks), "cd Documents" then
press Return Key.
7.The "current directory" will be /Users/[youruserid/Documents". Check
this by issuing the command "pwd" to see the *p*present *w*working
*d*iretory.
8.Confirm that your "processtext.py is there by issuing the command "ls
passwordtext.py" and if it is the Mac will respond by by showing you the
file name. If the file is not present, go back to steps 1, 2, and 3.
This file must be in this folder simply because we keep it simple by not
putting it elsewhere which would require giving a full path name when
launching the program.
9.Confirm that your input file (called "input.txt"???) is in that folder
by issuing the "ls input.txt" file. If it is not there, then go to step
4 above. This file must be in this folder simply because we keep it
simple by not putting it elsewhere which would require giving a full
path name in the programm.
10. Issue the command "python processtext.py" which will a. launch
Python, b. load the "processtext.py" file from the current directory
(Documents), then run that program. It should create the file
output.text (or whatever you called it in step 2 above) into the current
folder (Documents). It should only take a few seconds (depending on
the size of the input file).
11. With Word or TextEdit, look at the output.txt file and see if it
did what you wanted.
12. If it did not, then you need to adjust the Python program to do
what you actually want if you want to use Python. You may also find
that the input data format/pattern is not exactly as expected and
therefore the program may not work. You can then chose to change the
data format/pattern, or make the program more sophisticated to enable
transforming the unexpected format/patter. That's just life when
munging data.
Hi, Rob.
I just tried the Python program as you suggested. Thank you for the
detailed instructions that made every step easy to follow.
I am note sure if the program runs or doesn't. After I press enter
Terminal immediately displays the next line waiting for input and
doesn't show any error message. However, as I go into the Documents
folder I simply can't find the "output.txt" file that the program
should have saved. I also tried a spotlight search but couldn't locate
any file in my system with the name "output.txt" (I did this just to
make sure it had not saved the program to a different location
although I can see that Terminal is looking at the "right" folder).
What do you think may have happened ?
Thank you very much for your help and for the detailed explanation and
instructions.
Joe,
I haven't a clue what happened on your computer and why this is so hard
for you. What do you think is wrong?
I don't have a clue if Spotlight will find a recently-created file. Why
do you think the program will have chosen on it's own to save it
somewhere other than the current directory. It will only do that if you
told it to. You don't say that you ran the program from the Documents
folder. The program, as provided, will put the output file in the same
folder as the input file.
As you can see in the original source code I put in no statements to
show progress of work. This is something that you could have done to
debug it. From the symptoms you provide the program did work and there
were no errors and the output file should have been created in the
current directory. But I don't know where you told the program to put
the file.
Here is a *more* sophisticated version of the program (with a *lot* of
additional exception checking and output statements to show it's
working. It's the same program just with added extras (watch that lines
don't get garbled. remember the four space indent rule):
=====================
#!/usr/bin/env python
# encoding: utf-8
# change these if you want. without folder names,
# the current directory assumed
ifn="input.txt" # inputfile name
ofn="output.txt" # outputfile name
try:
ifh=open("input.txt",'r')
except:
print "EXCEPTION: can't open input file."
try:
ofh=open("output.txt","w")
except:
print "EXCEPTION: can't open output file."
try:
inputtext=ifh.readlines()
except:
print "EXCEPTION: can't read input file."
ifh.close
tab='\t'
for line in inputtext:
t1=line.split(';')
if len(t1)< 2:
print "ERROR: no ';' in input line. Skipping.",t1
else:
title=t1[0]
data=t1[1][:-1]
cnt=len(t1[1].split('.'))
print "...will put %i tab stops in %s;%s" % (cnt,title,data)
try:
ofh.write(tab*cnt+title+' ['+data+']\n')
except:
print "EXCEPTION: can't write output file."
try:
ofh.close()
except:
print "EXCEPTION. Can't close output file."
print "\nDone. input from:%s output to: %s." % (ifn,ofn)
print "Check with the command: 'ls %s'" % (ofn)
===============
Here is the input file I used to test (with one error introduced to
check the error checking):
Body Regions;A01
Abdomen;A01.047
Abdominal Cavity;A01.047.025
Mesocolon;A01.047.025.600.451.535
Omentum;A01.047.025.600.573
Peritoneal Cavity;A01.047.025.600.678
Peritoneal Stomata;A01.047.025.600.700
Retroperitoneal Space;A01.047.025.750
Back;A01.176
Lumbosacral Region;A01.176.519
Sacrococcygeal Region;A01.176.780
BreastA01.236
Mammary Glands, Human;A01.236.249
================
Here is the output printed to the screen for this test:
================
MacBook:documents rmschne$ python transform.py
...will put 1 tab stops in Body Regions;A01
...will put 2 tab stops in Abdomen;A01.047
...will put 3 tab stops in Abdominal Cavity;A01.047.025
...will put 6 tab stops in Mesocolon;A01.047.025.600.451.535
...will put 5 tab stops in Omentum;A01.047.025.600.573
...will put 5 tab stops in Peritoneal Cavity;A01.047.025.600.678
...will put 5 tab stops in Peritoneal Stomata;A01.047.025.600.700
...will put 4 tab stops in Retroperitoneal Space;A01.047.025.750
...will put 2 tab stops in Back;A01.176
...will put 3 tab stops in Lumbosacral Region;A01.176.519
...will put 3 tab stops in Sacrococcygeal Region;A01.176.780
ERROR: no ';' in input line. Skipping. ['BreastA01.236\n']
...will put 3 tab stops in Mammary Glands, Human;A01.236.24
Done. input from:input.txt output to: output.txt.
Check with the command: 'ls -l output.txt'
=================
Here is the result of the ls command:
=================
MacBook:documents rmschne$ ls -l output.txt
-rw-r--r-- 1 rmschne staff 416 18 Dec 08:09 output.txt
=================