Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-28 Thread Francesco Loffredo

lina wrote:



On Fri, Oct 7, 2011 at 9:38 AM, Dave Angel mailto:d...@davea.name>> wrote:

On 10/06/2011 12:21 PM, lina wrote:



Yes. I understand this part now. But how can I print a list consists of the 
value of key B + E.

For {'B': [4, 5, 6], 'E': [1, 2, 3]}

I wanna get the summary of B and E in each column, namely [5, 7, 9]


My two cents on this very long thread:

tot = [0]*numcolumns
for q in result.values():
tot = [i+j for i, j in zip(tot, q)]




-
Nessun virus nel messaggio.
Controllato da AVG - www.avg.com
Versione: 2012.0.1834 / Database dei virus: 2092/4578 -  Data di rilascio: 
27/10/2011___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-09 Thread lina
Just wanna say a quick thanks to all of you here,

Very informative.

Thanks,

On Sun, Oct 9, 2011 at 11:08 AM, Steven D'Aprano wrote:

> Walter Prins wrote:
>
>  As for the compiler/interpreter argument, I'll just point out again that
>> actually Python in its various forms can either be compiled and/or
>> interepreted, it depends on you really.  For compiled Python flavours, see
>> for example Cython (http://cython.org/) which provides a way to write
>> C/C++
>> modules for Python effectively in Python syntax.  (Cython arguably also
>> makes the "need to write wrapper" comment a moot point.)
>>
>
> Cython is not Python. It is a separate language similar to Python, based on
> Python, but not actually Python. It's actually a superset of Python. Any
> Python code should be legal in Cython, but not all Cython code is legal in
> Python.
>
> See, for example:
>
> http://docs.cython.org/src/**userguide/tutorial.html#primes
>
> and notice that the Cython code:
>
>
> def primes(int kmax):
>cdef int n, k, i
>cdef int p[1000]
># ...
>
> gives you a SyntaxError in Python.
>
> Credit where credit is due: Cython is a wonderful tool and very useful to
> anyone wanting to write C extensions for Python, but it isn't Python. That's
> not a limitation or a bad thing: it couldn't do what it sets out to do if it
> were Python.
>
>
>  Or see Shedskin (
>>
>> http://shed-skin.blogspot.com/**), a Python to C++ static compiler),
>>
>
>
> Shedskin, on the other hand, is a subset of Python: it can only work with a
> restricted set of Python features. So also not Python.
>
> http://code.google.com/p/**shedskin/ 
>
> But still a very useful and valuable tool.
>
>
>
>  or Psyco
>> (http://psyco.sourceforge.net/**introduction.html),
>>  a JIT (Just In Time)
>> compiler for CPython.
>>
>
> Psyco, on the other hand, is an add-on to Python: it runs inside the
> CPython compiler, as an importable module, rather than being a separate
> Python interpreter.
>
>
>
>  Or see Pypy (http://pypy.org/) which is another
>> reimplementation of the Python language with an optimizing JIT compiler.
>> (The following post re realtime image processing in Python is rather
>> impressive:
>> http://morepypy.blogspot.com/**2011/07/realtime-image-**
>> processing-in-python.html
>> )
>>
>
>
> PyPy is very impressive, and it truly is an independent implementation of
> Python, like CPython, Jython, IronPython and Stackless. It is faster than
> CPython (although requires more memory) and in certain restricted
> micro-benchmarks faster than C.
>
> Yes, faster than C.
>
> http://morepypy.blogspot.com/**2008/01/rpython-can-be-faster-**than-c.html
> http://morepypy.blogspot.com/**2011/02/pypy-faster-than-c-on-**
> carefully-crafted.html
> http://morepypy.blogspot.com/**2011/08/pypy-is-faster-than-c-**
> again-string.html
>
>
>
> This just goes to show that *languages* aren't faster or slower than other
> languages. Languages just are. But *implementations* can be faster or
> slower.
>
>
>
> --
> Steven
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread Steven D'Aprano

Walter Prins wrote:


As for the compiler/interpreter argument, I'll just point out again that
actually Python in its various forms can either be compiled and/or
interepreted, it depends on you really.  For compiled Python flavours, see
for example Cython (http://cython.org/) which provides a way to write C/C++
modules for Python effectively in Python syntax.  (Cython arguably also
makes the "need to write wrapper" comment a moot point.)   


Cython is not Python. It is a separate language similar to Python, based 
on Python, but not actually Python. It's actually a superset of Python. 
Any Python code should be legal in Cython, but not all Cython code is 
legal in Python.


See, for example:

http://docs.cython.org/src/userguide/tutorial.html#primes

and notice that the Cython code:


def primes(int kmax):
cdef int n, k, i
cdef int p[1000]
# ...

gives you a SyntaxError in Python.

Credit where credit is due: Cython is a wonderful tool and very useful 
to anyone wanting to write C extensions for Python, but it isn't Python. 
That's not a limitation or a bad thing: it couldn't do what it sets out 
to do if it were Python.




Or see Shedskin (
http://shed-skin.blogspot.com/), a Python to C++ static compiler), 



Shedskin, on the other hand, is a subset of Python: it can only work 
with a restricted set of Python features. So also not Python.


http://code.google.com/p/shedskin/

But still a very useful and valuable tool.



or Psyco
(http://psyco.sourceforge.net/introduction.html ), a JIT (Just In Time)
compiler for CPython.  


Psyco, on the other hand, is an add-on to Python: it runs inside the 
CPython compiler, as an importable module, rather than being a separate 
Python interpreter.




Or see Pypy (http://pypy.org/) which is another
reimplementation of the Python language with an optimizing JIT compiler.
(The following post re realtime image processing in Python is rather
impressive:
http://morepypy.blogspot.com/2011/07/realtime-image-processing-in-python.html)



PyPy is very impressive, and it truly is an independent implementation 
of Python, like CPython, Jython, IronPython and Stackless. It is faster 
than CPython (although requires more memory) and in certain restricted 
micro-benchmarks faster than C.


Yes, faster than C.

http://morepypy.blogspot.com/2008/01/rpython-can-be-faster-than-c.html
http://morepypy.blogspot.com/2011/02/pypy-faster-than-c-on-carefully-crafted.html
http://morepypy.blogspot.com/2011/08/pypy-is-faster-than-c-again-string.html



This just goes to show that *languages* aren't faster or slower than 
other languages. Languages just are. But *implementations* can be faster 
or slower.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread Walter Prins
Hi Lina,

On 8 October 2011 18:18, lina  wrote:

>  I write mainly critical speed code and large memory code that are meant to
> run as hug jobs over cluster (transportability is an issue;
> my C code is really faster than my Maple code; interpreter language are
> good for development of algorithm in view to write a fast C code;
> speed does not involved human time but machine time; I do not really care
> about candy interfaces; for human interface Python and alike are recommended
> but BASH (and even DASH) can do very good job; I am not sure that a
> scientist is more concerned with interface than with efficient code except
> for
> visual stuff; being very familiar with of a given language is certainly
> better than  knowing superficially a lot of languages;
> as a lot of interpreter languages, Python has a huge manual; C manual is
> rather small; D is both a interpreter language and a compiler language:
> it is why I want to learn D: no more need to write wrapper that can be
> boring and that can be bottle neck.
>

I should think that the Core Python *language* manual is almost certainly
smaller than the C one.  (The base Python language is actually rather small
and concise. What typically makes Python books larger in general is I think
actually the (so called "batteries included") extensive set of libraries and
modules included and commonly available for the language, not the Python
language itself.)

As for the compiler/interpreter argument, I'll just point out again that
actually Python in its various forms can either be compiled and/or
interepreted, it depends on you really.  For compiled Python flavours, see
for example Cython (http://cython.org/) which provides a way to write C/C++
modules for Python effectively in Python syntax.  (Cython arguably also
makes the "need to write wrapper" comment a moot point.)   Or see Shedskin (
http://shed-skin.blogspot.com/), a Python to C++ static compiler), or Psyco
(http://psyco.sourceforge.net/introduction.html ), a JIT (Just In Time)
compiler for CPython.  Or see Pypy (http://pypy.org/) which is another
reimplementation of the Python language with an optimizing JIT compiler.
(The following post re realtime image processing in Python is rather
impressive:
http://morepypy.blogspot.com/2011/07/realtime-image-processing-in-python.html)


I'd like to also mention that there's many many Python packages of interest
or used by the scientific community, not least NumPy/SciPy (
http://numpy.scipy.org/ and http://www.scipy.org/).  For a more complete
list see: http://www.scipy.org/Topical_Software  You'll see that Python is
used extensively in many science and engineering contexts.  Lastly regarding
(user) interfaces -- as has already been pointed out, Python can obviously
do UI's, but it's by no means limited to being some sort of front-end
language.

Anyway, I think I'll leave it at that for now.

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread Walter Prins
Lina,

Just a couple of comments on the topic of Python vs other languages to add
to what others have said:

On 7 October 2011 16:40, lina  wrote:

> but today I was also discouraged, I was told that you should not have
> learned python, you should focus on C or bash, or D, cause python is going
> to be obsolete, and I defensed that I saw lots people/apps using python, and
> it's always good to learn a language well, it's helpful for you to
> understand another language. I was further told that fortran is obsolete,
> but still lots of fortran guys using it.
> I don't know much, since I started and wanted to learn, actually I wish to
> hear some encouraging words, not some words discouraging, but on another
> hands, it's always good to have an open mind, so I want to hear your opinion
> and perspective, I am not knowledgable about this.
>

I'd say rather than being obsolete, Python is, if anything, on the
ascendancy.  My own personal opinion as someone who has been in this game
for a while now and know (for various levels of know) quite a number
languages (e.g. off the top of my head: C, C++, Java, C#, Pascal, Delphi,
Oberon, Python, VB, sql, prolog, Lisp, bash, awk), is that Python is
(generally speaking) one of the "best" languages I've had the pleasure to
work with.  By "best" here I mean that it's useful and available in more
contexts than most of the others, it's better at encouraging good
programming practices, the included as well as third party libraries are
very comprehensive, and usually as good as or better than many of the other
languages I've tried, etc etc, it's fast enough [and in this context I'd
point at the multiple implementations (CPython, IronPython, Jython, PyPy
etc)  as a further advantage, not to mention the fact that you can still
fall back to writing Python modules in C if need be.], it's highly
productive, it keeps you close to the problem domain and usually doesn't
burden you with lower level details, etc etc.  Addiontally, you might be
interested to know that Python is used as one of Google's core 3 languages,
as well as being extensively used in several Linux distributions as a system
utility programming language (for example Redhat, Ubuntu.)

So, for all these reasons (and probably many others I've not mentioned) I
again suggest it's rather a stretch to claim Python is "obsolete".  (I'd
addtionally ask, what does something being "obsolete" mean anyway?  If lots
of people use a languages, is it not then by definition not obsolete?)

I'll also point you at some stats which I dug up (for entertainment value,
or maybe you can use it to counter some of your friend's claims of
obsolescence etc.)
1) According to langpop.com, Python is overall the 6th most popular
language, ahead of C# at no. 7.  D only comes in at no. 28.  (C is at no.1)
There's several ranking listss, and Python is in fact no.1 one on a couple
(the programming.reddit.com ranking and the IRC ranking).
For more see here:  http://langpop.com/

2) On the TIOBE Programming Community Index (Sept. 2011) Python is similarly
ranked at no. 8.  D only re-entered the table recently at no. 20.
For more see here:
http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

Hope that helps,

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread lina
On Sat, Oct 8, 2011 at 11:34 PM, Alan Gauld wrote:

> On 08/10/11 14:17, lina wrote:
>
> You will never say, "Gosh, I wish I knew FEWER programming languages!"
>> Ha Ha ...
>>
>> regarding the python and C, I was told that python is good for interface
>> and others, but slow. and further was suggested to write the main part
>> in C and wrapped in python.
>>
>
> The myth that Python (and interpreted languages in general) are too slow
> for "real programming" is common among older programmers(*) who were trained
> on old computers like the original PC and servers like the DEC PDP series.
> When I left university our office had just acquired the largest, most
> powerful VAX computer in Europe - it ran at 12MHz and had 16MB of RAM and
> supported over 100 users! Wow! However many. many, applications can now be
> written in interpreted languages and run fast enough (and the key word is
> "enough").
>
> If you really need the last bit of speed then yes, C (or even assembler)
> may be needed, but that is a tiny proportion of the code for most things. So
> to take your friends comment and turn it around:
>
> "... write the main part in Python (or .NET or Java or...) and write the
> speed critical parts in C."
>
> Because typically the Python part is likely to be 70-90% of the code
> and the C part much less.
>
> (*)I tend to consider myself in that group and up until about 15 years ago
> held the same view. But around then I got involved in projects using
> languages like Smalltalk, Tcl/Tk and VB. It was obvious that these languages
> were perfectly capable of producing usable apps
> on the typical hardware of the time - early Pentiums running at
> around 500MHz and with 128M RAM etc. Nowadays with most PCs having
> processors running in excess of 2GHz and over 2GB RAM there are very few
> apps that cannot be constructed using interpreted languages, and many are. I
> suspect the reason most commercial projects still use Java, C++ etc is
> twofold:
> 1) Magazines etc tend to review apps against their competition on speed,
> even if the real world speed measures are irrelevant. So for marketing
> purposes you must be seen to be comparable to your competitors.
> 2) Compiled code is harder for the competition to reverse engineer so
> coding secrets are 'safer'. (This is largely psychological since its the
> design concepts your competitors are more likely to steal, not the
> algorithmic details.)
>
> There are projects where ultimate speed is still important, indeed one of
> my current projects at work is being largely rewritten from C#.Net to
> compiled C++ for exactly that reason. (But it is a number crunching server
> app supporting around 15000 concurrent users) But the vast bulk of user
> programs could be written in interpreted languages IMHO.  One of the
> interesting things about my recent conversion from Windows to Linux for my
> home PC  is to see just how many of the desktop apps are written in Python,
> including games and office apps etc.
>

Here is the feedback:"

He is certainly right and we are agree:
I have to choose a program language because I cannot afford to learn a lot
of them;
when I have to choose it was early age compare to today; I do not really
care about
interface (a good command line interface is enough that can use (BASH/DASH)
script and can be incorporated in (BASH/DASH) script is enough for me);
I write mainly critical speed code and large memory code that are meant to
run as hug jobs over cluster (transportability is an issue;
my C code is really faster than my Maple code; interpreter language are good
for development of algorithm in view to write a fast C code;
speed does not involved human time but machine time; I do not really care
about candy interfaces; for human interface Python and alike are recommended
but BASH (and even DASH) can do very good job; I am not sure that a
scientist is more concerned with interface than with efficient code except
for
visual stuff; being very familiar with of a given language is certainly
better than  knowing superficially a lot of languages;
as a lot of interpreter languages, Python has a huge manual; C manual is
rather small; D is both a interpreter language and a compiler language:
it is why I want to learn D: no more need to write wrapper that can be
boring and that can be bottle neck.


Last, enter in the game rather than watching. Then you can get your opinion.
According to the question in the Subject, I guess that you still have a lot
to learn before to appreciate all this subtilitises.

"

I kept the content of the email intact.

Thanks,



>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread lina
On Sat, Oct 8, 2011 at 11:34 PM, Alan Gauld wrote:

> On 08/10/11 14:17, lina wrote:
>
> You will never say, "Gosh, I wish I knew FEWER programming languages!"
>> Ha Ha ...
>>
>> regarding the python and C, I was told that python is good for interface
>> and others, but slow. and further was suggested to write the main part
>> in C and wrapped in python.
>>
>
> The myth that Python (and interpreted languages in general) are too slow
> for "real programming" is common among older programmers(*) who were trained
> on old computers like the original PC and servers like the DEC PDP series.
> When I left university our office had just acquired the largest, most
> powerful VAX computer in Europe - it ran at 12MHz and had 16MB of RAM and
> supported over 100 users! Wow! However many. many, applications can now be
> written in interpreted languages and run fast enough (and the key word is
> "enough").
>
Wow .. I used to hate slow computer. Now I won't hate the retard (slow
reaction) lab computers any more.  (It's a joke)
When I was in college, I needed to copy assignment to print it out.
I used floppy driver at that time, and the stores in campus put the floppy
driver near the cashier place with the gum and other candies. Now the gum
and other candies I guess are still there, but no floppy driver any more,
but probably some CDs or DVDs.


>
> If you really need the last bit of speed then yes, C (or even assembler)
> may be needed, but that is a tiny proportion of the code for most things. So
> to take your friends comment and turn it around:
>
> "... write the main part in Python (or .NET or Java or...) and write the
> speed critical parts in C."
>
> Because typically the Python part is likely to be 70-90% of the code
> and the C part much less.
>
> (*)I tend to consider myself in that group and up until about 15 years ago
> held the same view. But around then I got involved in projects using
> languages like Smalltalk, Tcl/Tk and VB. It was obvious that these languages
> were perfectly capable of producing usable apps
> on the typical hardware of the time - early Pentiums running at
> around 500MHz and with 128M RAM etc. Nowadays with most PCs having
> processors running in excess of 2GHz and over 2GB RAM there are very few
> apps that cannot be constructed using interpreted languages, and many are. I
> suspect the reason most commercial projects still use Java, C++ etc is
> twofold:
> 1) Magazines etc tend to review apps against their competition on speed,
> even if the real world speed measures are irrelevant. So for marketing
> purposes you must be seen to be comparable to your competitors.
> 2) Compiled code is harder for the competition to reverse engineer so
> coding secrets are 'safer'. (This is largely psychological since its the
> design concepts your competitors are more likely to steal, not the
> algorithmic details.)
>
> There are projects where ultimate speed is still important, indeed one of
> my current projects at work is being largely rewritten from C#.Net to
> compiled C++ for exactly that reason. (But it is a number crunching server
> app supporting around 15000 concurrent users) But the vast bulk of user
> programs could be written in interpreted languages IMHO.  One of the
> interesting things about my recent conversion from Windows to Linux for my
> home PC  is to see just how many of the desktop apps are written in Python,
> including games and office apps etc.
>

My initiative to learn python is that I noticed some (often used) apps are
written in python.
Another reason is that I want to learn (at least) one language deeper and
seriously (hopeful to have a happy ending in learning).

Thanks, I have forwarded this email to my friend.


>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread lina
On Sat, Oct 8, 2011 at 10:50 PM, Alan Gauld wrote:

> On 08/10/11 11:27, Alan Gauld wrote:
>
>  What you want, I think, is to join the original list
>> with spaces(and maybe add a newline for writing to file)
>>
>> summary = ' '.join(summary) + \n
>>
>
> But you need to make the original items strings first
> so it should be:
>
>  summary = ' '.join( map(str,summary) ) + \n
>

new_summary=' '.join( map(str,summary) ) + \n
^
SyntaxError: unexpected character after line continuation character

so I added it as

new_summary=' '.join( map(str,summary[1:-3]) ) + "\n"

but each one in summary does not show in new line.

new_summary='\n'.join(map(str,summary[1:-3]))

This one works,

>
> Sorry about that,


Thanks for your suggestions.


>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread Alan Gauld

On 08/10/11 14:17, lina wrote:


You will never say, "Gosh, I wish I knew FEWER programming languages!"
Ha Ha ...

regarding the python and C, I was told that python is good for interface
and others, but slow. and further was suggested to write the main part
in C and wrapped in python.


The myth that Python (and interpreted languages in general) are too slow 
for "real programming" is common among older programmers(*) who were 
trained on old computers like the original PC and servers like the DEC 
PDP series. When I left university our office had just acquired the 
largest, most powerful VAX computer in Europe - it ran at 12MHz and had 
16MB of RAM and supported over 100 users! Wow! However many. many, 
applications can now be written in interpreted languages and run fast 
enough (and the key word is "enough").


If you really need the last bit of speed then yes, C (or even assembler) 
may be needed, but that is a tiny proportion of the code for most 
things. So to take your friends comment and turn it around:


"... write the main part in Python (or .NET or Java or...) and write the 
speed critical parts in C."


Because typically the Python part is likely to be 70-90% of the code
and the C part much less.

(*)I tend to consider myself in that group and up until about 15 years 
ago held the same view. But around then I got involved in projects using 
languages like Smalltalk, Tcl/Tk and VB. It was obvious that these 
languages were perfectly capable of producing usable apps

on the typical hardware of the time - early Pentiums running at
around 500MHz and with 128M RAM etc. Nowadays with most PCs having 
processors running in excess of 2GHz and over 2GB RAM there are very few 
apps that cannot be constructed using interpreted languages, and many 
are. I suspect the reason most commercial projects still use Java, C++ 
etc is twofold:
1) Magazines etc tend to review apps against their competition on speed, 
even if the real world speed measures are irrelevant. So for marketing 
purposes you must be seen to be comparable to your competitors.
2) Compiled code is harder for the competition to reverse engineer so 
coding secrets are 'safer'. (This is largely psychological since its the 
design concepts your competitors are more likely to steal, not the 
algorithmic details.)


There are projects where ultimate speed is still important, indeed one 
of my current projects at work is being largely rewritten from C#.Net to 
compiled C++ for exactly that reason. (But it is a number crunching 
server app supporting around 15000 concurrent users) But the vast bulk 
of user programs could be written in interpreted languages IMHO.  One of 
the interesting things about my recent conversion from Windows to Linux 
for my home PC  is to see just how many of the desktop apps are written 
in Python, including games and office apps etc.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread Alan Gauld

On 08/10/11 11:27, Alan Gauld wrote:


What you want, I think, is to join the original list
with spaces(and maybe add a newline for writing to file)

summary = ' '.join(summary) + \n


But you need to make the original items strings first
so it should be:

 summary = ' '.join( map(str,summary) ) + \n

Sorry about that,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread lina
On Sat, Oct 8, 2011 at 6:27 PM, Alan Gauld wrote:

> On 08/10/11 04:34, lina wrote:
>
>  print(summary)   ### output is [1,3,5,6]
>> summary='\n'.join(str(summary)**.split(','))
>>
>
> This line is wrong.
> This is converting summary to a string -> '[1,3,5,6]'
> Then splitting by commas to create a new list -> [ '[1', '3', '5', 6]' ]
> Notice the first and last elements have the []
> Then it joins then into a string using \n as seperators.
>
> What you want, I think, is to join the original list
> with spaces(and maybe add a newline for writing to file)
>
>
> summary = ' '.join(summary) + \n

sorry, just correct forget testing,

Traceback (most recent call last):
  File "counter-vertically-WORKING.py", line 32, in 
new_summary=''.join((summary[1:-3]))+"\n"
TypeError: sequence item 0: expected str instance, int found

so
new_summary=''.join(str(summary[1:-3]))+"\n"

but the results seems as summary[1:-3], still has [ ] and not in new lines.

Thanks,



>
>
>  with open(base+OUTFILEEXT,"w") as f:
>> f.write(str(summary))
>>
>
> HTH,
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread lina
On Sat, Oct 8, 2011 at 6:27 PM, Alan Gauld wrote:

> On 08/10/11 04:34, lina wrote:
>
>  print(summary)   ### output is [1,3,5,6]
>> summary='\n'.join(str(summary)**.split(','))
>>
>
> This line is wrong.
> This is converting summary to a string -> '[1,3,5,6]'
> Then splitting by commas to create a new list -> [ '[1', '3', '5', 6]' ]
> Notice the first and last elements have the []
> Then it joins then into a string using \n as seperators.
>
> What you want, I think, is to join the original list
> with spaces(and maybe add a newline for writing to file)
>
You are right!

>
>
> summary = ' '.join(summary) + \n
>
>
> Just correct.

>  with open(base+OUTFILEEXT,"w") as f:
>> f.write(str(summary))
>>
>
> HTH,


Thanks, it's helpful.

>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread lina
On Sat, Oct 8, 2011 at 4:43 PM, Steven D'Aprano  wrote:

> Alan Gauld wrote:
>
>> On 07/10/11 16:40, lina wrote:
>>
>>  but today I was also discouraged, I was told that you should not have
>>> learned python, you should focus on C or bash, or D, cause python is
>>> going to be obsolete,
>>>
>>
>> C is a great language for writing Operating Systems and other "near the
>> metal" code. But its not the best language for busiess apps, artificial
>> intelligence and a host of other things. Bash is a good user shell, but its
>> not even the best Unix shell for scripting (Thats probably ksh).
>> D? Who actually uses D? I think your informant was not well informed.
>>
>
>
> Ah, flame wars over which is the best language :)
>
> I don't know enough about ksh to compare it to bash, but at least you
> didn't recommend csh :)
>
> http://www.faqs.org/faqs/unix-**faq/shell/csh-whynot/
>
> But frankly, all the shell languages are pretty horrible.
>
> D might not be a popular language, but it is a modern, advanced language in
> the C family, influenced heavily by Python. If I were to learn a new
> statically typed procedural/object-oriented language, I'd seriously consider
> learning D.
>
> http://en.wikipedia.org/wiki/**D_(programming_language)
>
> But it really depends on why you are learning the language.
>
> Are you learning a language because you need it for a specific job or
> project? Then learn whatever language you need, because you need it,
> regardless of whether it is widespread or not.
>
> Are you learning a language because you want to maximize your chances of
> getting a job? Then learn one of the big commercial languages like VB, C,
> C++, PHP, Javascript or Java. You won't learn to be a better programmer, you
> may learn to be a *worse* programmer, and you might be competing with
> thousands of code monkeys, but there are plenty of jobs available.
>
> Are you learning a language because you want to get the highest paid job?
> Then become an expert in an old language like PL/I or Fortran or something,
> and do it 30 years ago so you can claim 30 years experience. Now, if you can
> find a job -- and good luck with that -- you can pretty much set your own
> rate.
>
> Do you want to learn a new language for the love of learning languages?
> Then learn a wide variety of languages.
>
> Do you want to expand your skill-set (good for the resume!) and open your
> mind to new programming concepts? Then learn a variety of languages using
> many different programming paradigms:
>
> * procedural
> * declarative
> * functional
> * logic/deductive
> * object-oriented
> * stack-based
> * concatenative
> * concurrent
> * natural language programming
> * esoteric languages
>
> etc.
>
> Even if you don't have the time to learn the language, learn ABOUT the
> language.
>
> You will never say, "Gosh, I wish I knew FEWER programming languages!"
>

Ha Ha ...

Today I also talked with someone who had 20 years computation experience,
what I was told is that writing the algorithm is the most challenge things
than language its own.
regarding the python and C, I was told that python is good for interface and
others, but slow. and further was suggested to write the main part in C and
wrapped in python.

so complicated for me to achieve at present I guess.

Thanks

>
>
>
> --
> Steven
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread Alan Gauld

On 08/10/11 04:34, lina wrote:


 print(summary)   ### output is [1,3,5,6]
 summary='\n'.join(str(summary).split(','))


This line is wrong.
This is converting summary to a string -> '[1,3,5,6]'
Then splitting by commas to create a new list -> [ '[1', '3', '5', 6]' ]
Notice the first and last elements have the []
Then it joins then into a string using \n as seperators.

What you want, I think, is to join the original list
with spaces(and maybe add a newline for writing to file)


summary = ' '.join(summary) + \n


 with open(base+OUTFILEEXT,"w") as f:
 f.write(str(summary))


HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread Steven D'Aprano

Alan Gauld wrote:

On 07/10/11 16:40, lina wrote:


but today I was also discouraged, I was told that you should not have
learned python, you should focus on C or bash, or D, cause python is
going to be obsolete,


C is a great language for writing Operating Systems and other "near the 
metal" code. But its not the best language for busiess apps, artificial 
intelligence and a host of other things. Bash is a good user shell, but 
its not even the best Unix shell for scripting (Thats probably ksh).

D? Who actually uses D? I think your informant was not well informed.



Ah, flame wars over which is the best language :)

I don't know enough about ksh to compare it to bash, but at least you 
didn't recommend csh :)


http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

But frankly, all the shell languages are pretty horrible.

D might not be a popular language, but it is a modern, advanced language 
in the C family, influenced heavily by Python. If I were to learn a new 
statically typed procedural/object-oriented language, I'd seriously 
consider learning D.


http://en.wikipedia.org/wiki/D_(programming_language)

But it really depends on why you are learning the language.

Are you learning a language because you need it for a specific job or 
project? Then learn whatever language you need, because you need it, 
regardless of whether it is widespread or not.


Are you learning a language because you want to maximize your chances of 
getting a job? Then learn one of the big commercial languages like VB, 
C, C++, PHP, Javascript or Java. You won't learn to be a better 
programmer, you may learn to be a *worse* programmer, and you might be 
competing with thousands of code monkeys, but there are plenty of jobs 
available.


Are you learning a language because you want to get the highest paid 
job? Then become an expert in an old language like PL/I or Fortran or 
something, and do it 30 years ago so you can claim 30 years experience. 
Now, if you can find a job -- and good luck with that -- you can pretty 
much set your own rate.


Do you want to learn a new language for the love of learning languages? 
Then learn a wide variety of languages.


Do you want to expand your skill-set (good for the resume!) and open 
your mind to new programming concepts? Then learn a variety of languages 
using many different programming paradigms:


* procedural
* declarative
* functional
* logic/deductive
* object-oriented
* stack-based
* concatenative
* concurrent
* natural language programming
* esoteric languages

etc.

Even if you don't have the time to learn the language, learn ABOUT the 
language.


You will never say, "Gosh, I wish I knew FEWER programming languages!"



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread Andreas Perstinger

On 2011-10-08 09:12, lina wrote:

$ python3 counter-vertically-WORKING.py
[26, 22, 28, 30, 32, 27, 30, 29, 28, 30, 32, 24, 27, 27, 28, 30, 32, 30, 33,
27, 33, 32, 34, 31, 28, 34, 33, 32, 25, 35, 30, 32, 30, 32, 25, 30, 26, 24,
33, 28, 27, 26, 23, 27, 27, 28, 27, 25, 24, 23, 23, 27, 24, 27, 26, 23, 17,
25, 21, 28, 21, 15, 24, 21, 12, 11, 9, 15, 20, 15, 15, 22, 29, 30, 23, 21,
29, 31, 17, 26, 21, 23, 23, 19, 25, 21, 27, 23, 25, 30, 26, 23, 25, 23, 25,
30, 28, 29, 25, 26, 28, 23, 26, 30, 25, 25, 21, 23, 23, 25, 21, 25, 25, 23,
30, 29, 28, 27, 27, 29, 27, 23, 27, 29, 21, 24, 23, 21, 25, 23, 26, 23, 23,
23, 23, 24, 25, 21, 23, 23, 23, 25, 25, 30, 25, 23, 26, 24, 28, 25, 25, 23,
23, 19, 23, 23, 21, 25, 18, 23, 21, 25, 21, 23, 25, 23, 22, 23, 21, 28, 25,
24, 21, 20, 21, 14, 23, 21, 13, 14, 21, 23, 24, 29, 26, 26, 21, 17, 19, 23,
21, 24, 26, 24, 26, 29, 29, 28, 28, 21, 24]
Traceback (most recent call last):
   File "counter-vertically-WORKING.py", line 26, in
 results[ch][col]+=1
IndexError: list index out of range

still the same, and only one txt coming out. the others did not process.

$ ls *.xpm *.txt
ss_0.xpm   ss_1.xpm  ss_4.xpm  ss_7.xpm  ss_9.xpm
ss_10.xpm  ss_2.xpm  ss_5.xpm
ss_1.txt   ss_3.xpm  ss_6.xpm  ss_8.xpm


Strange, if I do

$ python3.2 counter-vertically-WORKING.py

it prints all results and also creates all txt-files:

$ ls *.xpm *.txt
ss_0.txt  ss_0.xpm  ss_1.txt  ss_1.xpm  ss_2.txt  ss_2.xpm

Please insert "print(fileName)" as the first line in your main for-loop 
so we can see on which file the program hangs.


Bye, Andreas
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread lina
On Sat, Oct 8, 2011 at 2:50 PM, Andreas Perstinger <
andreas.perstin...@gmx.net> wrote:

> On 2011-10-08 08:25, lina wrote:
>
>>  Still have a reading "multiple" files issue:
>>>

  Traceback (most recent call last):
   File "counter-vertically-WORKING.py", line 26, in

 results[ch][col]+=1
  IndexError: list index out of range

  only one file ss_1.xpm was processed and wrote file, for the rest
  ss_2.xpm,
  ss_3.xpm and following keep intact,

  any suggestions?

>>>
>> https://docs.google.com/leaf?**id=**0B93SVRfpVVg3MjIxZGI5YWQtN2NiY**
>> y00MWFkLWE0ZTctNjExNGJiNjYzYjQ**w&hl=en_GB
>>
>> https://docs.google.com/leaf?**id=**0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhY**
>> S00MTA4LTg3YTktY2JjYzEyMDU5OTI**2&hl=en_GB
>>
>> https://docs.google.com/leaf?**id=**0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhY**
>> S00MTA4LTg3YTktY2JjYzEyMDU5OTI**2&hl=en_US
>>
>> you may rename ss_1.xpm to the ss_2 or 3, keep them the same is okay, just
>> different names.
>>
>
> Ok, I just ran your script with this files and don't get an error. Except
> some stylistic issues the code looks fine. The resulting txt-files still
> have the problem with the leading '[' and trailing ']' but I've already
> suggested a solution earlier today.
>
> Do you still get an error?


I figured it out, my mistake,

the following files from ss_2.xpm to ss_10.xpm has one more line than
ss_1.xpm

so after changed
LINESTOSKIP=23
to 24

it works.

before the ss_1.xpm was taking as the trial and finished, the last 9 was
generated with script, so there was a very minor differences.

Thanks for your help,

Best,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-08 Thread lina
On Sat, Oct 8, 2011 at 2:50 PM, Andreas Perstinger <
andreas.perstin...@gmx.net> wrote:

> On 2011-10-08 08:25, lina wrote:
>
>>  Still have a reading "multiple" files issue:
>>>

  Traceback (most recent call last):
   File "counter-vertically-WORKING.py", line 26, in

 results[ch][col]+=1
  IndexError: list index out of range

  only one file ss_1.xpm was processed and wrote file, for the rest
  ss_2.xpm,
  ss_3.xpm and following keep intact,

  any suggestions?

>>>
>> https://docs.google.com/leaf?**id=**0B93SVRfpVVg3MjIxZGI5YWQtN2NiY**
>> y00MWFkLWE0ZTctNjExNGJiNjYzYjQ**w&hl=en_GB
>>
>> https://docs.google.com/leaf?**id=**0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhY**
>> S00MTA4LTg3YTktY2JjYzEyMDU5OTI**2&hl=en_GB
>>
>> https://docs.google.com/leaf?**id=**0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhY**
>> S00MTA4LTg3YTktY2JjYzEyMDU5OTI**2&hl=en_US
>>
>> you may rename ss_1.xpm to the ss_2 or 3, keep them the same is okay, just
>> different names.
>>
>
> Ok, I just ran your script with this files and don't get an error. Except
> some stylistic issues the code looks fine. The resulting txt-files still
> have the problem with the leading '[' and trailing ']' but I've already
> suggested a solution earlier today.
>
> Do you still get an error?



$ python3 counter-vertically-WORKING.py
[26, 22, 28, 30, 32, 27, 30, 29, 28, 30, 32, 24, 27, 27, 28, 30, 32, 30, 33,
27, 33, 32, 34, 31, 28, 34, 33, 32, 25, 35, 30, 32, 30, 32, 25, 30, 26, 24,
33, 28, 27, 26, 23, 27, 27, 28, 27, 25, 24, 23, 23, 27, 24, 27, 26, 23, 17,
25, 21, 28, 21, 15, 24, 21, 12, 11, 9, 15, 20, 15, 15, 22, 29, 30, 23, 21,
29, 31, 17, 26, 21, 23, 23, 19, 25, 21, 27, 23, 25, 30, 26, 23, 25, 23, 25,
30, 28, 29, 25, 26, 28, 23, 26, 30, 25, 25, 21, 23, 23, 25, 21, 25, 25, 23,
30, 29, 28, 27, 27, 29, 27, 23, 27, 29, 21, 24, 23, 21, 25, 23, 26, 23, 23,
23, 23, 24, 25, 21, 23, 23, 23, 25, 25, 30, 25, 23, 26, 24, 28, 25, 25, 23,
23, 19, 23, 23, 21, 25, 18, 23, 21, 25, 21, 23, 25, 23, 22, 23, 21, 28, 25,
24, 21, 20, 21, 14, 23, 21, 13, 14, 21, 23, 24, 29, 26, 26, 21, 17, 19, 23,
21, 24, 26, 24, 26, 29, 29, 28, 28, 21, 24]
Traceback (most recent call last):
  File "counter-vertically-WORKING.py", line 26, in 
results[ch][col]+=1
IndexError: list index out of range

still the same, and only one txt coming out. the others did not process.

$ ls *.xpm *.txt
ss_0.xpm   ss_1.xpm  ss_4.xpm  ss_7.xpm  ss_9.xpm
ss_10.xpm  ss_2.xpm  ss_5.xpm
ss_1.txt   ss_3.xpm  ss_6.xpm  ss_8.xpm



thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Andreas Perstinger

On 2011-10-08 08:25, lina wrote:

 Still have a reading "multiple" files issue:


 Traceback (most recent call last):
   File "counter-vertically-WORKING.**py", line 26, in
 results[ch][col]+=1
 IndexError: list index out of range

 only one file ss_1.xpm was processed and wrote file, for the rest
 ss_2.xpm,
 ss_3.xpm and following keep intact,

 any suggestions?


https://docs.google.com/leaf?id=0B93SVRfpVVg3MjIxZGI5YWQtN2NiYy00MWFkLWE0ZTctNjExNGJiNjYzYjQw&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhYS00MTA4LTg3YTktY2JjYzEyMDU5OTI2&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhYS00MTA4LTg3YTktY2JjYzEyMDU5OTI2&hl=en_US

you may rename ss_1.xpm to the ss_2 or 3, keep them the same is okay, just
different names.


Ok, I just ran your script with this files and don't get an error. 
Except some stylistic issues the code looks fine. The resulting 
txt-files still have the problem with the leading '[' and trailing ']' 
but I've already suggested a solution earlier today.


Do you still get an error?

Bye, Andreas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
> Still have a reading "multiple" files issue:
>>
>> Traceback (most recent call last):
>>   File "counter-vertically-WORKING.**py", line 26, in
>> results[ch][col]+=1
>> IndexError: list index out of range
>>
>> only one file ss_1.xpm was processed and wrote file, for the rest
>> ss_2.xpm,
>> ss_3.xpm and following keep intact,
>>
>> any suggestions?
>>
>
> Could you provide links to your current script file
> (counter-vertically-WORKING.**py) and your data files(ss_1.xpm, ss_2.xpm,
> ...)?
>

https://docs.google.com/leaf?id=0B93SVRfpVVg3MjIxZGI5YWQtN2NiYy00MWFkLWE0ZTctNjExNGJiNjYzYjQw&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhYS00MTA4LTg3YTktY2JjYzEyMDU5OTI2&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhYS00MTA4LTg3YTktY2JjYzEyMDU5OTI2&hl=en_US

you may rename ss_1.xpm to the ss_2 or 3, keep them the same is okay, just
different names.

Thanks,



> Bye, Andreas
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Andreas Perstinger

On 2011-10-08 06:07, lina wrote:

Still have a reading "multiple" files issue:

Traceback (most recent call last):
   File "counter-vertically-WORKING.py", line 26, in
 results[ch][col]+=1
IndexError: list index out of range

only one file ss_1.xpm was processed and wrote file, for the rest ss_2.xpm,
ss_3.xpm and following keep intact,

any suggestions?


Could you provide links to your current script file 
(counter-vertically-WORKING.py) and your data files(ss_1.xpm, ss_2.xpm, 
...)?


Bye, Andreas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Andreas Perstinger

On 2011-10-08 05:34, lina wrote:

Another minor derived questions:

 summary=[]
 for a,b in zip(results['E'],results['B']):
 summary.append(a+b)## now the summary is '[0,1, 3, 5,
6,0,0,0]'
 del summary[0]   ## here I wanna remove the first zero, which came
from the initial double quote "EB...E",
 summary.pop()## here I wanna remove the last three zeros
 summary.pop()
 summary.pop()
 print(summary)   ### output is [1,3,5,6]
 summary='\n'.join(str(summary).split(','))  ### wish the result in
one column,
 with open(base+OUTFILEEXT,"w") as f:
 f.write(str(summary))

the final result in the output.txt file is:
[1
3
5
6]

Q1: how can I remove the [1 "[" to only keep 1?


That's a job for a list comprehension (read 
http://docs.python.org/py3k/tutorial/datastructures.html#list-comprehensions 
for a short introduction):


new_summary = '\n'.join([str(element) for element in summary])


Q2 how can I improve above expressions?


-) As above, you could use a list comprehension for creating your 
summary list:


summary = [(a + b) for a, b in zip(results['E'], results['B'])]

-) Instead of

del summary[0]
summary.pop()
summary.pop()
summary.pop()

I suggest to slice your list:

summary = summary[1:-3]

But this assumes that your summary list will always have one leading 
zero and 3 trailing zeros.


-) In your last line

with open(base+OUTFILEEXT,"w") as f:
 f.write(str(summary))

you don't need to convert summary to a string because it is already one.

Bye, Andreas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
Still have a reading "multiple" files issue:

Traceback (most recent call last):
  File "counter-vertically-WORKING.py", line 26, in 
results[ch][col]+=1
IndexError: list index out of range

only one file ss_1.xpm was processed and wrote file, for the rest ss_2.xpm,
ss_3.xpm and following keep intact,

any suggestions?

thanks, and sorry for so many questions here,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
Another minor derived questions:

summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)## now the summary is '[0,1, 3, 5,
6,0,0,0]'
del summary[0]   ## here I wanna remove the first zero, which came
from the initial double quote "EB...E",
summary.pop()## here I wanna remove the last three zeros
summary.pop()
summary.pop()
print(summary)   ### output is [1,3,5,6]
summary='\n'.join(str(summary).split(','))  ### wish the result in
one column,
with open(base+OUTFILEEXT,"w") as f:
f.write(str(summary))

