Python questions OT

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Python questions OT

Art Kendall
This is OT but I know that there are some PYTHON users on this list and I am finally getting back to starting with python after a long delay.

1) Is there a PYTHON list someplace?

2) Where would I find the documentation for Python error numbers?

3) My python that starts with this
import re, textwrap
# read the combined file and split into individual papers
alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
papers= re.split(r'FEDERALIST No\.'," ".join(alltext))
print len(papers)

outfile = file("C:\Users\Art\desktop\fed\federalistCounts.txt", "w")
 
words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

gets this error message:

>>>
Traceback (most recent call last):
  File "C:\Users\Art\Desktop\fed\countFedWords.py", line 5, in <module>
    alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\\Art\\Desktop\x0ced\x0ceder16v2.txt'
>>>

the filespec python does not seem to like is not what is in the code.   Also the file does exist. 

4) The list of words is now hard coded in the python code.

How would I replace
words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

with a command to read the list from another file?


Art Kendall
Social Research Consultants

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Python questions OT

Jon K Peck

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Art Kendall <[hidden email]>
To: [hidden email]
Date: 05/03/2010 03:02 PM
Subject: [SPSSX-L] Python questions   OT
Sent by: "SPSSX(r) Discussion" <[hidden email]>





This is OT but I know that there are some PYTHON users on this list and I am finally getting back to starting with python after a long delay.

1) Is there a PYTHON list someplace?

There is a version active Python language list at http://groups.google.com/group/comp.lang.python/topics
but they won't know much about SPSS in particular.  Besides this list, you can post to the forums at Developer Central

2) Where would I find the documentation for Python error numbers?

>>>The Python language and standard library generally don't use error numbers.  Some SPSS messages do, but there is no additional documentation for these.

3) My python that starts with this
import re, textwrap
# read the combined file and split into individual papers
alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
papers= re.split(r'FEDERALIST No\.'," ".join(alltext))
print len(papers)

outfile = file("C:\Users\Art\desktop\fed\federalistCounts.txt", "w")

words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

gets this error message:

>>>
Traceback (most recent call last):
 File "C:\Users\Art\Desktop\fed\countFedWords.py", line 5, in <module>
   alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\\Art\\Desktop\x0ced\x0ceder16v2.txt'
>>>

the filespec python does not seem to like is not what is in the code.   Also the file does exist.  


>>>In Python strings, escape sequence such as \f are interpreted as special characters - in this case a form feed.  To avoid this,
 - use forward slashes (even for paths), or
 - precede the whole string with r for raw, e.g., r"\feder16", or
 - double the slash, e.g., "desktop\\fed

4) The list of words is now hard coded in the python code.

How would I replace
words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

with a command to read the list from another file?


>>>One way
f = open(filespec)
mylist = f.readlines()
mylist = [item.rstrip("\n") for item in mylist]

This time you do want the \n to be interpreted as a newline character.

Dep


Art Kendall
Social Research Consultants

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD

Reply | Threaded
Open this post in threaded view
|

Re: Python questions OT

Albert-Jan Roskam
Hi Art,
 
I am subscribed to Python Tutor, which I find (given my level of experience with Python) more useful than the general Python mailing list. http://mail.python.org/mailman/listinfo/tutor
I never asked anything specific to the spss modules for python there. DevCentral or Spss-L is the place for such questions.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 5/4/10, Jon K Peck <[hidden email]> wrote:

From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] Python questions OT
To: [hidden email]
Date: Tuesday, May 4, 2010, 1:10 AM


Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Art Kendall <[hidden email]>
To: [hidden email]
Date: 05/03/2010 03:02 PM
Subject: [SPSSX-L] Python questions   OT
Sent by: "SPSSX(r) Discussion" <[hidden email]>





This is OT but I know that there are some PYTHON users on this list and I am finally getting back to starting with python after a long delay.

1) Is there a PYTHON list someplace?

There is a version active Python language list at http://groups.google.com/group/comp.lang.python/topics
but they won't know much about SPSS in particular.  Besides this list, you can post to the forums at Developer Central

2) Where would I find the documentation for Python error numbers?

>>>The Python language and standard library generally don't use error numbers.  Some SPSS messages do, but there is no additional documentation for these.

3) My python that starts with this
import re, textwrap
# read the combined file and split into individual papers
alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
papers= re.split(r'FEDERALIST No\.'," ".join(alltext))
print len(papers)

outfile = file("C:\Users\Art\desktop\fed\federalistCounts.txt", "w")

words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

gets this error message:

>>>
Traceback (most recent call last):
 File "C:\Users\Art\Desktop\fed\countFedWords.py", line 5, in <module>
   alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\\Art\\Desktop\x0ced\x0ceder16v2.txt'
>>>

the filespec python does not seem to like is not what is in the code.   Also the file does exist.  