the final result in the output.txt file is:
[1
3
5
6]

Q1: how can I remove the [1 "[" to only keep 1?

Q2 how can I improve above expressions?

Thanks,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
> C is a great language for writing Operating Systems and other "near the
> metal" code. But its not the best language for busiess apps, artificial
> intelligence and a host of other things. Bash is a good user shell, but its
> not even the best Unix shell for scripting (Thats probably ksh).
> D? Who actually uses D? I think your informant was not well informed.
>
Next time when I saw my friend, I would ask, "have you met someone who has
actually started in use D yet?"
it's a joke, thanks for informing me that.

>
> AS fopr Python becoming obsolete? Maybe someday, but not soon.
> And even if it does the principles it teaches are alive and well in many
> other languages - eg, Ruby, Lua both openly borrow from Python.
> Just as Python borrows from Lisp and Haskell and Smalltalk and Perl.
>
>
> > I was further told that fortran is obsolete, but still lots of
> > fortran guys using it.
>
> Absolutely and Lisp and COBOL ghave both been predicted to die for decades
> but are still marching on. It is very difficult to kill a popular language
> because the thing that made it popular keeps it
> alive.
>

> Ignore rumours and focus on programming. Don't worry about languages, once
> you know one you'll usually pick up another very quickly. They come and go
> in fashion but the concepts remain constant. Most programmers work in
> multiple languages, often even in a single
> project.

Thanks again,

>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina


> The 'with' syntax above implicitly closes the file for you
> at the end of the block. If you use the
>
> f = open(...)
>
> style you are expected to explicitly close the file when you are finished
> with it. This is especially important when writing to a file because that
> will guarantee that the OS writes the data to the file on disk. Without a
> close its possible for your data to  get held in a buffer that doesn't get
> written to disk. Rare, but possible.
>
> Thanks. BTW, are there some tricks in using gedit input code.

such as I wanna backspace, but usually won't count 4 spaces, and sometimes I
wanna more back space,
like move:

>>> a
to
>>> a

the "prompt" sign here to give a reference position of a, I am interested in
gedit, not idle here.

Thanks,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 18:56, lina wrote:

On Oct 8, 2011, at 0:39, "Prasad, Ramit"  wrote:
 I would really change this to explicitly close the file.


with open(base+OUTFILEEXT,"w") as f:
f.write(str(summary))


Btw, I do notice lots of suggestions of closing file.
Does your above sentence close the file here? Implicitly ?
Sorry, I don't see "close"


The 'with' syntax above implicitly closes the file for you
at the end of the block. If you use the

f = open(...)

style you are expected to explicitly close the file when you are 
finished with it. This is especially important when writing to a file 
because that will guarantee that the OS writes the data to the file on 
disk. Without a close its possible for your data to  get held in a 
buffer that doesn't get written to disk. Rare, but possible.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 16:40, lina wrote:


but today I was also discouraged, I was told that you should not have
learned python, you should focus on C or bash, or D, cause python is
going to be obsolete,


C is a great language for writing Operating Systems and other "near the 
metal" code. But its not the best language for busiess apps, artificial 
intelligence and a host of other things. Bash is a good user shell, but 
its not even the best Unix shell for scripting (Thats probably ksh).

D? Who actually uses D? I think your informant was not well informed.

AS fopr Python becoming obsolete? Maybe someday, but not soon.
And even if it does the principles it teaches are alive and well in many 
other languages - eg, Ruby, Lua both openly borrow from Python.

Just as Python borrows from Lisp and Haskell and Smalltalk and Perl.

> I was further told that fortran is obsolete, but still lots of
> fortran guys using it.

Absolutely and Lisp and COBOL ghave both been predicted to die for 
decades but are still marching on. It is very difficult to kill a 
popular language because the thing that made it popular keeps it

alive.

Ignore rumours and focus on programming. Don't worry about languages, 
once you know one you'll usually pick up another very quickly. They come 
and go in fashion but the concepts remain constant. Most programmers 
work in multiple languages, often even in a single

project.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Oct 8, 2011, at 0:39, "Prasad, Ramit"  wrote:

>> open(base+OUTFILEEXT,"w").write(str(summary))
> Unless Python3 is different with respect to files, I would really change this 
> to explicitly close the file. In general, I think explicitly closing 
> resources (database connections, files, etc) are a Good Thing.
> 
> with open(base+OUTFILEEXT,"w") as f:
>f.write(str(summary))
> 
Btw, I do notice lots of suggestions of closing file. 
Does your above sentence close the file here? Implicitly ?
Sorry, I don't see "close" 
And how do I know it's safely closed already?

I will check further later. Now reading email on phone. 

Thanks with best regards,

> 
> 
> Ramit
> 
> 
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
> 
> 
> 
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.  
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Oct 8, 2011, at 0:33, "Prasad, Ramit"  wrote:

>> what does the 
>> 
>>   for col, ch in enumerate(line):
>> 
> 
> I highly recommend looking at web documentation when you can. It is not that 
> I have any problems answering any questions, but I know I get frustrated when 
> I am forced to learn things in 5 minute increments while waiting on people to 
> respond. Reading the documentation will give a better idea of Python because 
> there is *so* much more information there and the Python docs are actually 
> quite well written.
> 
> Please read the following link and then ask us about any questions you have. 
> http://docs.python.org/dev/library/functions.html#enumerate 
> 

Actually before I asked the question. I did some basic check before.
here is another way/channel of looking for answers. 
Later I also checked online. 
And learned what the enumerate mean, 
But at that time, when I asked on list, I did really don't understand. 
Sometimes the question I asked here is also the question I asked to myself. 
But thanks for your suggestion. 
When I saw soneone asked some very simple question on another list which I know 
that staff long. I also felt that way and wanna suggest them to google or use 
manual. 
> 
> 
>> but today I was also discouraged, I was told that you should not have 
>> learned python, you should focus on C or bash, or D, cause python is going 
>> to be obsolete, and I defensed that I saw lots people/apps using python, and 
>> it's always good to learn a language well, it's helpful for you to 
>> understand another language. I was further told that fortran is obsolete, 
>> but still lots of fortran guys using it.
>> I don't know much, since I started and wanted to learn, actually I wish to 
>> hear some encouraging words, not some words discouraging, but on another 
>> hands, it's always good to have an open mind, so I want to hear your opinion 
>> and >perspective, I am not knowledgable about this.
> 
> Language choice is an often debated subject. People are always proclaiming 
> that a certain language is the "best" and this list probably considers Python 
> the "best". The truth is there is no "best" language! The "best" language (in 
> my not-so-humble opinion) is the language that allows you to effectively, 
> quickly, and easily solve a specific problem. Use the best tool (i.e. 
> language) for the problem; just because you know how to use a hammer does not 
> mean that you should use it for cooking! :)
^_^  good point. 
> 
> As for encouraging words, well...that is not really something I am good at. 
> When you get stuck or bogged down in frustration, remember that learning 
> occurs most often by making mistakes first and then learning to avoid those 
> mistakes.
Thanks. 
> 
> Ramit
> 
> 
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
> 
> 
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.  
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Prasad, Ramit
>what does the 
>
>for col, ch in enumerate(line):
>

I highly recommend looking at web documentation when you can. It is not that I 
have any problems answering any questions, but I know I get frustrated when I 
am forced to learn things in 5 minute increments while waiting on people to 
respond. Reading the documentation will give a better idea of Python because 
there is *so* much more information there and the Python docs are actually 
quite well written.

Please read the following link and then ask us about any questions you have. 
http://docs.python.org/dev/library/functions.html#enumerate 



>but today I was also discouraged, I was told that you should not have learned 
>python, you should focus on C or bash, or D, cause python is going to be 
>obsolete, and I defensed that I saw lots people/apps using python, and it's 
>always good to learn a language well, it's helpful for you to understand 
>another language. I was further told that fortran is obsolete, but still lots 
>of fortran guys using it.
>I don't know much, since I started and wanted to learn, actually I wish to 
>hear some encouraging words, not some words discouraging, but on another 
>hands, it's always good to have an open mind, so I want to hear your opinion 
>and >perspective, I am not knowledgable about this.

Language choice is an often debated subject. People are always proclaiming that 
a certain language is the "best" and this list probably considers Python the 
"best". The truth is there is no "best" language! The "best" language (in my 
not-so-humble opinion) is the language that allows you to effectively, quickly, 
and easily solve a specific problem. Use the best tool (i.e. language) for the 
problem; just because you know how to use a hammer does not mean that you 
should use it for cooking! :)

As for encouraging words, well...that is not really something I am good at. 
When you get stuck or bogged down in frustration, remember that learning occurs 
most often by making mistakes first and then learning to avoid those mistakes.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Prasad, Ramit
> open(base+OUTFILEEXT,"w").write(str(summary))
Unless Python3 is different with respect to files, I would really change this 
to explicitly close the file. In general, I think explicitly closing resources 
(database connections, files, etc) are a Good Thing.

with open(base+OUTFILEEXT,"w") as f:
f.write(str(summary))



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
#!/usr/bin/python3

import os.path
import glob

TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"


if __name__=="__main__":

for fileName in glob.glob('*.xpm'):
base, ext =os.path.splitext(fileName)
text=open(fileName).readlines()
text=text[LINESTOSKIP:]
numcolumns=len(text[0])
results={}
for ch in TOKENS:
results[ch] = [0]*numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in TOKENS:
results[ch][col]+=1
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)
print(summary)
open(base+OUTFILEEXT,"w").write(str(summary))

I test it, it finally works. (I was not so sensitive about indentation also)

If you have time or interest, hope to help polish the code,
and warmly welcome to make above one a bit sophisticated. better not
easy-readable for me at first sight.

Thanks again,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
what does the

for col, ch in enumerate(line):

enumerate(line) mean, I used idle3 test enumerate, it always showed me:

>>> a
'abcde'
>>> enumerate(a)


I don't have a deep understanding.

Thanks all of you, for your kindness of giving advice and your patience in
explaination.

also another question,

sometimes in my life, I made lots of blind choice,
today I was kinda of happy telling a friend that I feel I finally started on
the road of learning or potentially using python in future.
As I mentioned before, I really did learn it on and off, study one week and
then give up, never give it a deep understanding.
but today I was also discouraged, I was told that you should not have
learned python, you should focus on C or bash, or D, cause python is going
to be obsolete, and I defensed that I saw lots people/apps using python, and
it's always good to learn a language well, it's helpful for you to
understand another language. I was further told that fortran is obsolete,
but still lots of fortran guys using it.
I don't know much, since I started and wanted to learn, actually I wish to
hear some encouraging words, not some words discouraging, but on another
hands, it's always good to have an open mind, so I want to hear your opinion
and perspective, I am not knowledgable about this.

Thanks again for all your time and guide,

Best regards,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 11:03 PM, Alan Gauld wrote:

> On 07/10/11 13:21, lina wrote:
>
> One simple explanation:  it continued on to the next file, which has
>>neither "E" nor "B" in it.
>>
>> In this directory, I only kept one file. try.xpm
>>
>>  $ more try.xpm
>> aaEbb
>> aEEbb
>> EaEbb
>> EaEbE
>>
>> $ ls
>> counter-vertically-v2.py  try.xpm
>> counter-vertically.py   try.txt
>>
>
> That's 4 files.
> And your code will try to process all of them, including the Python
> scripts.
>


#!/usr/bin/python3

import os.path
import glob

TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"


if __name__=="__main__":

for fileName in glob.glob('*.xpm'):
base, ext =os.path.splitext(fileName)
text=open(fileName).readlines()
text=text[LINESTOSKIP:]
numcolumns=len(text[0])
results={}
for ch in TOKENS:
results[ch] = [0]*numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in TOKENS:
results[ch][col]+=1
print(results)
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)
print(summary)
open(base+OUTFILEEXT,"w").write(str(summary))

$ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE


$ python3 counter-vertically-v4.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
[1, 0, 1, 0, 1, 0]


Huge unexpected problems, it's only output 1 or 0,
the summary results is not correct.



>
> I think that's a fundamental problem, you should use glob.glob() to ensure
> you only process the files you are interested in.
>
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 13:21, lina wrote:


One simple explanation:  it continued on to the next file, which has
neither "E" nor "B" in it.

In this directory, I only kept one file. try.xpm

  $ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE

$ ls
counter-vertically-v2.py  try.xpm
counter-vertically.py   try.txt


That's 4 files.
And your code will try to process all of them, including the Python scripts.

I think that's a fundamental problem, you should use glob.glob() to 
ensure you only process the files you are interested in.



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 11:25, Dave Angel wrote:


for a,b in zip(results['E'],results['B']):
summary.append(a+b)


I don't know why this gives a key error on 'E' (which basically means
that there is no key 'E') since the code above should guarantee that
it exists. Odd. I'm also not sure why the error occurs after it prints
summary. Are you sure the output is in the sequence you showed in your
message?


One simple explanation: it continued on to the next file, which has
neither "E" nor "B" in it.


But even then this bit of code should ensure that a list of zeros exists:

def processfile(infilename):
results={}
base, ext =os.path.splitext(infilename)
if ext == INFILEEXT:
text = fetchonefiledata(infilename)
numcolumns=len(text[0])
for ch in TOKENS:
results[ch] = [0]*numcolumns

Unless of course the second file doesn't have the right file extension!
In that case it would jump to process the summary with no results data.

for k,v in results.items():
print(results)
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)

Yep, that would do it!

Lina, it looks like you need to either move the initialisation of 
results above the extension check, or else move the summary check inside 
the if block.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina


>
> Now fixed the excessive output.
>
> Thanks,
>
> but in another case, seems there is a problem, for the line actually is:
>
> "EEES~~EE~EE~EE~
>
> E~~EEE~E
> EE~EE~",
>
> not naked EEEor whatever. it's already in ""
>

They treated " "  as char,

but I don't understand why the output is so many zeros ? and only zero for
my case
because each line ends up with , ?

print(text) showed:

SSST",\n', '"E

Thanks for any hint,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 4:08 PM, lina  wrote:

>
>
> On Fri, Oct 7, 2011 at 3:39 PM, lina  wrote:
>
>>
>>
>> On Fri, Oct 7, 2011 at 9:50 AM, Steven D'Aprano wrote:
>>
>>> lina wrote:
>>>
>>>  May I ask a further question:

  a
>>>
>> {'B': [4, 5, 6], 'E': {1, 2, 3}}

>>>
>>> Why is a['B'] a list and a['E'] a set?
>>>
>>>
>>>
>>>
>>>  How can I get the value of
 set(a['E'])+set(a['B'])

 I mean, get a new dict 'B+E':[5,7,9]

>>>
>>>
>>> You are confusing different things into one question, as if I had asked:
>>>
>>> "How do I make a hard boiled egg? I mean, get a potato salad."
>>>
>>> You must ask a clear question to get a clear answer.
>>>
>>>
>>>
>>> To answer your first question, what do you mean by adding two sets? I can
>>> take the *union* of two sets (anything in either one OR the other):
>>>
>>> >>> a['E'] | set(a['B'])  # one is already a set, no need to convert
>>> {1, 2, 3, 4, 5, 6}
>>>
>>>
>>> or I can take the *intersection* of the two sets (anything in both one
>>> AND the other):
>>>
>>> >>> a['E'] & set(a['B'])
>>> set()
>>>
>>> There are no items in common between the two, so nothing in the
>>> intersection.
>>>
>>>
>>> To get the result you are asking for:
>>>
>>> [5, 7, 9]
>>>
>>> makes no sense. How do you expect to get a *list* by combining two
>>> *sets*? They are different things. Lists have order, sets do not:
>>>
>>> >>> [1, 2, 3] == [3, 2, 1]
>>> False
>>> >>> {1, 2, 3} == {3, 2, 1}
>>> True
>>>
>>>
>>> A list is a sequence of values in order, a set is like a jumble of values
>>> tossed in a bag.
>>>
>>> My *guess* is that you don't care about sets at all, you want two lists:
>>>
>>
>> Thanks, I did not realize the great differences between the list and sets.
>> I was not so sensitive about the concepts before.
>>
>>>
>>>
>>> [1, 2, 3]
>>> [4, 5, 6]
>>>
>>>
>>> and you want to add them item by item to get another list:
>>>
>>> [5, 7, 9]
>>>
>>>
>>> Have I guessed correctly?
>>>
>>>
>>> If so, here's the hard way to do it:
>>>
>>>
>>> first_list = [1, 2, 3]
>>> second_list = [4, 5, 6]
>>> result = []
>>> for i in range(3):
>>>a = first_list[i]
>>>b = second_list[i]
>>>result.append(a + b)
>>>
>>> print(result)
>>>
>>>
>>> Walking along two lists in lock-step like that is so common that Python
>>> has a dedicated function specially for it: zip.
>>>
>>> result = []
>>> for a,b in zip(first_list, second_list):
>>>result.append(a+b)
>>>
>>>
>>> which can be simplified further to a list comprehension:
>>>
>>> result = [a+b for a,b in zip(first_list, second_list)]
>>>
>>>
>> Thanks, just why the output it's something double, more than I want.
>>
>> #!/bin/python3
>>
>> import os.path
>>
>> TOKENS="BE"
>>
>> LINESTOSKIP=0
>> INFILEEXT=".xpm"
>> OUTFILEEXT=".txt"
>>
>> def dofiles(topdirectory):
>> for filename in os.listdir(topdirectory):
>> processfile(filename)
>>
>> def processfile(infilename):
>> results={}
>>
>> base, ext =os.path.splitext(infilename)
>> if ext == INFILEEXT:
>> text = fetchonefiledata(infilename)
>> numcolumns=len(text[0])
>> for ch in TOKENS:
>> results[ch] = [0]*numcolumns
>> for line in text:
>> line = line.strip()
>> for col, ch in enumerate(line):
>> if ch in TOKENS:
>> results[ch][col]+=1
>> for k,v in results.items():
>>
> My mistake, here should remove the "for k,v in results.items()"

> print(results)
>> summary=[]
>> for a,b in zip(results['E'],results['B']):
>> summary.append(a+b)
>> writeonefiledata(base+OUTFILEEXT,summary)
>>
>>
>> def fetchonefiledata(inname):
>> infile = open(inname)
>> text = infile.readlines()
>> return text[LINESTOSKIP:]
>>
>> def writeonefiledata(outname,summary):
>>
>> outfile = open(outname,"w")
>> for elem in summary:
>>
> another mistake here, I shouldn't have used "for elem in summary"

> outfile.write(str(summary))
>>
>>
>>
>> if __name__=="__main__":
>> dofiles(".")
>>
>>
>>  $ python3 counter-vertically-v2.py
>> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
>> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
>>
>> $ more try.txt
>> [1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1,
>> 0][1, 0, 1
>> , 0, 1, 0][1, 0, 1, 0, 1, 0]
>>
>> $ more try.xpm
>> aaEbb
>> aEEbb
>> EaEbb
>> EaEbE
>>
>> Thanks,
>>
>
> I thought it might be some loop reason made it double output the results,
> so I made an adjustation in indent, now it showed:
>
> $ python3 counter-vertically-v2.py
> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
> [1, 0, 1, 0, 1, 0]
> Traceback (most recent call last):
>   File "counter-vertically-v2.py", line 48, in 
> dofiles(".")
>   File "counter-vertically-v2.py", line 13, in dofiles
> processfile(filename)
>   File "counter-vertically-v2.py", line 31, in processfil

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 9:08 PM, Andreas Perstinger <
andreas.perstin...@gmx.net> wrote:

> On 2011-10-07 14:21, lina wrote:
>
>>  I don't know why this gives a key error on 'E' (which basically means
 that
  there is no key 'E') since the code above should guarantee that it
 exists.
  Odd. I'm also not sure why the error occurs after it prints summary.
 Are you
  sure the output is in the sequence you showed in your message?

  One simple explanation:  it continued on to the next file, which has

>>>  neither "E" nor "B" in it.
>>>
>>>  In this directory, I only kept one file. try.xpm
>>
>
> That's wrong.
>
>
>   $ more try.xpm
>> aaEbb
>> aEEbb
>> EaEbb
>> EaEbE
>>
>> $ ls
>> counter-vertically-v2.py  try.xpm
>> counter-vertically.py   try.txt
>>
>
> As "ls" proves, you have *4* files in the directory.
>
> You start your script with "dofiles(".")". This function gets a list of
> *all* files in the directory in an *arbitrary* order and processes each of
> it.
>
> In your function "processfile" you first create an empty dictionary
> ("results = {}") and then you put values into the dictionary *only* for
> xpm-files ("if ext == INFILEEXT:"). *But* you print the summary for *every*
> file because the indentation at the end of the function is outside the
> if-branch where you check for the file extension. So for every file which
> isn't a xpm-file, "results" is an empty dictionary (see first line of the
> function) and therefore "zip(results['E'], results['B'])" will throw an
> exception.
>
> How to solve it? I personally would check for the file extension in the
> function "dofiles" and would only continue with xpm-files (in other words
> move the if-statement from "processfile" to "dofiles".)
>
 def processfile(infilename):
results={}
base, ext =os.path.splitext(infilename)
if ext == INFILEEXT:
text = fetchonefiledata(infilename)
numcolumns=len(text[0])
for ch in TOKENS:
results[ch] = [0]*numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in TOKENS:
results[ch][col]+=1
for k,v in results.items():
print(results)
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)
writeonefiledata(base+OUTFILEEXT,summary)
else:
os.sys.exit()

This part has already given the indentation. if moved it a bit further,

it would show more like:
$ python3 counter-vertically-v2.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}

Thanks again,

>
> Bye, Andreas
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Andreas Perstinger

On 2011-10-07 14:21, lina wrote:

 I don't know why this gives a key error on 'E' (which basically means that
 there is no key 'E') since the code above should guarantee that it exists.
 Odd. I'm also not sure why the error occurs after it prints summary. Are you
 sure the output is in the sequence you showed in your message?

  One simple explanation:  it continued on to the next file, which has

 neither "E" nor "B" in it.


In this directory, I only kept one file. try.xpm


That's wrong.


  $ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE

$ ls
counter-vertically-v2.py  try.xpm
counter-vertically.py   try.txt


As "ls" proves, you have *4* files in the directory.

You start your script with "dofiles(".")". This function gets a list of 
*all* files in the directory in an *arbitrary* order and processes each 
of it.


In your function "processfile" you first create an empty dictionary 
("results = {}") and then you put values into the dictionary *only* for 
xpm-files ("if ext == INFILEEXT:"). *But* you print the summary for 
*every* file because the indentation at the end of the function is 
outside the if-branch where you check for the file extension. So for 
every file which isn't a xpm-file, "results" is an empty dictionary (see 
first line of the function) and therefore "zip(results['E'], 
results['B'])" will throw an exception.


How to solve it? I personally would check for the file extension in the 
function "dofiles" and would only continue with xpm-files (in other 
words move the if-statement from "processfile" to "dofiles".)


Bye, Andreas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
When I put it into a real case,

it showed me all as 0 0 0 s

The python code and the real one file can be accessed from below link:

https://docs.google.com/leaf?id=0B93SVRfpVVg3MjQ0YzEzOWUtYWU0MC00YzAwLWJiYTctY2E5YTEzY2U0NGI3&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3MWMxZDg0MmItOTNiMi00NGZkLWEzMGEtZDU5NzI1YjRlZjU3&hl=en_GB

Thanks,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 6:25 PM, Dave Angel  wrote:

> On 10/07/2011 06:06 AM, Alan Gauld wrote:
>
>> On 07/10/11 09:08, lina wrote:
>> 
>>
>>  summary=[]
>>> for a,b in zip(results['E'],results['B'])**:
>>> summary.append(a+b)
>>>
>>
>> I don't know why this gives a key error on 'E' (which basically means that
>> there is no key 'E') since the code above should guarantee that it exists.
>> Odd. I'm also not sure why the error occurs after it prints summary. Are you
>> sure the output is in the sequence you showed in your message?
>>
>>  One simple explanation:  it continued on to the next file, which has
> neither "E" nor "B" in it.
>
In this directory, I only kept one file. try.xpm

 $ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE

$ ls
counter-vertically-v2.py  try.xpm
counter-vertically.py   try.txt


>
>
> --
>
> DaveA
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 6:25 PM, Dave Angel  wrote:

> On 10/07/2011 06:06 AM, Alan Gauld wrote:
>
>> On 07/10/11 09:08, lina wrote:
>> 
>>
>>  summary=[]
>>> for a,b in zip(results['E'],results['B'])**:
>>> summary.append(a+b)
>>>
>>
>> I don't know why this gives a key error on 'E' (which basically means that
>> there is no key 'E') since the code above should guarantee that it exists.
>> Odd. I'm also not sure why the error occurs after it prints summary. Are you
>> sure the output is in the sequence you showed in your message?
>>
>>  One simple explanation:  it continued on to the next file, which has
> neither "E" nor "B" in it.
>
>
>
>
The final results got "6" summary out, which I only expect one.
and how can I output the summary, in the wrong situation, let it \n ?


$ more try.txt
[1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1,
0, 1
, 0, 1, 0][1, 0, 1, 0, 1, 0]



> --
>
> DaveA
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Dave Angel

On 10/07/2011 06:06 AM, Alan Gauld wrote:

On 07/10/11 09:08, lina wrote:


 summary=[]
 for a,b in zip(results['E'],results['B']):
 summary.append(a+b)


I don't know why this gives a key error on 'E' (which basically means 
that there is no key 'E') since the code above should guarantee that 
it exists. Odd. I'm also not sure why the error occurs after it prints 
summary. Are you sure the output is in the sequence you showed in your 
message?


One simple explanation:  it continued on to the next file, which has 
neither "E" nor "B" in it.




--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Dave Angel

On 10/07/2011 04:08 AM, lina wrote:


I thought it might be some loop reason made it double output the results, so
I made an adjustation in indent, now it showed:
$ python3 counter-vertically-v2.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
[1, 0, 1, 0, 1, 0]
Traceback (most recent call last):
   File "counter-vertically-v2.py", line 48, in
 dofiles(".")
   File "counter-vertically-v2.py", line 13, in dofiles
 processfile(filename)
   File "counter-vertically-v2.py", line 31, in processfile
 for a,b in zip(results['E'],results['B']):
KeyError: 'E'

still two results, but the summary is correct, with a KeyError which I don't
know how to fix the key error here.

#!/bin/python3

import os.path


TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
 for filename in os.listdir(topdirectory):
 processfile(filename)

def processfile(infilename):
 results={}
 base, ext =os.path.splitext(infilename)
 if ext == INFILEEXT:
 text = fetchonefiledata(infilename)
 numcolumns=len(text[0])
 for ch in TOKENS:
 results[ch] = [0]*numcolumns
 for line in text:
 line = line.strip()
 for col, ch in enumerate(line):
 if ch in TOKENS:
 results[ch][col]+=1
 for k,v in results.items():
 print(results)
That'll print the whole map for each item in it.  Since you apparently 
have two items, "E" and "B", you get the whole thing printed out twice.


I have no idea what you really wanted to print, but it probably was k and v


 summary=[]
 for a,b in zip(results['E'],results['B']):
 summary.append(a+b)
 print(summary)
 writeonefiledata(base+OUTFILEEXT,summary)

def fetchonefiledata(inname):
 infile = open(inname)
 text = infile.readlines()
 return text[LINESTOSKIP:]

def writeonefiledata(outname,summary):
 outfile = open(outname,"w")
 for elem in summary:
 outfile.write(str(summary))


if __name__=="__main__":
 dofiles(".")

Thanks all for your time,


As for the reason you got the exception, it probably was because the 
NEXT file had no E's in it.


One of the reasons to break this stuff into separate functions is so you 
can test them separately.  You probably should be calling processfile() 
directly in your top-level code, till it all comes out correctly.  Or at 
least add a print of the filename it's working on.



Anyway, it's  probably a mistake to ever reference "E" and "B" 
explicitly, but instead loop through the TOKENS.  That way it'll still 
work when you add more or different tokens.  Further, if it's considered 
valid for an input file not to have samples of all the tokens, then you 
have to loop through the ones you actually have.  That might mean 
looping through the keys of results.  Or, for the particular use case in 
that line, there's undoubtedly a method of results that will give you 
all the values in a list.  That list would make an even better argument 
to zip().  Once again, I remind you of the dir() function, to see 
available methods.


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 09:08, lina wrote:


TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
 for filename in os.listdir(topdirectory):
 processfile(filename)

def processfile(infilename):
 results={}
 base, ext =os.path.splitext(infilename)
 if ext == INFILEEXT:



 text = fetchonefiledata(infilename)
 numcolumns=len(text[0])
 for ch in TOKENS:
 results[ch] = [0]*numcolumns
 for line in text:
 line = line.strip()
 for col, ch in enumerate(line):
 if ch in TOKENS:
 results[ch][col]+=1


It would be easier to read(and debug) if you put
that chunk into a function. Using the naming style below
it could be called processOneFileData() for example...

Make it return the results dictionary.


 for k,v in results.items():
 print(results)


This prints the same thing (results) for as many items
are in results. I'm pretty sure you don't want that.
Just printing results once should be sufficient.


 summary=[]
 for a,b in zip(results['E'],results['B']):
 summary.append(a+b)


I don't know why this gives a key error on 'E' (which basically means 
that there is no key 'E') since the code above should guarantee that it 
exists. Odd. I'm also not sure why the error occurs after it prints 
summary. Are you sure the output is in the sequence you showed in your 
message?



 print(summary)
 writeonefiledata(base+OUTFILEEXT,summary)

def fetchonefiledata(inname):
 infile = open(inname)
 text = infile.readlines()
 return text[LINESTOSKIP:]

def writeonefiledata(outname,summary):
 outfile = open(outname,"w")
 for elem in summary:
 outfile.write(str(summary))


if __name__=="__main__":
 dofiles(".")



HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 3:39 PM, lina  wrote:

>
>
> On Fri, Oct 7, 2011 at 9:50 AM, Steven D'Aprano wrote:
>
>> lina wrote:
>>
>>  May I ask a further question:
>>>
>>>  a
>>
> {'B': [4, 5, 6], 'E': {1, 2, 3}}
>>>
>>
>> Why is a['B'] a list and a['E'] a set?
>>
>>
>>
>>
>>  How can I get the value of
>>> set(a['E'])+set(a['B'])
>>>
>>> I mean, get a new dict 'B+E':[5,7,9]
>>>
>>
>>
>> You are confusing different things into one question, as if I had asked:
>>
>> "How do I make a hard boiled egg? I mean, get a potato salad."
>>
>> You must ask a clear question to get a clear answer.
>>
>>
>>
>> To answer your first question, what do you mean by adding two sets? I can
>> take the *union* of two sets (anything in either one OR the other):
>>
>> >>> a['E'] | set(a['B'])  # one is already a set, no need to convert
>> {1, 2, 3, 4, 5, 6}
>>
>>
>> or I can take the *intersection* of the two sets (anything in both one AND
>> the other):
>>
>> >>> a['E'] & set(a['B'])
>> set()
>>
>> There are no items in common between the two, so nothing in the
>> intersection.
>>
>>
>> To get the result you are asking for:
>>
>> [5, 7, 9]
>>
>> makes no sense. How do you expect to get a *list* by combining two *sets*?
>> They are different things. Lists have order, sets do not:
>>
>> >>> [1, 2, 3] == [3, 2, 1]
>> False
>> >>> {1, 2, 3} == {3, 2, 1}
>> True
>>
>>
>> A list is a sequence of values in order, a set is like a jumble of values
>> tossed in a bag.
>>
>> My *guess* is that you don't care about sets at all, you want two lists:
>>
>
> Thanks, I did not realize the great differences between the list and sets.
> I was not so sensitive about the concepts before.
>
>>
>>
>> [1, 2, 3]
>> [4, 5, 6]
>>
>>
>> and you want to add them item by item to get another list:
>>
>> [5, 7, 9]
>>
>>
>> Have I guessed correctly?
>>
>>
>> If so, here's the hard way to do it:
>>
>>
>> first_list = [1, 2, 3]
>> second_list = [4, 5, 6]
>> result = []
>> for i in range(3):
>>a = first_list[i]
>>b = second_list[i]
>>result.append(a + b)
>>
>> print(result)
>>
>>
>> Walking along two lists in lock-step like that is so common that Python
>> has a dedicated function specially for it: zip.
>>
>> result = []
>> for a,b in zip(first_list, second_list):
>>result.append(a+b)
>>
>>
>> which can be simplified further to a list comprehension:
>>
>> result = [a+b for a,b in zip(first_list, second_list)]
>>
>>
> Thanks, just why the output it's something double, more than I want.
>
> #!/bin/python3
>
> import os.path
>
> TOKENS="BE"
>
> LINESTOSKIP=0
> INFILEEXT=".xpm"
> OUTFILEEXT=".txt"
>
> def dofiles(topdirectory):
> for filename in os.listdir(topdirectory):
> processfile(filename)
>
> def processfile(infilename):
> results={}
>
> base, ext =os.path.splitext(infilename)
> if ext == INFILEEXT:
> text = fetchonefiledata(infilename)
> numcolumns=len(text[0])
> for ch in TOKENS:
> results[ch] = [0]*numcolumns
> for line in text:
> line = line.strip()
> for col, ch in enumerate(line):
> if ch in TOKENS:
> results[ch][col]+=1
> for k,v in results.items():
> print(results)
> summary=[]
> for a,b in zip(results['E'],results['B']):
> summary.append(a+b)
> writeonefiledata(base+OUTFILEEXT,summary)
>
>
> def fetchonefiledata(inname):
> infile = open(inname)
> text = infile.readlines()
> return text[LINESTOSKIP:]
>
> def writeonefiledata(outname,summary):
>
> outfile = open(outname,"w")
> for elem in summary:
> outfile.write(str(summary))
>
>
>
> if __name__=="__main__":
> dofiles(".")
>
>
>  $ python3 counter-vertically-v2.py
> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
>
> $ more try.txt
> [1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1,
> 0, 1
> , 0, 1, 0][1, 0, 1, 0, 1, 0]
>
> $ more try.xpm
> aaEbb
> aEEbb
> EaEbb
> EaEbE
>
> Thanks,
>

I thought it might be some loop reason made it double output the results, so
I made an adjustation in indent, now it showed:
$ python3 counter-vertically-v2.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
[1, 0, 1, 0, 1, 0]
Traceback (most recent call last):
  File "counter-vertically-v2.py", line 48, in 
dofiles(".")
  File "counter-vertically-v2.py", line 13, in dofiles
processfile(filename)
  File "counter-vertically-v2.py", line 31, in processfile
for a,b in zip(results['E'],results['B']):
KeyError: 'E'

still two results, but the summary is correct, with a KeyError which I don't
know how to fix the key error here.

#!/bin/python3

import os.path


TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
for filename in os.listdir(topdirectory):
processfile(filename)

def processfile(infilename):

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 9:50 AM, Steven D'Aprano  wrote:

> lina wrote:
>
>  May I ask a further question:
>>
>>  a
>
 {'B': [4, 5, 6], 'E': {1, 2, 3}}
>>
>
> Why is a['B'] a list and a['E'] a set?
>
>
>
>
>  How can I get the value of
>> set(a['E'])+set(a['B'])
>>
>> I mean, get a new dict 'B+E':[5,7,9]
>>
>
>
> You are confusing different things into one question, as if I had asked:
>
> "How do I make a hard boiled egg? I mean, get a potato salad."
>
> You must ask a clear question to get a clear answer.
>
>
>
> To answer your first question, what do you mean by adding two sets? I can
> take the *union* of two sets (anything in either one OR the other):
>
> >>> a['E'] | set(a['B'])  # one is already a set, no need to convert
> {1, 2, 3, 4, 5, 6}
>
>
> or I can take the *intersection* of the two sets (anything in both one AND
> the other):
>
> >>> a['E'] & set(a['B'])
> set()
>
> There are no items in common between the two, so nothing in the
> intersection.
>
>
> To get the result you are asking for:
>
> [5, 7, 9]
>
> makes no sense. How do you expect to get a *list* by combining two *sets*?
> They are different things. Lists have order, sets do not:
>
> >>> [1, 2, 3] == [3, 2, 1]
> False
> >>> {1, 2, 3} == {3, 2, 1}
> True
>
>
> A list is a sequence of values in order, a set is like a jumble of values
> tossed in a bag.
>
> My *guess* is that you don't care about sets at all, you want two lists:
>

Thanks, I did not realize the great differences between the list and sets. I
was not so sensitive about the concepts before.

>
>
> [1, 2, 3]
> [4, 5, 6]
>
>
> and you want to add them item by item to get another list:
>
> [5, 7, 9]
>
>
> Have I guessed correctly?
>
>
> If so, here's the hard way to do it:
>
>
> first_list = [1, 2, 3]
> second_list = [4, 5, 6]
> result = []
> for i in range(3):
>a = first_list[i]
>b = second_list[i]
>result.append(a + b)
>
> print(result)
>
>
> Walking along two lists in lock-step like that is so common that Python has
> a dedicated function specially for it: zip.
>
> result = []
> for a,b in zip(first_list, second_list):
>result.append(a+b)
>
>
> which can be simplified further to a list comprehension:
>
> result = [a+b for a,b in zip(first_list, second_list)]
>
>
Thanks, just why the output it's something double, more than I want.

#!/bin/python3

import os.path

TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
for filename in os.listdir(topdirectory):
processfile(filename)

def processfile(infilename):
results={}
base, ext =os.path.splitext(infilename)
if ext == INFILEEXT:
text = fetchonefiledata(infilename)
numcolumns=len(text[0])
for ch in TOKENS:
results[ch] = [0]*numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in TOKENS:
results[ch][col]+=1
for k,v in results.items():
print(results)
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)
writeonefiledata(base+OUTFILEEXT,summary)

def fetchonefiledata(inname):
infile = open(inname)
text = infile.readlines()
return text[LINESTOSKIP:]

def writeonefiledata(outname,summary):
outfile = open(outname,"w")
for elem in summary:
outfile.write(str(summary))


if __name__=="__main__":
dofiles(".")


 $ python3 counter-vertically-v2.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}

$ more try.txt
[1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1,
0, 1
, 0, 1, 0][1, 0, 1, 0, 1, 0]

$ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE

Thanks,

>
>
> --
> Steven
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 9:38 AM, Dave Angel  wrote:

> On 10/06/2011 12:21 PM, lina wrote:
>
>> 
>>
>>
>>> As for splitting into functions, consider:
>>>
>>> #these two are capitalized because they're intended to be constant
>>> TOKENS = "BE"
>>> LINESTOSKIP = 43
>>> INFILEEXT = ".xpm"
>>> OUTFILEEXT = ".txt"
>>>
>>> def dofiles(topdirectory):
>>>for filename in os.listdr(topdirectory):
>>>processfile(filename)
>>>
>>> def processfile(infilename):
>>>base, ext =os.path.splitext(fileName)
>>>if ext == INFILEEXT:
>>>text = fetchonefiledata(infilename)
>>>numcolumns = len(text[0])
>>>results = {}
>>>for ch in TOKENS:
>>>
>>>results[ch] = [0] * numcolumns
>>>for line in text:
>>>line = line.strip()
>>>
>>>for col, ch in enumerate(line):
>>>if ch in tokens:
>>>results[ch][col] += 1
>>>
>>>  I still have trouble understanding the results[ch][col] part.
>>
>> Thanks ahead,
>>
>>
>>  First ask yourself what results consists of.  It's a dictionary, which is
> a aggregate of pairs of key/value items.  In each case the key is a
> character, and the the value is a list of numbers.
>
> So results[ch] is a particular value, in other words a list.
>
> And results[ch] [col] is a particular item of the list, in other words, and
> integer
>
> If we use += 1 on that integer, we increment it by 1
>
> Is that clear?   if not, write a separate program to build such as
> structure, and try printing out each level of item
> print results["E"]
> should display a list
>  print results["E"][0]
> should display an integer
>  print results["E"][1]
>  print results["E"][2]
>

Yes. I understand this part now. But how can I print a list consists of the
value of key B + E.

For {'B': [4, 5, 6], 'E': [1, 2, 3]}

I wanna get the summary of B and E in each column, namely [5, 7, 9]


>
>
> Does this help?
>
> --
>
> DaveA
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-06 Thread Steven D'Aprano

lina wrote:


May I ask a further question:


a

{'B': [4, 5, 6], 'E': {1, 2, 3}}


Why is a['B'] a list and a['E'] a set?




How can I get the value of
set(a['E'])+set(a['B'])

I mean, get a new dict 'B+E':[5,7,9]



You are confusing different things into one question, as if I had asked:

"How do I make a hard boiled egg? I mean, get a potato salad."

You must ask a clear question to get a clear answer.



To answer your first question, what do you mean by adding two sets? I 
can take the *union* of two sets (anything in either one OR the other):


>>> a['E'] | set(a['B'])  # one is already a set, no need to convert
{1, 2, 3, 4, 5, 6}


or I can take the *intersection* of the two sets (anything in both one 
AND the other):


>>> a['E'] & set(a['B'])
set()

There are no items in common between the two, so nothing in the 
intersection.



To get the result you are asking for:

[5, 7, 9]

makes no sense. How do you expect to get a *list* by combining two 
*sets*? They are different things. Lists have order, sets do not:


>>> [1, 2, 3] == [3, 2, 1]
False
>>> {1, 2, 3} == {3, 2, 1}
True


A list is a sequence of values in order, a set is like a jumble of 
values tossed in a bag.


My *guess* is that you don't care about sets at all, you want two lists:


[1, 2, 3]
[4, 5, 6]


and you want to add them item by item to get another list:

[5, 7, 9]


Have I guessed correctly?


If so, here's the hard way to do it:


first_list = [1, 2, 3]
second_list = [4, 5, 6]
result = []
for i in range(3):
a = first_list[i]
b = second_list[i]
result.append(a + b)

print(result)


Walking along two lists in lock-step like that is so common that Python 
has a dedicated function specially for it: zip.


result = []
for a,b in zip(first_list, second_list):
result.append(a+b)


which can be simplified further to a list comprehension:

result = [a+b for a,b in zip(first_list, second_list)]



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-06 Thread Dave Angel

On 10/06/2011 12:21 PM, lina wrote:





As for splitting into functions, consider:

#these two are capitalized because they're intended to be constant
TOKENS = "BE"
LINESTOSKIP = 43
INFILEEXT = ".xpm"
OUTFILEEXT = ".txt"

def dofiles(topdirectory):
for filename in os.listdr(topdirectory):
processfile(filename)

def processfile(infilename):
base, ext =os.path.splitext(fileName)
if ext == INFILEEXT:
text = fetchonefiledata(infilename)
numcolumns = len(text[0])
results = {}
for ch in TOKENS:

results[ch] = [0] * numcolumns
for line in text:
line = line.strip()

for col, ch in enumerate(line):
if ch in tokens:
results[ch][col] += 1


I still have trouble understanding the results[ch][col] part.

Thanks ahead,


First ask yourself what results consists of.  It's a dictionary, which 
is a aggregate of pairs of key/value items.  In each case the key is a 
character, and the the value is a list of numbers.


So results[ch] is a particular value, in other words a list.

And results[ch] [col] is a particular item of the list, in other words, 
and integer


If we use += 1 on that integer, we increment it by 1

Is that clear?   if not, write a separate program to build such as 
structure, and try printing out each level of item

 print results["E"]
should display a list
  print results["E"][0]
should display an integer
  print results["E"][1]
  print results["E"][2]


Does this help?

--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-06 Thread lina


>
>
> As for splitting into functions, consider:
>
> #these two are capitalized because they're intended to be constant
> TOKENS = "BE"
> LINESTOSKIP = 43
> INFILEEXT = ".xpm"
> OUTFILEEXT = ".txt"
>
> def dofiles(topdirectory):
>for filename in os.listdr(topdirectory):
>processfile(filename)
>
> def processfile(infilename):
>base, ext =os.path.splitext(fileName)
>if ext == INFILEEXT:
>text = fetchonefiledata(infilename)
>numcolumns = len(text[0])
>results = {}
>for ch in TOKENS:
>
>results[ch] = [0] * numcolumns
>for line in text:
>line = line.strip()
>
>for col, ch in enumerate(line):
>if ch in tokens:
>results[ch][col] += 1
>
I still have trouble understanding the results[ch][col] part.

Thanks ahead,


>writeonefiledata(base+**OUTFILEEXT, results)
>
> def fetchonefiledata(inname):
>infile = open(inname)
>text = infile.readlines()
>return text[LINESTOSKIP:]
>
> def writeonefiledata(outname):
>outfile = open(outname, "w")
>...process the results as appropriate...
>(since you didn't tell us how multiple tokens were to be displayed)
>
> if __name__ == "__main__":
>dofiles(".") #or get the top directory from the sys.argv variable,
> which is set from command line.
>
>
>
>  --
>
> DaveA
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-06 Thread Andreas Perstinger

On 2011-10-06 16:11, lina wrote:

I still don't know how to (standard) convert the list values to a string.

def writeonefiledata(outname,results):
 outfile = open(outname,"w")
 for key, value in results.items():
 print(value)
 outfile.write(str(results[key]))
Is it a wrong way?


There isn't really a wrong way because it's completely up to you how the 
list (or its elements) should be represented. As you will have noticed, 
"str(list)" just gives you the standard representation in Python (all 
list elements separated by a comma inside square brackets):

>>> str([1, 2, 3, 4])
'[1, 2, 3, 4]'

But I think you want something else because earlier you wrote:

On 2011-10-04 18:38, lina wrote:
> For file:
>
> aaEbb
> aEEbb
> EaEbb
> EaEbE
>
> the expected output is
>
> 2 1 0 1

(Although I suppose, as Alan already noticed, that you've forgotten to 
mention the third column and the output should be: 2 1 4 0 1)


So you have to build your own string. You could use the join method. It 
takes a list and concatenates the list elements to a string separated by 
the given string object:

>>> "".join(["1", "2", "3"])
'123'
>>> "-".join(["1", "2", "3"])
'1-2-3'

The problem now is that "join" will only join the list elements if they 
are strings but in your case the list elements are integers. You have to 
convert them before you can concatenate them. In Python that's usually 
the job of a list comprehension but I'm not sure if you already know 
what that is. So for a start you can also use a for-loop:

>>> l = []
>>> for number in [1, 2, 3]:
... l.append(str(number))
...
>>> l
["1", "2", "3"]

You must also be aware that when you write your new strings into a file 
that "write" doesn't append a newline ("\n") to each string (in contrast 
to "print"). So you have to do it manually if you don't want to write 
just a long list of numbers.


Bye, Andreas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-06 Thread lina
On Thu, Oct 6, 2011 at 4:33 AM, Prasad, Ramit wrote:

> >>yes, you're iterating over the keys of a dictionary.  Since it only has
> the key "E", that's what you get.  Try printing dir(results) to see what
> methods might return something other than the key.  Make the language work
> for you.
>
> >Sorry I am not smart.  value?
>
> Dictionaries {} are containers for key/value based pairs like { key :
> value, another_key : value(can be same or repeated) }
>
> For example:
> {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
> The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0] (for
> key 'B') and [2, 1, 4, 0, 1, 0] (for key 'E')
>
> You can get the value of a dictionary by doing: value = dictionary[key]
> You can set the value of a dictionary by doing: dictionary[key] = value
>
> You will get an exception if you try to get a value for a key that is not
> in the dictionary.
> >>> dictionary = {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
> >>> dictionary['F']
> KeyError: 'F'
>
> You can iterate over dictionaries in a couple ways
> 1-
> for key in dictionary: # also the same as
>  # for key in dictionary.keys()
>value = dictionary[ key ]
> 2-
> for key, value in dictionary.iteritems():
>< do something here with >
>
Now finally figure out what's the string, list and dictionary.
May I ask a further question:

>>> a
{'B': [4, 5, 6], 'E': {1, 2, 3}}

How can I get the value of
set(a['E'])+set(a['B'])

I mean, get a new dict 'B+E':[5,7,9]

Thanks,






>
>
>
> Ramit
>
>
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
>
>
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-06 Thread lina
On Thu, Oct 6, 2011 at 1:39 PM, Andreas Perstinger <
andreas.perstin...@gmx.net> wrote:

> On 2011-10-06 05:46, lina wrote:
>
>> On Thu, Oct 6, 2011 at 4:33 AM, Prasad, 
>> Ramit
>> >wrote:
>>
>>>  Dictionaries {} are containers for key/value based pairs like { key :
>>>
>>>  value, another_key : value(can be same or repeated) }
>>>
>>>  For example:
>>>  {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
>>>  The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0]
>>> (for
>>>
>>  ^^**^^^
>
>>  key 'B') and [2, 1, 4, 0, 1, 0] (for key 'E')
>>>
>>
>>  def writeonefiledata(outname,**results):
>> outfile = open(outname,"w")
>> for key in results:
>> return outfile.write(results[key])
>>
>> $ python3 counter-vertically-v2.py
>> Traceback (most recent call last):
>>   File "counter-vertically-v2.py", line 43, in
>> dofiles(".")
>>   File "counter-vertically-v2.py", line 12, in dofiles
>> processfile(filename)
>>   File "counter-vertically-v2.py", line 29, in processfile
>> writeonefiledata(base+**OUTFILEEXT,results)
>>   File "counter-vertically-v2.py", line 39, in writeonefiledata
>> return outfile.write(results[key])
>> TypeError: must be str, not list
>>
>^
> The error message tells you, that "results[key]" is a list but "write" just
> excepts a string. (see Ramit's explanation above).
> You have to convert the list values to a string.
>
I still don't know how to (standard) convert the list values to a string.

def writeonefiledata(outname,results):
outfile = open(outname,"w")
for key, value in results.items():
print(value)
outfile.write(str(results[key]))
Is it a wrong way?

Thanks all for the help.


> BTW: You shouldn't return the write operation because that will exit your
> function after the first iteration.



>
>
>  def writeonefiledata(outname,**results):
>> outfile = open(outname,"w")
>> for key, value in results.iteritems():
>> return outfile.write(key)
>>
>> $ python3 counter-vertically-v2.py
>> Traceback (most recent call last):
>>   File "counter-vertically-v2.py", line 43, in
>> dofiles(".")
>>   File "counter-vertically-v2.py", line 12, in dofiles
>> processfile(filename)
>>   File "counter-vertically-v2.py", line 29, in processfile
>> writeonefiledata(base+**OUTFILEEXT,results)
>>   File "counter-vertically-v2.py", line 38, in writeonefiledata
>> for key, value in results.iteritems():
>> AttributeError: 'dict' object has no attribute 'iteritems'
>>
>
> In Python 3 there is no "dict.iteritems()" any more:
> http://docs.python.org/py3k/**whatsnew/3.0.html#views-and-**
> iterators-instead-of-lists
> Use "dict.items()" instead
>
> Bye, Andreas
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-06 Thread lina
On Thu, Oct 6, 2011 at 5:18 PM, Alan Gauld wrote:

> On 06/10/11 04:54, lina wrote:
>
> If you use IDLE, the standard IDE that comes with Python, you should
>>find that hitting tab (or pausing briefly) in a file editor will
>>bring up a pick list of options.
>>
>> Just tried the  idle-python2.6,
>> Q1: Is it bound with certain python version, such as for python3, do I
>> need get idle-python3 ?
>>
>
> Yes, you need IDLE for V3.
> IDLE is a python program itself so in theory you could run earlier versions
> but the version for V3 uses V3 syntax so you need to match the versions. If
> you are using Linux your package manager should have an IDLE v3 package
> available.
>
>
>
>  Q2: how to use the last command I input, such as I typed something wrong
>> and I wanna correct in the next step.
>>
>
> Use Alt-p(previous) to go back through history
> Use Alt-n(next) to go forward.
> You can also cursor up to the command you want(or use the mouse) and hit
> return to bring it into the prompt.

Thanks, this is helpful. ^_^

>
>
>  seems no much difference with using terminal directly. I might be wrong
>> here.
>>
>
> You get syntax colouring and auto indentation as well as the tool tips.
> There is a slight "bug" in the interactive window in that the alignment is
> off for some statements(*) but if typing code into a file it all works fine.
>
>
> (*)
> >>> if True:
>x = 42
> else:
>y = 42
>
>
> Notice the else is out of line with if because of the >>>
>
>
> Other features of IDLE are a graphical debugger that allows you to step
> through your code line by line, A syntax checker, a class browser(for when
> you get into objects). There is also a "code context" option but I have no
> idea what it does!


Thanks for your time.

>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-06 Thread Alan Gauld

On 06/10/11 04:54, lina wrote:


If you use IDLE, the standard IDE that comes with Python, you should
find that hitting tab (or pausing briefly) in a file editor will
bring up a pick list of options.

Just tried the  idle-python2.6,
Q1: Is it bound with certain python version, such as for python3, do I
need get idle-python3 ?


Yes, you need IDLE for V3.
IDLE is a python program itself so in theory you could run earlier 
versions but the version for V3 uses V3 syntax so you need to match the 
versions. If you are using Linux your package manager should have an 
IDLE v3 package available.




Q2: how to use the last command I input, such as I typed something wrong
and I wanna correct in the next step.


Use Alt-p(previous) to go back through history
Use Alt-n(next) to go forward.
You can also cursor up to the command you want(or use the mouse) and hit 
return to bring it into the prompt.



seems no much difference with using terminal directly. I might be wrong
here.


You get syntax colouring and auto indentation as well as the tool tips.
There is a slight "bug" in the interactive window in that the alignment 
is off for some statements(*) but if typing code into a file it all 
works fine.



(*)
>>> if True:
x = 42
else:
y = 42


Notice the else is out of line with if because of the >>>


Other features of IDLE are a graphical debugger that allows you to step 
through your code line by line, A syntax checker, a class browser(for 
when you get into objects). There is also a "code context" option but I 
have no idea what it does!


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread Andreas Perstinger

On 2011-10-06 05:46, lina wrote:

On Thu, Oct 6, 2011 at 4:33 AM, Prasad, Ramitwrote:

 Dictionaries {} are containers for key/value based pairs like { key :
 value, another_key : value(can be same or repeated) }

 For example:
 {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
 The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0] (for

  ^

 key 'B') and [2, 1, 4, 0, 1, 0] (for key 'E')


  def writeonefiledata(outname,results):
 outfile = open(outname,"w")
 for key in results:
 return outfile.write(results[key])

$ python3 counter-vertically-v2.py
Traceback (most recent call last):
   File "counter-vertically-v2.py", line 43, in
 dofiles(".")
   File "counter-vertically-v2.py", line 12, in dofiles
 processfile(filename)
   File "counter-vertically-v2.py", line 29, in processfile
 writeonefiledata(base+OUTFILEEXT,results)
   File "counter-vertically-v2.py", line 39, in writeonefiledata
 return outfile.write(results[key])
TypeError: must be str, not list

^
The error message tells you, that "results[key]" is a list but "write" 
just excepts a string. (see Ramit's explanation above).

You have to convert the list values to a string.
BTW: You shouldn't return the write operation because that will exit 
your function after the first iteration.



def writeonefiledata(outname,results):
 outfile = open(outname,"w")
 for key, value in results.iteritems():
 return outfile.write(key)

$ python3 counter-vertically-v2.py
Traceback (most recent call last):
   File "counter-vertically-v2.py", line 43, in
 dofiles(".")
   File "counter-vertically-v2.py", line 12, in dofiles
 processfile(filename)
   File "counter-vertically-v2.py", line 29, in processfile
 writeonefiledata(base+OUTFILEEXT,results)
   File "counter-vertically-v2.py", line 38, in writeonefiledata
 for key, value in results.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'


In Python 3 there is no "dict.iteritems()" any more:
http://docs.python.org/py3k/whatsnew/3.0.html#views-and-iterators-instead-of-lists
Use "dict.items()" instead

Bye, Andreas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread lina
On Thu, Oct 6, 2011 at 2:01 AM, Alan Gauld wrote:

> On 05/10/11 13:46, lina wrote:
>
>  another question, you know in linux, when use TAB, can automatically
>> input something,
>> so in python3, are there some way they can intelligent give some hints
>> or fill the left.
>>
>
> What development tool are you using?
>
Ha... I used gedit.

> If you use IDLE, the standard IDE that comes with Python, you should find
> that hitting tab (or pausing briefly) in a file editor will bring up a pick
> list of options.
>
> Just tried the  idle-python2.6,
Q1: Is it bound with certain python version, such as for python3, do I need
get idle-python3 ?
Q2: how to use the last command I input, such as I typed something wrong and
I wanna correct in the next step.
seems no much difference with using terminal directly. I might be wrong
here.

Thanks,


> Similarly typing a function name and an open paren will give a tooltip
> for the functions parameters.
>
> IPython will do some similar things for you as a replacement for the
> standard Python interpreter (plus a bunch of extras).
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread lina
On Thu, Oct 6, 2011 at 4:33 AM, Prasad, Ramit wrote:

> >>yes, you're iterating over the keys of a dictionary.  Since it only has
> the key "E", that's what you get.  Try printing dir(results) to see what
> methods might return something other than the key.  Make the language work
> for you.
>
> >Sorry I am not smart.  value?
>
> Dictionaries {} are containers for key/value based pairs like { key :
> value, another_key : value(can be same or repeated) }
>
> For example:
> {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
> The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0] (for
> key 'B') and [2, 1, 4, 0, 1, 0] (for key 'E')
>
> You can get the value of a dictionary by doing: value = dictionary[key]
> You can set the value of a dictionary by doing: dictionary[key] = value
>
> You will get an exception if you try to get a value for a key that is not
> in the dictionary.
> >>> dictionary = {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
> >>> dictionary['F']
> KeyError: 'F'
>

I feel I started to understand what's the keys and values, but still
confused in real practice.

>
> You can iterate over dictionaries in a couple ways
> 1-
> for key in dictionary: # also the same as
>  # for key in dictionary.keys()
>value = dictionary[ key ]
>

 def writeonefiledata(outname,results):
outfile = open(outname,"w")
for key in results:
return outfile.write(results[key])

$ python3 counter-vertically-v2.py
Traceback (most recent call last):
  File "counter-vertically-v2.py", line 43, in 
dofiles(".")
  File "counter-vertically-v2.py", line 12, in dofiles
processfile(filename)
  File "counter-vertically-v2.py", line 29, in processfile
writeonefiledata(base+OUTFILEEXT,results)
  File "counter-vertically-v2.py", line 39, in writeonefiledata
return outfile.write(results[key])
TypeError: must be str, not list


2-
> for key, value in dictionary.iteritems():
>< do something here with >
>

def writeonefiledata(outname,results):
outfile = open(outname,"w")
for key, value in results.iteritems():
return outfile.write(key)

$ python3 counter-vertically-v2.py
Traceback (most recent call last):
  File "counter-vertically-v2.py", line 43, in 
dofiles(".")
  File "counter-vertically-v2.py", line 12, in dofiles
processfile(filename)
  File "counter-vertically-v2.py", line 29, in processfile
writeonefiledata(base+OUTFILEEXT,results)
  File "counter-vertically-v2.py", line 38, in writeonefiledata
for key, value in results.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'

I don't know how to fix above problem.

Thanks for your patience.

>
>
>
> Ramit
>
>
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
>
>
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread Prasad, Ramit
>>yes, you're iterating over the keys of a dictionary.  Since it only has the 
>>key "E", that's what you get.  Try printing dir(results) to see what methods 
>>might return something other than the key.  Make the language work for you.

>Sorry I am not smart.  value?

Dictionaries {} are containers for key/value based pairs like { key : value, 
another_key : value(can be same or repeated) }

For example: 
{'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0] (for key 
'B') and [2, 1, 4, 0, 1, 0] (for key 'E')

You can get the value of a dictionary by doing: value = dictionary[key]
You can set the value of a dictionary by doing: dictionary[key] = value

You will get an exception if you try to get a value for a key that is not in 
the dictionary.
>>> dictionary = {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
>>> dictionary['F']
KeyError: 'F'

You can iterate over dictionaries in a couple ways
1-
for key in dictionary: # also the same as 
  # for key in dictionary.keys()
value = dictionary[ key ]
2-
for key, value in dictionary.iteritems():
< do something here with >



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread Alan Gauld

On 05/10/11 13:46, lina wrote:


another question, you know in linux, when use TAB, can automatically
input something,
so in python3, are there some way they can intelligent give some hints
or fill the left.


What development tool are you using?
If you use IDLE, the standard IDE that comes with Python, you should 
find that hitting tab (or pausing briefly) in a file editor will bring 
up a pick list of options.


Similarly typing a function name and an open paren will give a tooltip
for the functions parameters.

IPython will do some similar things for you as a replacement for the 
standard Python interpreter (plus a bunch of extras).


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread lina


> I ask again.  What did results look like when you print it out.  I'm
> referring to the argument to writeonefiledata().
>
>> def writeonefiledata(outname,**results):
>>
> put the lines here:
>print ("results is: ", results)
>print("repr is:", repr(results))

 $ python3 counter-vertically-v2.py
results is: {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
repr is: {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}


>
>  outfile = open(outname,"w")
>> for item in results:
>> return outfile.write(item)
>>
>> This final part I made some mistakes?
>>
>>  yes, you're iterating over the keys of a dictionary.  Since it only has
> the key "E", that's what you get.  Try printing dir(results) to see what
> methods might return something other than the key.  Make the language work
> for you.

Sorry I am not smart.  value?

>
>
> DaveA
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread Dave Angel

On 10/05/2011 08:46 AM, lina wrote:

On Wed, Oct 5, 2011 at 8:21 PM, Dave Angel  wrote:




#these two are capitalized because they're intended to be constant
TOKENS = "BE"
LINESTOSKIP = 43
INFILEEXT = ".xpm"
OUTFILEEXT = ".txt"

def dofiles(topdirectory):
for filename in os.listdr(topdirectory):


Here your typo is listdir not listdr,

processfile(filename)

def processfile(infilename):
base, ext =os.path.splitext(fileName)


Here I changed the fileName to infilename

if ext == INFILEEXT:

text = fetchonefiledata(infilename)
numcolumns = len(text[0])
results = {}
for ch in TOKENS:

results[ch] = [0] * numcolumns
for line in text:
line = line.strip()

for col, ch in enumerate(line):
if ch in tokens:


Here I changed the tokens to TOKENS

results[ch][col] += 1

writeonefiledata(base+**OUTFILEEXT, results)


def fetchonefiledata(inname):
infile = open(inname)
text = infile.readlines()
return text[LINESTOSKIP:]

def writeonefiledata(outname):
outfile = open(outname, "w")
...process the results as appropriate...
(since you didn't tell us how multiple tokens were to be
displayed)

if __name__ == "__main__":
dofiles(".") #or get the top directory from the sys.argv variable,
which is set from command line.


You dissect the former one you suggested before into 4 functions.


a little question, why choose .ext? why the splitext is also ext here?



  Try the following, perhaps in the interpreter:

mytuple = ("one thing", "Another thing")
base, extension = mytuple

Now look and see what base and extension have for values.

Previously we just needed the second element of the splitext return value.
  This time we'll need both, so might as well put them in variables that have
  useful names.

Yes, thanks for reminding, I understand now.





import os.path


TOKENS="E"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
 for filename in os.listdir(topdirectory):
 processfile(filename)

def processfile(infilename):
 base, ext =os.path.splitext(infilename)
 if ext == INFILEEXT:
 text = fetchonefiledata(infilename)
 numcolumns=len(text[0])
 results={}
 for ch in TOKENS:

 results[ch] = [0]*numcolumns
 for line in text:
 line = line.strip()

 for col, ch in enumerate(line):
 if ch in TOKENS:
 results[ch][col]+=1
 writeonefiledata(base+**OUTFILEEXT,results)

def fetchonefiledata(inname):
 infile = open(inname)
 text = infile.readlines()
 return text[LINESTOSKIP:]

def writeonefiledata(outname,**results):
 outfile = open(outname,"w")
 for item in results:
 return outfile.write(item)


if __name__=="__main__":
 dofiles(".")

just the results is a bit unexpected.

  $ more try.txt
E

I might make a mistake in the writeonefiledata your left part.

  I'd be amazed if there weren't at least a couple of typos in my message.

  But this is where you sprinkle a couple of prints.  What did results look
like when you print it out?


Yes, you did keep some typos there.
The result is kind of weird? only E there.

I ask again.  What did results look like when you print it out.  I'm 
referring to the argument to writeonefiledata().

def writeonefiledata(outname,results):

put the lines here:
print ("results is: ", results)
print("repr is:", repr(results))


 outfile = open(outname,"w")
 for item in results:
 return outfile.write(item)

This final part I made some mistakes?

yes, you're iterating over the keys of a dictionary.  Since it only has 
the key "E", that's what you get.  Try printing dir(results) to see what 
methods might return something other than the key.  Make the language 
work for you.

I hope you'll find that results is a dictionary, you might not want to just
write() its keys.  You probably want to write() its values instead, perhaps
with a heading showing what key you're printing.

Later I wish to get the value of B+E, the two tokens. so the final results
of each columns is enough. I will use this data to proceed further in
future.

the code to get multiple keys is already there.  Only reason you're 
getting only E is that you only specified one token. Try changing it to


TOKENS = "EA"


  But it gives you a simple refactoring that splits the logic so each can be

visualized (and tested) independently.  i'd also split up processfile(),
once I realized how big it was.

There are many shortcuts that can be applied. Some of them probably use
language features you're not comfortable with, like perhaps generators.
  And
if  efficiency is important, there are optimizations to do, like using
islice directly on the infile object.  That one would eliminate having to
have the whole file stored in memory at one time.

Like

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread lina
On Wed, Oct 5, 2011 at 8:21 PM, Dave Angel  wrote:

>
>
>>
>>> #these two are capitalized because they're intended to be constant
>>> TOKENS = "BE"
>>> LINESTOSKIP = 43
>>> INFILEEXT = ".xpm"
>>> OUTFILEEXT = ".txt"
>>>
>>> def dofiles(topdirectory):
>>>for filename in os.listdr(topdirectory):
>>>
>> Here your typo is listdir not listdr,

>processfile(filename)
>>>
>>> def processfile(infilename):
>>>base, ext =os.path.splitext(fileName)
>>>
>> Here I changed the fileName to infilename

>if ext == INFILEEXT:
>>>text = fetchonefiledata(infilename)
>>>numcolumns = len(text[0])
>>>results = {}
>>>for ch in TOKENS:
>>>
>>>results[ch] = [0] * numcolumns
>>>for line in text:
>>>line = line.strip()
>>>
>>>for col, ch in enumerate(line):
>>>if ch in tokens:
>>>
>> Here I changed the tokens to TOKENS

>results[ch][col] += 1
>>>writeonefiledata(base+**OUTFILEEXT, results)
>>>
>>>
>>> def fetchonefiledata(inname):
>>>infile = open(inname)
>>>text = infile.readlines()
>>>return text[LINESTOSKIP:]
>>>
>>> def writeonefiledata(outname):
>>>outfile = open(outname, "w")
>>>...process the results as appropriate...
>>>(since you didn't tell us how multiple tokens were to be
>>> displayed)
>>>
>>> if __name__ == "__main__":
>>>dofiles(".") #or get the top directory from the sys.argv variable,
>>> which is set from command line.
>>>
>>>
>>> You dissect the former one you suggested before into 4 functions.
>>>
>>
>> a little question, why choose .ext? why the splitext is also ext here?
>>
>>
>>
>>  Try the following, perhaps in the interpreter:
>
> mytuple = ("one thing", "Another thing")
> base, extension = mytuple
>
> Now look and see what base and extension have for values.
>
> Previously we just needed the second element of the splitext return value.
>  This time we'll need both, so might as well put them in variables that have
>  useful names.

Yes, thanks for reminding, I understand now.

>
>
>
>>
>> import os.path
>>
>>
>> TOKENS="E"
>> LINESTOSKIP=0
>> INFILEEXT=".xpm"
>> OUTFILEEXT=".txt"
>>
>> def dofiles(topdirectory):
>> for filename in os.listdir(topdirectory):
>> processfile(filename)
>>
>> def processfile(infilename):
>> base, ext =os.path.splitext(infilename)
>> if ext == INFILEEXT:
>> text = fetchonefiledata(infilename)
>> numcolumns=len(text[0])
>> results={}
>> for ch in TOKENS:
>>
>> results[ch] = [0]*numcolumns
>> for line in text:
>> line = line.strip()
>>
>> for col, ch in enumerate(line):
>> if ch in TOKENS:
>> results[ch][col]+=1
>> writeonefiledata(base+**OUTFILEEXT,results)
>>
>> def fetchonefiledata(inname):
>> infile = open(inname)
>> text = infile.readlines()
>> return text[LINESTOSKIP:]
>>
>> def writeonefiledata(outname,**results):
>> outfile = open(outname,"w")
>> for item in results:
>> return outfile.write(item)
>>
>>
>> if __name__=="__main__":
>> dofiles(".")
>>
>> just the results is a bit unexpected.
>>
>>  $ more try.txt
>> E
>>
>> I might make a mistake in the writeonefiledata your left part.
>>
>>  I'd be amazed if there weren't at least a couple of typos in my message.
>  But this is where you sprinkle a couple of prints.  What did results look
> like when you print it out?
>
Yes, you did keep some typos there.
The result is kind of weird? only E there.

def writeonefiledata(outname,results):
outfile = open(outname,"w")
for item in results:
return outfile.write(item)

This final part I made some mistakes?

>
> I hope you'll find that results is a dictionary, you might not want to just
> write() its keys.  You probably want to write() its values instead, perhaps
> with a heading showing what key you're printing.

Later I wish to get the value of B+E, the two tokens. so the final results
of each columns is enough. I will use this data to proceed further in
future.

>
>
>  But it gives you a simple refactoring that splits the logic so each can be
>>
>>> visualized (and tested) independently.  i'd also split up processfile(),
>>> once I realized how big it was.
>>>
>>> There are many shortcuts that can be applied. Some of them probably use
>>> language features you're not comfortable with, like perhaps generators.
>>>  And
>>> if  efficiency is important, there are optimizations to do, like using
>>> islice directly on the infile object.  That one would eliminate having to
>>> have the whole file stored in memory at one time.
>>>
>>> Likewise there are further things that could be done to decouple the
>>> functions even more.
>>>
>>> But there's nothing in the above code which uses very advanced topics, so
>>> you should be able to understand it and fix whatever typos I've
>>> undoubtedly
>>> got.
>>>
>>> What are you using for deb

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-05 Thread Dave Angel

On 10/05/2011 02:51 AM, lina wrote:

On Wed, Oct 5, 2011 at 1:42 PM, Dave Angel  wrote:


On 10/04/2011 11:13 PM, lina wrote:


On Wed, Oct 5, 2011 at 10:45 AM, Dave Angel   wrote:

  On 10/04/2011 10:22 PM, lina wrote:

  On Wed, Oct 5, 2011 at 1:30 AM, Prasad, Ramit
*com


w



SyntaxError: invalid syntax

for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=

=".xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 cols = len(text[0])
 except IndexError:
 print ("Index Error.")
 result=[]
 for idx in xrange(cols):
 results.append(0)
 for line in text:
 for col_idx, field in enumerate(line):
 if token in field:
 results[col_idx]+=1
 for index in col_idx:
 print results[index]

it showed up:

 print results[]
 ^
SyntaxError: invalid syntax

Sorry, I am still lack deep understanding about something basic. Thanks
for
your patience.


  Simplest answer here is you might have accidentally run this under
Python


3.x.  That would explain the syntax error on the print function.   Pick a
single version and stick to it.  In fact, you might even put a version
test
at the beginning of the code to give an immediate error.

  choose python3.

  Then change that last print to use parentheses.  print() is a function

call in Python 3.x, while it was a statement in earlier Python versions.

  

  This example illustrates one reason why it's a mistake to write all the

code at top level.  This code should probably be at least 4 functions,
with
each one handling one abstraction.

  It's frustrating. Seriously. (I think I need to read some good

(relevant)
codes first.

  Is Python your first programming language?  It was approximately my 30th.

Not exactly. Ha ... I don't know there are so many languages there.


I learned "programming" from a Fortran book in 1967.  I had no access to a
computer, though there was at least one in the state, at the Yale campus.  I
saw it in a field trip by the (advanced) students that were taking
programming.  They weren't allowed to take it till finishing 2nd year
calculus, which I didn't do till I got to college.  However, when I went to
college the following year, I ran across another student who knew how to
access the mainframe (via punch-cards), and could tell me how to do it.
  (Security was very light).  For a few months, I hacked daily, and learned a
lot.  Then the following year, I actually took an electrical engineering
class that introduced the concepts of programming, and I spent my time doing
experiments that barely resembled the assignments.  I ended up with an
incomplete in the course, which I made up by writing a linear circuit
analysis program.  Punched card input, graphical output to a line printer
using rows of asterisks.


How to start, I learned C 10 years ago, but for whole semester, I never
wrote a serious program, but indeed attended every lecture.
At that time, I was addicted literature staff. But later realized that lots
of writers (especially the ones I like)  ended up with committing suicide,
something to heavy to handle, so I changed to something like physics, I
noted lots of people doing physics living really long and happy (long living
the physicist), then four years as (applied) physics, three years as
(theoretical) physics, then (bio-) physics in the following years. (It's a
joke).
During those years used maple, matlab and some basic awk, bash. but all is
very basic. shame...did not do something seriously.


Point is, it takes a lot of time, and usually a one-on-one mentor to get
the concepts nailed down.  Seldom did anyone tell me "write these lines
down, and it'll solve the problem."  instead they told me where my problem
was, and where in those manuals (chained to tables in the lab) to find more
information.

It wasn't till my fourth language that I found out about local variables,
and how a function should encapsulate one concept.  The first three didn't
have such things.



  Further, while you're developing, you should probably put the test data

into a literal (probably a multiline literal using triplequotes), so you
can
experiment easily with changes to the data, and see how it results.



  #!/bin/python

import os.path

tokens=['B','E']

for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=**
=".xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 results={}
 numcolumns=len(text.strip())
 for ch in tokens:
 results[ch]=[0]*numcolumns
 for line in text:
 for col, ch in enumerate(line):
 if ch in tokens:
 results[ch][col]+=1
 for item in results:
 print item

$ python3 counter-vertically.py
   File "counter-vertically.py", line 20
 

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread lina
On Wed, Oct 5, 2011 at 1:42 PM, Dave Angel  wrote:

> On 10/04/2011 11:13 PM, lina wrote:
>
>> On Wed, Oct 5, 2011 at 10:45 AM, Dave Angel  wrote:
>>
>>  On 10/04/2011 10:22 PM, lina wrote:
>>>
>>>  On Wed, Oct 5, 2011 at 1:30 AM, Prasad, Ramit>>> *com

> w
>
 
>>>
 SyntaxError: invalid syntax

 for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=

 =".xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 cols = len(text[0])
 except IndexError:
 print ("Index Error.")
 result=[]
 for idx in xrange(cols):
 results.append(0)
 for line in text:
 for col_idx, field in enumerate(line):
 if token in field:
 results[col_idx]+=1
 for index in col_idx:
 print results[index]

 it showed up:

 print results[]
 ^
 SyntaxError: invalid syntax

 Sorry, I am still lack deep understanding about something basic. Thanks
 for
 your patience.


  Simplest answer here is you might have accidentally run this under
 Python

>>> 3.x.  That would explain the syntax error on the print function.   Pick a
>>> single version and stick to it.  In fact, you might even put a version
>>> test
>>> at the beginning of the code to give an immediate error.
>>>
>>>  choose python3.
>>
>>  Then change that last print to use parentheses.  print() is a function
> call in Python 3.x, while it was a statement in earlier Python versions.
>
>  
>>
>>  This example illustrates one reason why it's a mistake to write all the
>>> code at top level.  This code should probably be at least 4 functions,
>>> with
>>> each one handling one abstraction.
>>>
>>>  It's frustrating. Seriously. (I think I need to read some good
>> (relevant)
>> codes first.
>>
>>  Is Python your first programming language?  It was approximately my 30th.
>
Not exactly. Ha ... I don't know there are so many languages there.

>
> I learned "programming" from a Fortran book in 1967.  I had no access to a
> computer, though there was at least one in the state, at the Yale campus.  I
> saw it in a field trip by the (advanced) students that were taking
> programming.  They weren't allowed to take it till finishing 2nd year
> calculus, which I didn't do till I got to college.  However, when I went to
> college the following year, I ran across another student who knew how to
> access the mainframe (via punch-cards), and could tell me how to do it.
>  (Security was very light).  For a few months, I hacked daily, and learned a
> lot.  Then the following year, I actually took an electrical engineering
> class that introduced the concepts of programming, and I spent my time doing
> experiments that barely resembled the assignments.  I ended up with an
> incomplete in the course, which I made up by writing a linear circuit
> analysis program.  Punched card input, graphical output to a line printer
> using rows of asterisks.
>
How to start, I learned C 10 years ago, but for whole semester, I never
wrote a serious program, but indeed attended every lecture.
At that time, I was addicted literature staff. But later realized that lots
of writers (especially the ones I like)  ended up with committing suicide,
something to heavy to handle, so I changed to something like physics, I
noted lots of people doing physics living really long and happy (long living
the physicist), then four years as (applied) physics, three years as
(theoretical) physics, then (bio-) physics in the following years. (It's a
joke).
During those years used maple, matlab and some basic awk, bash. but all is
very basic. shame...did not do something seriously.

>
> Point is, it takes a lot of time, and usually a one-on-one mentor to get
> the concepts nailed down.  Seldom did anyone tell me "write these lines
> down, and it'll solve the problem."  instead they told me where my problem
> was, and where in those manuals (chained to tables in the lab) to find more
> information.
>
> It wasn't till my fourth language that I found out about local variables,
> and how a function should encapsulate one concept.  The first three didn't
> have such things.
>
>
>
>  Further, while you're developing, you should probably put the test data
>>> into a literal (probably a multiline literal using triplequotes), so you
>>> can
>>> experiment easily with changes to the data, and see how it results.
>>>
>>>
>>  #!/bin/python
>>
>> import os.path
>>
>> tokens=['B','E']
>>
>> for fileName in os.listdir("."):
>> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=**
>> =".xpm":
>> filedata = open(fileName)
>> text=filedata.readlines()
>> results={}
>> numcolumns=len(text.strip())
>> for ch in tokens:
>> res

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Andreas Perstinger

On 2011-10-05 05:13, lina wrote:

$ python3 counter-vertically.py

^^^

   File "counter-vertically.py", line 20
 print item
  ^
SyntaxError: invalid syntax


In Python 3 print is a function: print(item)

In another message in this thread you've said:
"Sorry, I am still lack deep understanding about something basic."

IMHO you should take the time to read the tutorial (as others have 
already told you) before continuing your work:

http://docs.python.org/py3k/tutorial/index.html

Bye, Andreas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Dave Angel

On 10/04/2011 11:13 PM, lina wrote:

On Wed, Oct 5, 2011 at 10:45 AM, Dave Angel  wrote:


On 10/04/2011 10:22 PM, lina wrote:


On Wed, Oct 5, 2011 at 1:30 AM, Prasad, 
Ramit

w



SyntaxError: invalid syntax

for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=**
=".xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 cols = len(text[0])
 except IndexError:
 print ("Index Error.")
 result=[]
 for idx in xrange(cols):
 results.append(0)
 for line in text:
 for col_idx, field in enumerate(line):
 if token in field:
 results[col_idx]+=1
 for index in col_idx:
 print results[index]

it showed up:

 print results[]
 ^
SyntaxError: invalid syntax

Sorry, I am still lack deep understanding about something basic. Thanks
for
your patience.


  Simplest answer here is you might have accidentally run this under Python

3.x.  That would explain the syntax error on the print function.   Pick a
single version and stick to it.  In fact, you might even put a version test
at the beginning of the code to give an immediate error.


choose python3.

Then change that last print to use parentheses.  print() is a function 
call in Python 3.x, while it was a statement in earlier Python versions.





This example illustrates one reason why it's a mistake to write all the
code at top level.  This code should probably be at least 4 functions, with
each one handling one abstraction.


It's frustrating. Seriously. (I think I need to read some good (relevant)
codes first.


Is Python your first programming language?  It was approximately my 30th.

I learned "programming" from a Fortran book in 1967.  I had no access to 
a computer, though there was at least one in the state, at the Yale 
campus.  I saw it in a field trip by the (advanced) students that were 
taking programming.  They weren't allowed to take it till finishing 2nd 
year calculus, which I didn't do till I got to college.  However, when I 
went to college the following year, I ran across another student who 
knew how to access the mainframe (via punch-cards), and could tell me 
how to do it.  (Security was very light).  For a few months, I hacked 
daily, and learned a lot.  Then the following year, I actually took an 
electrical engineering class that introduced the concepts of 
programming, and I spent my time doing experiments that barely resembled 
the assignments.  I ended up with an incomplete in the course, which I 
made up by writing a linear circuit analysis program.  Punched card 
input, graphical output to a line printer using rows of asterisks.


Point is, it takes a lot of time, and usually a one-on-one mentor to get 
the concepts nailed down.  Seldom did anyone tell me "write these lines 
down, and it'll solve the problem."  instead they told me where my 
problem was, and where in those manuals (chained to tables in the lab) 
to find more information.


It wasn't till my fourth language that I found out about local 
variables, and how a function should encapsulate one concept.  The first 
three didn't have such things.




Further, while you're developing, you should probably put the test data
into a literal (probably a multiline literal using triplequotes), so you can
experiment easily with changes to the data, and see how it results.



  #!/bin/python

import os.path

tokens=['B','E']

for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 results={}
 numcolumns=len(text.strip())
 for ch in tokens:
 results[ch]=[0]*numcolumns
 for line in text:
 for col, ch in enumerate(line):
 if ch in tokens:
 results[ch][col]+=1
 for item in results:
 print item

$ python3 counter-vertically.py
   File "counter-vertically.py", line 20
 print item
  ^
SyntaxError: invalid syntax


As I said above, Python 3 needs parentheses around print's argument list.

As for splitting into functions, consider:

#these two are capitalized because they're intended to be constant
TOKENS = "BE"
LINESTOSKIP = 43
INFILEEXT = ".xpm"
OUTFILEEXT = ".txt"

def dofiles(topdirectory):
for filename in os.listdr(topdirectory):
processfile(filename)

def processfile(infilename):
base, ext =os.path.splitext(fileName)
if ext == INFILEEXT:
text = fetchonefiledata(infilename)
numcolumns = len(text[0])
results = {}
for ch in TOKENS:
results[ch] = [0] * numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in tokens:
results[ch][col] += 1
writeonefileda

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread lina
On Wed, Oct 5, 2011 at 10:45 AM, Dave Angel  wrote:

> On 10/04/2011 10:22 PM, lina wrote:
>
>> On Wed, Oct 5, 2011 at 1:30 AM, Prasad, 
>> Ramit
>> >wrote:
>>
>>  But I still don't know how to get the
 statistic result of each column,

>>> Thanks.
>>> try:
>>>cols = len( text[0] ) # Find out how many columns there are (assuming
>>> each row has the same number of columns)
>>> except IndexError:
>>>raise #  This will make sure you can see the error while developing;
>>>
>>>  This part:
>>
>> It's showed up:
>>
>> except IndexError:
>>
> Best guess I can make is that the line "each row has..."  needs a # in
> front of it.  or maybe your code looks like the following, which has no try
> block at all.
>
> The except clause has to be the first line at the same indentation as the
> try line it's protecting.
>
>
>   ^
>> SyntaxError: invalid syntax
>>
>> for fileName in os.listdir("."):
>> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=**
>> =".xpm":
>> filedata = open(fileName)
>> text=filedata.readlines()
>> cols = len(text[0])
>> except IndexError:
>> print ("Index Error.")
>> result=[]
>> for idx in xrange(cols):
>> results.append(0)
>> for line in text:
>> for col_idx, field in enumerate(line):
>> if token in field:
>> results[col_idx]+=1
>> for index in col_idx:
>> print results[index]
>>
>> it showed up:
>>
>> print results[]
>> ^
>> SyntaxError: invalid syntax
>>
>> Sorry, I am still lack deep understanding about something basic. Thanks
>> for
>> your patience.
>>
>>
>>  Simplest answer here is you might have accidentally run this under Python
> 3.x.  That would explain the syntax error on the print function.   Pick a
> single version and stick to it.  In fact, you might even put a version test
> at the beginning of the code to give an immediate error.
>
choose python3.

>
> But you do have many other problems with the code.  One is that this no
> longer does anything useful with multiple tokens.  (See my last email to see
> an approach that handles multiple tokens).  Another is that you mix result
> and results.  They're entirely distinct.  So pick one spelling and stick to
> it.

This is a very good suggestions, I choose results.

> Another is that for the "for index" is indented wrong, and uses the wrong
> limit value.  As it stands, it's trying to iterate over an integer.  You
> probably want to replace the whole phrase with something like for item in
> results: print item
>
> This example illustrates one reason why it's a mistake to write all the
> code at top level.  This code should probably be at least 4 functions, with
> each one handling one abstraction.
>
It's frustrating. Seriously. (I think I need to read some good (relevant)
codes first.

>
> Further, while you're developing, you should probably put the test data
> into a literal (probably a multiline literal using triplequotes), so you can
> experiment easily with changes to the data, and see how it results.
>


 #!/bin/python

import os.path

tokens=['B','E']

for fileName in os.listdir("."):
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
results={}
numcolumns=len(text.strip())
for ch in tokens:
results[ch]=[0]*numcolumns
for line in text:
for col, ch in enumerate(line):
if ch in tokens:
results[ch][col]+=1
for item in results:
print item

$ python3 counter-vertically.py
  File "counter-vertically.py", line 20
print item
 ^
SyntaxError: invalid syntax

>
> --
>
> DaveA
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Dave Angel

On 10/04/2011 10:22 PM, lina wrote:

On Wed, Oct 5, 2011 at 1:30 AM, Prasad, Ramitwrote:


But I still don't know how to get the
statistic result of each column,

Thanks.
try:
cols = len( text[0] ) # Find out how many columns there are (assuming
each row has the same number of columns)
except IndexError:
raise #  This will make sure you can see the error while developing;


This part:

It's showed up:

 except IndexError:
Best guess I can make is that the line "each row has..."  needs a # in 
front of it.  or maybe your code looks like the following, which has no 
try block at all.


The except clause has to be the first line at the same indentation as 
the try line it's protecting.



  ^
SyntaxError: invalid syntax

for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 cols = len(text[0])
 except IndexError:
 print ("Index Error.")
 result=[]
 for idx in xrange(cols):
 results.append(0)
 for line in text:
 for col_idx, field in enumerate(line):
 if token in field:
 results[col_idx]+=1
 for index in col_idx:
 print results[index]

it showed up:

 print results[]
 ^
SyntaxError: invalid syntax

Sorry, I am still lack deep understanding about something basic. Thanks for
your patience.


Simplest answer here is you might have accidentally run this under 
Python 3.x.  That would explain the syntax error on the print 
function.   Pick a single version and stick to it.  In fact, you might 
even put a version test at the beginning of the code to give an 
immediate error.


But you do have many other problems with the code.  One is that this no 
longer does anything useful with multiple tokens.  (See my last email to 
see an approach that handles multiple tokens).  Another is that you mix 
result and results.  They're entirely distinct.  So pick one spelling 
and stick to it.  Another is that for the "for index" is indented wrong, 
and uses the wrong limit value.  As it stands, it's trying to iterate 
over an integer.  You probably want to replace the whole phrase with 
something like for item in results: print item


This example illustrates one reason why it's a mistake to write all the 
code at top level.  This code should probably be at least 4 functions, 
with each one handling one abstraction.


Further, while you're developing, you should probably put the test data 
into a literal (probably a multiline literal using triplequotes), so you 
can experiment easily with changes to the data, and see how it results.


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread lina
On Wed, Oct 5, 2011 at 1:30 AM, Prasad, Ramit wrote:

> >But I still don't know how to get the
> >statistic result of each column,
>
> Thanks.

>
> try:
>cols = len( text[0] ) # Find out how many columns there are (assuming
> each row has the same number of columns)
> except IndexError:
>raise #  This will make sure you can see the error while developing;
>
This part:

It's showed up:

except IndexError:
 ^
SyntaxError: invalid syntax

for fileName in os.listdir("."):
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
cols = len(text[0])
except IndexError:
print ("Index Error.")
result=[]
for idx in xrange(cols):
results.append(0)
for line in text:
for col_idx, field in enumerate(line):
if token in field:
results[col_idx]+=1
for index in col_idx:
print results[index]

it showed up:

print results[]
^
SyntaxError: invalid syntax

Sorry, I am still lack deep understanding about something basic. Thanks for
your patience.



>  #  replace with whatever is appropriate for your application
> results = []
> for idx in xrange( cols ):
>results.append( 0 ) # Initialize an array to zero value with the correct
> number of columns
># results = [ 0, 0, 0 ]  for 3 columns
> for line in text: # Check each row
>for col_idx, field in enumerate( line ): # check each column
>if token in field:
># Or possibly if token==field, not sure exactly what kind of
> comparison you need.
>results[col_idx] += 1 # token found so increment count for that
> column
>
>
> This is a simple to understand, brute-force solution. It is not very
> efficient and might be slow for large amounts of data.
>
> Ramit
>
>
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
>
>
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Alan Gauld

On 04/10/11 18:09, lina wrote:

But I still don't know how to get the
statistic result of each column,


Let's ignore the traversal of files issue for now.
Let's look at processing one file:

You want a list of totals of equal length to the number of columns.
We can find that number using len(line.split())

And we can initialise a list to that many elements with:

counts = [0] * numCols

Now you want to iterate over each line and for each column
increment the corresponding count if the value is equal
to your token ('E' in the example).

Lets try putting that together:

def countTokenInColumn(aFileName,token):

lines = open(aFileName).readlines()
numCols = len( lines[0].split() ) #use 1st line as a template
counts = [0] * numCols

for line in lines:
cols = line.split()
for index in range(numCols):
if cols[index] == token:
   counts[index] += 1
return counts


Now that is untested but should be close to what you want.
We could tighten it up a bit but I've tried to keep the
algorithm clear.

Does that help?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Alan Gauld

On 04/10/11 17:38, lina wrote:

For file:

aaEbb
aEEbb
EaEbb
EaEbE

the expected output is

2 1 0 1


I expected

2 1 4 0 1


In the first column there is 2 E, so the output is 2 E, second column is
1 E.


But the 3rd is all E so why miss out the 4?

I'm still confused - and I thought I was getting it!

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Dave Angel

On 10/04/2011 01:09 PM, lina wrote:

But I still don't know how to get the
statistic result of each column,

Thanks for further suggestions,

Best regards,

lina

As I said before, your current code counts across a line at a time, 
while you need to count vertically.  That could be done by transposing 
the list of strings, but it's probably easier to just count them 
yourself.  So set up a list of counters, all zero, and every time you 
see a desired character, increment the corresponding counter.


Here's the existing code:

#!/bin/python

import os.path

tokens=['B','E']

for fileName in os.listdir("."):
result=[]
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
for line in text[0:]:
result.append({t:line.strip().count(t) for t in tokens})
for index,r in enumerate(result):
   outfiledata=open("fileName.txt","w")


Decide first on a reasonable data arrangement for the results.   I'd suggest 
having a map of lists, rather than the other way around.

So initialize it to having zeroes in all the right places.

results = {}
numcolumns = len(text[23].strip())
for ch in tokens:
results[ch] = [0] * numcolumns


and

for line in  text[23:]:
for col, ch in enumerate(line):
if ch in tokens:
   results[ch][col] += 1


(Untested)


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Prasad, Ramit
>But I still don't know how to get the 
>statistic result of each column,


try:
cols = len( text[0] ) # Find out how many columns there are (assuming each 
row has the same number of columns)
except IndexError:
raise #  This will make sure you can see the error while developing; 
  #  replace with whatever is appropriate for your application
results = []
for idx in xrange( cols ):
results.append( 0 ) # Initialize an array to zero value with the correct 
number of columns
# results = [ 0, 0, 0 ]  for 3 columns
for line in text: # Check each row
for col_idx, field in enumerate( line ): # check each column
if token in field: 
# Or possibly if token==field, not sure exactly what kind of 
comparison you need.
results[col_idx] += 1 # token found so increment count for that 
column


This is a simple to understand, brute-force solution. It is not very efficient 
and might be slow for large amounts of data. 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread lina
But I still don't know how to get the
statistic result of each column,

Thanks for further suggestions,

Best regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread lina
On Wed, Oct 5, 2011 at 12:59 AM, Marc Tompkins wrote:

> On Tue, Oct 4, 2011 at 8:48 AM, lina  wrote:
>
>> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
>> filedata = open(fileName)
>> text=filedata.readlines()
>> for line in text[0:]:
>> result.append({t:line.strip().count(t) for t in tokens})
>> for index,r in enumerate(result):
>>outfiledata=open("fileName.txt","w").write(index,"-",r)
>> I still have problem using the value of the fileName,
>>
>> here the output is fileName.txt, not $fileName.txt which is supposed to be
>> 1.txt following input 1.xpm
>>
>
> That's because in this line:
>
>>outfiledata=open("fileName.txt","w").write(index,"-",r)
>>
> you put "fileName.txt" in quotes - which makes it a literal, not a
> variable.
>
> Try this:
>
>>outfiledata=open(os.path.splitext(fileName)[0] +
>> ".txt","w").write(index,"-",r)
>>
> Thanks, it works.  I understand it now.
> (in other words, split fileName, take just the first part, and add ".txt"
> to the end of it.)


> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Marc Tompkins
On Tue, Oct 4, 2011 at 8:48 AM, lina  wrote:

> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
> filedata = open(fileName)
> text=filedata.readlines()
> for line in text[0:]:
> result.append({t:line.strip().count(t) for t in tokens})
> for index,r in enumerate(result):
>outfiledata=open("fileName.txt","w").write(index,"-",r)
> I still have problem using the value of the fileName,
>
> here the output is fileName.txt, not $fileName.txt which is supposed to be
> 1.txt following input 1.xpm
>

That's because in this line:

>outfiledata=open("fileName.txt","w").write(index,"-",r)
>
you put "fileName.txt" in quotes - which makes it a literal, not a variable.


Try this:

>outfiledata=open(os.path.splitext(fileName)[0] +
> ".txt","w").write(index,"-",r)
>

(in other words, split fileName, take just the first part, and add ".txt" to
the end of it.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread lina
For file:

aaEbb
aEEbb
EaEbb
EaEbE

the expected output is

2 1 0 1

In the first column there is 2 E, so the output is 2 E, second column is 1
E.


#!/bin/python

import os.path

tokens=['B','E']

for fileName in os.listdir("."):
result=[]
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
for line in text[0:]:
result.append({t:line.strip().count(t) for t in tokens})
for index,r in enumerate(result):
   outfiledata=open("fileName.txt","w")

Thanks,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread Dave Angel

On 10/04/2011 10:26 AM, lina wrote:

On Thu, Sep 29, 2011 at 11:28 PM, Dave Angel  wrote:


(Please don't top-post.  Put your remarks AFTER the part you're quoting
from the previous message)


On 09/29/2011 10:55 AM, lina wrote:


import os.path

tokens=['E']
result=[]

for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)=="**xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 for line in text:


How can I read from line 24 and do further looking for "E".

Thanks,




As I said in my earlier message, this was untested.  It gave you the
building blocks, but was not correct.

In particular, that if-test will always fail, so you're not seeing any
files.


import os.path

tokens=['E']
result=[]

for fileName in os.listdir("."):

if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=**
=".xpm":

filedata = open(fileName)
text=filedata.readlines()
for line in text:
print line


Once you've tested that, then you're ready to just look at line 24.

text is a list, so you can refer to line 24 as text[24]

Or you can get lines 24-28, with  text[24, 29]   (look up slices in the
Python doc)

==
DaveA


Thanks for former help,

but I wonder how to output (write) the final result in each respectively
fileName with just different extension, such as original a.xpm write to
a.txt

Thanks,

#!/bin/python

import os.path

tokens=['E']
result=[]

for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 for line in text[23:len(text)-1]:
 result.append({t:line.count(t) for t in tokens})
 for index,r in enumerate(result):
 fileName.txt.write(index,"-",r)
???



 for line in text[23:len(text)-1]:

probably doesn't do what you expect.  It'll start at the 24th line, but 
it won't include the last line. slicing uses half-open intervals, same 
as range.  So you don't want the -1 on that line.


Fortunately, all you have to do is use the default second parm,

 for line in text[23:]:

Now I have no idea why you want such a complex structure in result, but 
I'll ignore that for the moment.
You want to know how to write an output file. Just like an input file, 
you first have to open it (in 'w' mode).

  outfile = open(newfilename,  "w")
will give you a file object, just like filedata did for the input file.
So you would then do outfile.write(somedata)  as needed.  Notice that if 
you use write(), it does NOT put newlines in.  That's up to you.


Note also that opening a file with "w" deletes an existing file of the 
same name.  So you want to thoroughly test your transformation code 
before running the actual command.


Now back to your result.append line.  As it stands now, result contains 
the results for all the files you've processed so far.  In other words, 
as you process multiple files, it'll get larger and larger.  If you're 
writing the data out to multiple files, that isn't likely what you want.


But I also thought you wanted to count the occurrences of  each token by 
column, and you're counting them by rows.  That count method will return 
how many are in that particular line.


Perhaps this would be clearer if your data wasn't square.  if you had 10 
lines with 3 characters in each, it might be more obvious.  I assume you 
would then want 3 result counts.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread lina
On Tue, Oct 4, 2011 at 11:27 PM, Dave Angel  wrote:

> On 10/04/2011 10:26 AM, lina wrote:
>
>> On Thu, Sep 29, 2011 at 11:28 PM, Dave Angel  wrote:
>>
>>  (Please don't top-post.  Put your remarks AFTER the part you're quoting
>>> from the previous message)
>>>
>>>
>>> On 09/29/2011 10:55 AM, lina wrote:
>>>
>>>  import os.path

 tokens=['E']
 result=[]

 for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)=="***
 *xpm":

 filedata = open(fileName)
 text=filedata.readlines()
 for line in text:


 How can I read from line 24 and do further looking for "E".

 Thanks,



  As I said in my earlier message, this was untested.  It gave you the
>>> building blocks, but was not correct.
>>>
>>> In particular, that if-test will always fail, so you're not seeing any
>>> files.
>>>
>>>
>>> import os.path
>>>
>>> tokens=['E']
>>> result=[]
>>>
>>> for fileName in os.listdir("."):
>>>
>>>if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=
>>>
>>> =".xpm":
>>>
>>>filedata = open(fileName)
>>>text=filedata.readlines()
>>>for line in text:
>>>print line
>>>
>>>
>>> Once you've tested that, then you're ready to just look at line 24.
>>>
>>> text is a list, so you can refer to line 24 as text[24]
>>>
>>> Or you can get lines 24-28, with  text[24, 29]   (look up slices in the
>>> Python doc)
>>>
>>> ==
>>> DaveA
>>>
>>>
>>> Thanks for former help,
>>>
>> but I wonder how to output (write) the final result in each respectively
>> fileName with just different extension, such as original a.xpm write to
>> a.txt
>>
>> Thanks,
>>
>> #!/bin/python
>>
>> import os.path
>>
>> tokens=['E']
>> result=[]
>>
>> for fileName in os.listdir("."):
>> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=**
>> =".xpm":
>> filedata = open(fileName)
>> text=filedata.readlines()
>> for line in text[23:len(text)-1]:
>> result.append({t:line.count(t) for t in tokens})
>> for index,r in enumerate(result):
>> fileName.txt.write(index,"**-",r)
>> ???
>>
>>
>  for line in text[23:len(text)-1]:
>
> probably doesn't do what you expect.  It'll start at the 24th line, but it
> won't include the last line. slicing uses half-open intervals, same as
> range.  So you don't want the -1 on that line.
>

Yes. It starts from 24th line, the first 23 were irrelevant here for
analysis. I made a mistake, it should be text[23:len(text)]

>
> Fortunately, all you have to do is use the default second parm,
>
>  for line in text[23:]:
>
> Now I have no idea why you want such a complex structure in result, but
> I'll ignore that for the moment.
> You want to know how to write an output file. Just like an input file, you
> first have to open it (in 'w' mode).
>  outfile = open(newfilename,  "w")
> will give you a file object, just like filedata did for the input file.
> So you would then do outfile.write(somedata)  as needed.  Notice that if
> you use write(), it does NOT put newlines in.  That's up to you.
>
> Note also that opening a file with "w" deletes an existing file of the same
> name.  So you want to thoroughly test your transformation code before
> running the actual command.
>
Yes,

for fileName in os.listdir("."):
result=[]
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
for line in text[0:]:
result.append({t:line.strip().count(t) for t in tokens})
for index,r in enumerate(result):
   outfiledata=open("fileName.txt","w").write(index,"-",r)
I still have problem using the value of the fileName,

here the output is fileName.txt, not $fileName.txt which is supposed to be
1.txt following input 1.xpm


>
> Now back to your result.append line.  As it stands now, result contains the
> results for all the files you've processed so far.  In other words, as you
> process multiple files, it'll get larger and larger.  If you're writing the
> data out to multiple files, that isn't likely what you want.
>
Thanks for reminding, now I put result=[] in the for loop for files.

>
> But I also thought you wanted to count the occurrences of  each token by
> column, and you're counting them by rows.  That count method will return how
> many are in that particular line.
>
Yes, but indeed I calculated it in each row. big mistake.
can you tell me how to achieve this, transpose?

>
> Perhaps this would be clearer if your data wasn't square.  if you had 10
> lines with 3 characters in each, it might be more obvious.  I assume you
> would then want 3 result counts.
>
It's n*n matrix. now I used this one as example:
aaEbb
aEEbb
EaEbb
EaEbE

Thanks ahead for any further suggestions,

>
> DaveA
>
>


-- 
Best Regards,

lina
___
Tutor ma

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-04 Thread lina
On Thu, Sep 29, 2011 at 11:28 PM, Dave Angel  wrote:

> (Please don't top-post.  Put your remarks AFTER the part you're quoting
> from the previous message)
>
>
> On 09/29/2011 10:55 AM, lina wrote:
>
>> import os.path
>>
>> tokens=['E']
>> result=[]
>>
>> for fileName in os.listdir("."):
>> if os.path.isfile(fileName) and os.path.splitext(fileName)=="**xpm":
>> filedata = open(fileName)
>> text=filedata.readlines()
>> for line in text:
>>
>>
>> How can I read from line 24 and do further looking for "E".
>>
>> Thanks,
>>
>>
>>
> As I said in my earlier message, this was untested.  It gave you the
> building blocks, but was not correct.
>
> In particular, that if-test will always fail, so you're not seeing any
> files.
>
>
> import os.path
>
> tokens=['E']
> result=[]
>
> for fileName in os.listdir("."):
>
>if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=**
> =".xpm":
>
>filedata = open(fileName)
>text=filedata.readlines()
>for line in text:
>print line
>
>
> Once you've tested that, then you're ready to just look at line 24.
>
> text is a list, so you can refer to line 24 as text[24]
>
> Or you can get lines 24-28, with  text[24, 29]   (look up slices in the
> Python doc)
>
> ==
> DaveA
>
>
> Thanks for former help,

but I wonder how to output (write) the final result in each respectively
fileName with just different extension, such as original a.xpm write to
a.txt

Thanks,

#!/bin/python

import os.path

tokens=['E']
result=[]

for fileName in os.listdir("."):
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
for line in text[23:len(text)-1]:
result.append({t:line.count(t) for t in tokens})
for index,r in enumerate(result):
fileName.txt.write(index,"-",r)
???

-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Walter Prins
Hi Alan,

On 29 September 2011 18:12, Alan Gauld  wrote:

> On 29/09/11 17:00, Walter Prins wrote:
>
>  email.  By the way, a question to the list adminstrators: why does the
>> default reply to address for this mailing list not default to the
>> mailing list?
>>
>
> This is an oft debated issue and there are arguments for both options.
> The current setup allows easier replies to either originator or list by
> simply selecting which Reply button you use. If you set default reply to the
> list how do you reply to just the originator when you want to?
>

Well, for that particular (comparatively rare) case, with copy and paste.
(Which is in any case basically what I have to remember to do each and every
time I reply to the list-only currently, assuming I've also remembered to
click reply-all instead of reply...)

IMHO, ideally things should work exactly as they are except that "Reply"
goes to the list, "Reply-all" sends to the list as well as anyone else
(which then allows copying and pasting/re-arranging of addresses etc if only
some people are meant to get the response.) As I say, this would be
consistent with how most other mailing lists (that I'm on anyway) works.
IMHO the current default behaviour makes the most common use-case more work
than it would otherwise be, but fine, if the powers that be actually want it
like it is, then who am I to argue. :)


>
>
> > All my other mailing lists operate like that, it's only
>
>> the Python list that's peculiar in this way... )
>>
>
> I'm the opposite, all my lists work this way.
> I think the older lists tend to work like Python because all the
> older list managers (eg listserv etc) tend to work thataway.
> With the advent of web mail systems lists seem to be swinging
> the other way, but most of my mailing lists pre-date the web
> by a long way! :-)


Yes... well I don't want to start an argument, but I can't help but point
out that just because things might've in sometimes in the past been set up
to work like that doesn't mean it's neccesarily the most appropriate way it
should be done today, in general, or on this list in particular.

But, as I say, I don't want to start an argument -- if this has been debated
before (and you've just told me it has), and so has been deliberately and
intentionally set to the current behaviour then that's fine.  I'll not say
another word about it.  :) (I'll however reserve the right to have a few
choice thoughts about it next time I again forget to swap around the
addresses on reply-all, or when I again robotically click reply instead of
reply-all when responding to this list and end up sending a reply to the
person only instead of to the list and then have to go dig the reply out the
sent box again and forward it on to the list manually...  ;) )

Best,

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Prasad, Ramit
>This is an oft debated issue and there are arguments for both options.
>The current setup allows easier replies to either originator or list by 
>simply selecting which Reply button you use. If you set default reply to 
>the list how do you reply to just the originator when you want to?

I would have preferred it to set the list as the TO and the originator as the 
reply CC. 
Not sure if that works with the email standards if it just "appears" to work 
that way from Outlook.
That way I can reply by default to the list and if I want to send to the 
originator I can
also do reply all.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423




This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Alan Gauld

On 29/09/11 17:00, Walter Prins wrote:


email.  By the way, a question to the list adminstrators: why does the
default reply to address for this mailing list not default to the
mailing list?


This is an oft debated issue and there are arguments for both options.
The current setup allows easier replies to either originator or list by 
simply selecting which Reply button you use. If you set default reply to 
the list how do you reply to just the originator when you want to?


OTOH if your mail tool doesn't have a ReplyAll button its slightly less 
convenient.


The upshot of all the debates (see the archive!) has been that it is the 
way it was set up so that's the way it is.


> All my other mailing lists operate like that, it's only

the Python list that's peculiar in this way... )


I'm the opposite, all my lists work this way.
I think the older lists tend to work like Python because all the
older list managers (eg listserv etc) tend to work thataway.
With the advent of web mail systems lists seem to be swinging
the other way, but most of my mailing lists pre-date the web
by a long way! :-)


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Alan Gauld

On 29/09/11 15:22, lina wrote:

I want to read a bunch of *.doc file in present working directory,


What format are the doc files?
If they are word processor files they may well be in binary format so 
you will need to either decode them (using struct?) or find a module 
that can read them, or a tool that can convert them to something you can 
read.


Once you figure out how to read a single file reading multiple files can 
be done in a number of ways including using os.walk() and a

for loop (or the fileinput module).

for root,dirs,files in os.walk(path):
docs = [f for f in files if f.endswith '.doc'] # or use glob
for line in fileinput.input(docs):
#process line


But the hardest bit is likely going to be the reading of the files if 
they are not plain text.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread lina
On Fri, Sep 30, 2011 at 12:31 AM, Walter Prins  wrote:

> Hi,
>
> On 29 September 2011 17:07, lina  wrote:
>
>> I found one thing a bit weird, Here is the one:
>>
>> import os.path
>>
>> tokens=['E']
>> result=[]
>>
>> """
>> for fileName in os.listdir("."):
>> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
>> """
>> filedata = open("1.xpm")
>> text=filedata.readlines()
>> for line in text[23]:
>> print line
>>
>
>> 1] $ python try2.py | wc -l
>> 206
>>
>
> Please go and carefully study how strings, lists etc work -- you don't seem
> to grasp what "text[23]" returns vs what text[23:24] etc returns etc.  To
> spell it out:  If the variable text refers to a list of strings, then
> text[23] is the 24th string.  24th, because the index starts from 0.
> text[23:24] by contrast, is a sublist (called a slice) that contains strings
> 24 and 25 from the original list.
>
> So, if text[23] is already itself a single string, what can the following
> code possibly mean?  Eg in general, what does s[0] give you if s is a
> string?  A: The first character in s. So then, what does the following do:
>
> for x in text[23]:
>   print x
>
> a: It steps through the letters in text[23] and prints each in turn (on a
> new line.)
>
> So what you then do when you "wc -l" text[23], is to effectively count the
> number of characters in line 24
>
> You should probably just have run the script without the wc -l and this
> would've become very clear very quickly on inspecting the output.   ;)
>
> The other results are similarly explainable, by examining the output. (To
> be exact, the lines in the sublists/slices include newline characters.  When
> you print them seperately, you therefore end up with more lines than you
> think.  Try "print line.strip()" to get rid of the newline.  Try the
> following in the python interpreter:
>
> f=open('C:\\Users\\walterp\\Desktop\\1.xpm')
> >>> lines=f.readlines()
> >>> slice1=lines[23]
> >>> slice2=lines[23:24]
> >>> slice3=lines[23:25]
> >>> print slice1
> >>> print slice2
> >>> print slice3
> >>> print len(slice3)
>

Thanks, I truly understand now. There was a "blank line", and the strip()
works.

(I learned C 10 years ago, but barely used. so can understand something very
basic. Thanks again for your explaination)

Now I am facing how to read each column about the occurence of some letter.

Long time ago, my senior left me a script with the following line,

for line in text:
result.append({t:line.count(t) for t in tokens})

for index,r in enumerate(result):
print(index,"-",r)

I don't understand how it works, kinda of transpose a matrix?




> etc.
>
>
> Walter
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Walter Prins
Hi,

On 29 September 2011 17:07, lina  wrote:

> I found one thing a bit weird, Here is the one:
>
> import os.path
>
> tokens=['E']
> result=[]
>
> """
> for fileName in os.listdir("."):
> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
> """
> filedata = open("1.xpm")
> text=filedata.readlines()
> for line in text[23]:
> print line
>

> 1] $ python try2.py | wc -l
> 206
>

Please go and carefully study how strings, lists etc work -- you don't seem
to grasp what "text[23]" returns vs what text[23:24] etc returns etc.  To
spell it out:  If the variable text refers to a list of strings, then
text[23] is the 24th string.  24th, because the index starts from 0.
text[23:24] by contrast, is a sublist (called a slice) that contains strings
24 and 25 from the original list.

So, if text[23] is already itself a single string, what can the following
code possibly mean?  Eg in general, what does s[0] give you if s is a
string?  A: The first character in s. So then, what does the following do:

for x in text[23]:
  print x

a: It steps through the letters in text[23] and prints each in turn (on a
new line.)

So what you then do when you "wc -l" text[23], is to effectively count the
number of characters in line 24

You should probably just have run the script without the wc -l and this
would've become very clear very quickly on inspecting the output.   ;)

The other results are similarly explainable, by examining the output. (To be
exact, the lines in the sublists/slices include newline characters.  When
you print them seperately, you therefore end up with more lines than you
think.  Try "print line.strip()" to get rid of the newline.  Try the
following in the python interpreter:

f=open('C:\\Users\\walterp\\Desktop\\1.xpm')
>>> lines=f.readlines()
>>> slice1=lines[23]
>>> slice2=lines[23:24]
>>> slice3=lines[23:25]
>>> print slice1
>>> print slice2
>>> print slice3
>>> print len(slice3)

etc.


Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread lina
On Thu, Sep 29, 2011 at 11:57 PM, Walter Prins  wrote:

> Hi,
>
> On 29 September 2011 16:39, lina  wrote:
>
>>
>> Or you can get lines 24-28, with  text[24, 29]   (look up slices in the
>>> Python doc)
>>>
>>
> Dave probably meant: text[24:29]
>
>
>
>>  >>> print splitext.__doc__
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> NameError: name 'splitext' is not defined
>> >>> print slices.__doc__
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> NameError: name 'slices' is not defined
>> >>> print slices._doc_
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> NameError: name 'slices' is not defined
>>
>
> You should probably be looking in the documentation, not trying to feed it
> into the interpreter.  (If you want to get help on a specific entity in
> Python it's usually better to use the "help()" function, e.g:
>
> >>> import os.path
> >>> help (os.path.splitext)
> Help on function splitext in module ntpath:
>
> splitext(p)
> Split the extension from a pathname.
>
> Extension is everything from the last dot to the end, ignoring
> leading dots.  Returns "(root, ext)"; ext may be empty.
>
> >>>
>
> Note, trying to do "help(os.path.splitext)" would not have worked before I
> imported the "os.path" module.  (Differently put: Whatever you want to get
> help() on, must be known to the interpreter, so must either be built in or
> previously imported.)
>
> Walter
>
>
> Thanks all for your time,

I found one thing a bit weird, Here is the one:

import os.path

tokens=['E']
result=[]

"""
for fileName in os.listdir("."):
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
"""
filedata = open("1.xpm")
text=filedata.readlines()
for line in text[23]:
print line

1] $ python try2.py | wc -l
206

now changed to:
for line in text[23:24]:
print line

2] $ python try2.py | wc -l
2

for line in text[23:25]:
print line

3] $ python try2.py | wc -l
4

the situation 1 is supposed to be 1, while it's 206, it's just tilted 90
degree of this one line, kind of split. (if I am right here).
2] is correct.
3] is supposed to be 3, right, why it's 4?

and I want to check the correctness before using
for line in text[23:len(text)-1]:
print line

I upload the 1.xpm in below link, really appreciate for the guidance.

https://docs.google.com/leaf?id=0B93SVRfpVVg3NzQxNWU4ZjktNzkwZi00ZDI5LWI1YzAtZTBkZGUzNzJmNGJh&sort=name&layout=list&num=50

-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Walter Prins
Hi,

On 29 September 2011 16:39, lina  wrote:

> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'slices' is not defined
>

Sorry I meant to include a link to relevant documentation:
http://docs.python.org/tutorial/introduction.html

(And apologies for forgetting to remove ther other recipients from the
email.  By the way, a question to the list adminstrators: why does the
default reply to address for this mailing list not default to the mailing
list?  All my other mailing lists operate like that, it's only the Python
list that's peculiar in this way... )

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Walter Prins
Hi,

On 29 September 2011 16:39, lina  wrote:

>
> Or you can get lines 24-28, with  text[24, 29]   (look up slices in the
>> Python doc)
>>
>
Dave probably meant: text[24:29]



>  >>> print splitext.__doc__
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'splitext' is not defined
> >>> print slices.__doc__
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'slices' is not defined
> >>> print slices._doc_
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'slices' is not defined
>

You should probably be looking in the documentation, not trying to feed it
into the interpreter.  (If you want to get help on a specific entity in
Python it's usually better to use the "help()" function, e.g:

>>> import os.path
>>> help (os.path.splitext)
Help on function splitext in module ntpath:

splitext(p)
Split the extension from a pathname.

Extension is everything from the last dot to the end, ignoring
leading dots.  Returns "(root, ext)"; ext may be empty.

>>>

Note, trying to do "help(os.path.splitext)" would not have worked before I
imported the "os.path" module.  (Differently put: Whatever you want to get
help() on, must be known to the interpreter, so must either be built in or
previously imported.)

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Prasad, Ramit
Or you can get lines 24-28, with  text[24, 29]   (look up slices in the Python 
doc)

 >>> print splitext.__doc__
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'splitext' is not defined
>>> print slices.__doc__
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'slices' is not defined
>>> print slices._doc_
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'slices' is not defined


===
I think he meant something like: 
http://docs.python.org/tutorial/introduction.html (see the section on strings 
around half way down) or 
http://docs.python.org/release/2.3.5/whatsnew/section-slices.html



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread lina
On Thu, Sep 29, 2011 at 11:28 PM, Dave Angel  wrote:

> (Please don't top-post.  Put your remarks AFTER the part you're quoting
> from the previous message)
>
>
> On 09/29/2011 10:55 AM, lina wrote:
>
>> import os.path
>>
>> tokens=['E']
>> result=[]
>>
>> for fileName in os.listdir("."):
>> if os.path.isfile(fileName) and os.path.splitext(fileName)=="**xpm":
>> filedata = open(fileName)
>> text=filedata.readlines()
>> for line in text:
>>
>>
>> How can I read from line 24 and do further looking for "E".
>>
>> Thanks,
>>
>>
>>
> As I said in my earlier message, this was untested.  It gave you the
> building blocks, but was not correct.
>
> In particular, that if-test will always fail, so you're not seeing any
> files.
>
>
> import os.path
>
> tokens=['E']
> result=[]
>
> for fileName in os.listdir("."):
>
>if os.path.isfile(fileName) and os.path.splitext(fileName)[1]=**
> =".xpm":
>
>filedata = open(fileName)
>text=filedata.readlines()
>for line in text:
>print line
>
>
> Once you've tested that, then you're ready to just look at line 24.
>
> text is a list, so you can refer to line 24 as text[24]
>
> Or you can get lines 24-28, with  text[24, 29]   (look up slices in the
> Python doc)
>

 >>> print splitext.__doc__
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'splitext' is not defined
>>> print slices.__doc__
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'slices' is not defined
>>> print slices._doc_
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'slices' is not defined

Thanks,

>
> ==
> DaveA
>
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Dave Angel
(Please don't top-post.  Put your remarks AFTER the part you're quoting 
from the previous message)


On 09/29/2011 10:55 AM, lina wrote:

import os.path

tokens=['E']
result=[]

for fileName in os.listdir("."):
 if os.path.isfile(fileName) and os.path.splitext(fileName)=="xpm":
 filedata = open(fileName)
 text=filedata.readlines()
 for line in text:


How can I read from line 24 and do further looking for "E".

Thanks,




As I said in my earlier message, this was untested.  It gave you the 
building blocks, but was not correct.


In particular, that if-test will always fail, so you're not seeing any 
files.


import os.path

tokens=['E']
result=[]

for fileName in os.listdir("."):

if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
for line in text:
print line


Once you've tested that, then you're ready to just look at line 24.

text is a list, so you can refer to line 24 as text[24]

Or you can get lines 24-28, with  text[24, 29]   (look up slices in the 
Python doc)


==
DaveA


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread lina
On Thu, Sep 29, 2011 at 11:18 PM, Walter Prins  wrote:

>
>
> On 29 September 2011 16:13, lina  wrote:
>
>> mport os.path
>>
>> tokens=['E']
>> result=[]
>>
>> for fileName in os.listdir("."):
>> if os.path.isfile(fileName) and os.path.splitext(fileName)=="xpm":
>> filedata = open(fileName,'r')
>>
>> text=filedata.readlines()
>> for line in text:
>> print line
>>
>> why here I print nothing out?
>>
>> There is .xpm file there with content.
>>
>> Thanks,
>>
>>
> Is your .xpm files actually text files?  Or are they binary (e.g. maybe
> graphics) files?  (XPM is normally the extension of a graphics filetype.)
> Treating a binary file as if its text is not liable to work very well.
>
> Also can you please confirm whether this task is homework/study related?
>

I have some awk script to achieve this. I can assure you it's not homework.
LOL ... I have never attended some python course.
sometimes it's hard for you to image how hard I persuade myself to use/think
in python way, forget what is-already-relatively-easy-for-me-to-use bash
script.

the xpm file an read, I just text

f=open('1.xpm','r')
for line in f.readlines():
print line
f.close()

I am google-ing how to get the line index, so will use those after 24.


>
> Walter
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread Walter Prins
On 29 September 2011 16:13, lina  wrote:

> mport os.path
>
> tokens=['E']
> result=[]
>
> for fileName in os.listdir("."):
> if os.path.isfile(fileName) and os.path.splitext(fileName)=="xpm":
> filedata = open(fileName,'r')
>
> text=filedata.readlines()
> for line in text:
> print line
>
> why here I print nothing out?
>
> There is .xpm file there with content.
>
> Thanks,
>
>
Is your .xpm files actually text files?  Or are they binary (e.g. maybe
graphics) files?  (XPM is normally the extension of a graphics filetype.)
Treating a binary file as if its text is not liable to work very well.

Also can you please confirm whether this task is homework/study related?

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread lina
mport os.path

tokens=['E']
result=[]

for fileName in os.listdir("."):
if os.path.isfile(fileName) and os.path.splitext(fileName)=="xpm":
filedata = open(fileName,'r')
text=filedata.readlines()
for line in text:
print line

why here I print nothing out?

There is .xpm file there with content.

Thanks,

On Thu, Sep 29, 2011 at 11:06 PM, lina  wrote:

>
>
> On Thu, Sep 29, 2011 at 10:55 PM, lina  wrote:
>
>> import os.path
>>
>> tokens=['E']
>> result=[]
>>
>>
>> for fileName in os.listdir("."):
>> if os.path.isfile(fileName) and os.path.splitext(fileName)=="xpm":
>> filedata = open(fileName)
>> text=filedata.readlines()
>> for line in text:
>>
>>
>> How can I read from line 24 and do further looking for "E".
>>
>> Thanks,
>>
>>
> The file starts from line 24 is something like
>
> aabbccddEaabb
> acbbddEbbaaca
> EabcEabc
> aEaaEaaa
>
> so for the first column, I can get 1 E, second column is also 1 E, third is
> 0, four is 2.
>
> namely count the occurence of E in each column.
>
> Thanks,
>
>
>>
>>
>> On Thu, Sep 29, 2011 at 10:43 PM, Dave Angel  wrote:
>>
>>> On 09/29/2011 10:22 AM, lina wrote:
>>>
 Hi,

 I want to read a bunch of *.doc file in present working directory,

 how can I use for to read one by one and do further work,

 sorry,

 what's the best reference webpage I can use?

 I googled, lots of distracting info, and I barely can understand how
 they
 think.

 THanks,

  Look in the os module for things like this.  In particular (untested):
>>>
>>> import os.path
>>>
>>> for fileName in os.listdir(".")
>>>if os.path.isfile(fileName) and os.path.splitext(fileName) == "doc":
>>>filedata = open(fileName)
>>>xxxdosomething with filedata
>>>
>>>
>>>
>>> --
>>>
>>> DaveA
>>>
>>>
>>
>>
>> --
>> Best Regards,
>>
>> lina
>>
>>
>>
>
>
> --
> Best Regards,
>
> lina
>
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-09-29 Thread lina
On Thu, Sep 29, 2011 at 10:55 PM, lina  wrote:

> import os.path
>
> tokens=['E']
> result=[]
>
>
> for fileName in os.listdir("."):
> if os.path.isfile(fileName) and os.path.splitext(fileName)=="xpm":
> filedata = open(fileName)
> text=filedata.readlines()
> for line in text:
>
>
> How can I read from line 24 and do further looking for "E".
>
> Thanks,
>
>
The file starts from line 24 is something like

aabbccddEaabb
acbbddEbbaaca
EabcEabc
aEaaEaaa

so for the first column, I can get 1 E, second column is also 1 E, third is
0, four is 2.

namely count the occurence of E in each column.

Thanks,


>
>
> On Thu, Sep 29, 2011 at 10:43 PM, Dave Angel  wrote:
>
>> On 09/29/2011 10:22 AM, lina wrote:
>>
>>> Hi,
>>>
>>> I want to read a bunch of *.doc file in present working directory,
>>>
>>> how can I use for to read one by one and do further work,
>>>
>>> sorry,
>>>
>>> what's the best reference webpage I can use?
>>>
>>> I googled, lots of distracting info, and I barely can understand how they
>>> think.
>>>
>>> THanks,
>>>
>>>  Look in the os module for things like this.  In particular (untested):
>>
>> import os.path
>>
>> for fileName in os.listdir(".")
>>if os.path.isfile(fileName) and os.path.splitext(fileName) == "doc":
>>filedata = open(fileName)
>>xxxdosomething with filedata
>>
>>
>>
>> --
>>
>> DaveA
>>
>>
>
>
> --
> Best Regards,
>
> lina
>
>
>


-- 
Best Regards,

lina
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


  1   2   >