>>>In Python strings, escape sequence such as \f are interpreted as special characters - in this case a form feed.  To avoid this,
 - use forward slashes (even for paths), or
 - precede the whole string with r for raw, e.g., r"\feder16", or
 - double the slash, e.g., "desktop\\fed

4) The list of words is now hard coded in the python code.

How would I replace
words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

with a command to read the list from another file?


>>>One way
f = open(filespec)
mylist = f.readlines()
mylist = [item.rstrip("\n") for item in mylist]

This time you do want the \n to be interpreted as a newline character.

Dep


Art Kendall
Social Research Consultants

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: Python questions OT

Albert-Jan Roskam
In reply to this post by Art Kendall
hi!

Do you mean something like this?

import os.path

def splitPaper (filespec, splitTerm):
    return open(filespec, "rb").read().split(splitTerm)

def splitPapers(filespec, splitTerms):
    for splitTerm in splitTerms:
        for cnt, splitted in enumerate(splitPaper(filespec, splitTerm)):
            fn = os.path.join(os.path.dirname(filespec), splitTerm + str(cnt+1).zfill(2) + ".txt")
            open(fn, "wb").write("".join(splitted))
    print "Done!"

splitPapers(filespec = "d:/temp/somefile.txt", splitTerms = ("and", "or", "some phrase"))


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 5/3/10, Art Kendall <[hidden email]> wrote:

From: Art Kendall <[hidden email]>
Subject: [SPSSX-L] Python questions OT
To: [hidden email]
Date: Monday, May 3, 2010, 10:56 PM

This is OT but I know that there are some PYTHON users on this list and I am finally getting back to starting with python after a long delay.

1) Is there a PYTHON list someplace?

2) Where would I find the documentation for Python error numbers?

3) My python that starts with this
import re, textwrap
# read the combined file and split into individual papers
alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
papers= re.split(r'FEDERALIST No\.'," ".join(alltext))
print len(papers)

outfile = file("C:\Users\Art\desktop\fed\federalistCounts.txt", "w")
 
words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

gets this error message:

>>>
Traceback (most recent call last):
  File "C:\Users\Art\Desktop\fed\countFedWords.py", line 5, in <module>
    alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\\Art\\Desktop\x0ced\x0ceder16v2.txt'
>>>

the filespec python does not seem to like is not what is in the code.   Also the file does exist. 

4) The list of words is now hard coded in the python code.

How would I replace
words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

with a command to read the list from another file?


Art Kendall
Social Research Consultants

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD

Reply | Threaded
Open this post in threaded view
|

Re: Python questions OT

Art Kendall
In reply to this post by Jon K Peck
Thank you.  That worked.
Art

On 5/3/2010 7:10 PM, Jon K Peck wrote:

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Art Kendall [hidden email]
To: [hidden email]
Date: 05/03/2010 03:02 PM
Subject: [SPSSX-L] Python questions   OT
Sent by: "SPSSX(r) Discussion" [hidden email]





This is OT but I know that there are some PYTHON users on this list and I am finally getting back to starting with python after a long delay.

1) Is there a PYTHON list someplace?

There is a version active Python language list at http://groups.google.com/group/comp.lang.python/topics
but they won't know much about SPSS in particular.  Besides this list, you can post to the forums at Developer Central

2) Where would I find the documentation for Python error numbers?

>>>The Python language and standard library generally don't use error numbers.  Some SPSS messages do, but there is no additional documentation for these.

3) My python that starts with this
import re, textwrap
# read the combined file and split into individual papers
alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
papers= re.split(r'FEDERALIST No\.'," ".join(alltext))
print len(papers)

outfile = file("C:\Users\Art\desktop\fed\federalistCounts.txt", "w")

words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

gets this error message:

>>>
Traceback (most recent call last):
 File "C:\Users\Art\Desktop\fed\countFedWords.py", line 5, in <module>
   alltext = file("C:\Users\Art\Desktop\fed\feder16v2.txt").readlines()
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\\Art\\Desktop\x0ced\x0ceder16v2.txt'
>>>

the filespec python does not seem to like is not what is in the code.   Also the file does exist.  


>>>In Python strings, escape sequence such as \f are interpreted as special characters - in this case a form feed.  To avoid this,
 - use forward slashes (even for paths), or
 - precede the whole string with r for raw, e.g., r"\feder16", or
 - double the slash, e.g., "desktop\\fed

4) The list of words is now hard coded in the python code.

How would I replace
words = """A                  
ABANDON            
ABANDONED  
. . .
ZEALAND            
ZEALOUS              """

with a command to read the list from another file?


>>>One way
f = open(filespec)
mylist = f.readlines()
mylist = [item.rstrip("\n") for item in mylist]

This time you do want the \n to be interpreted as a newline character.

Dep


Art Kendall
Social Research Consultants

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants