[Tutor] new user question

2013-01-15 Thread Adel Afzal

Hi,

I am new to Python, and my interest in learning is to make a Python 
script that can do something like what I describe below.  Could you 
please provide some advice, or direct me to some resources that may help 
me along in this process?



Lets say there are three pages:

http://bob.com/dogs.html
http://bob.com/cats.html
http://bob.com/birds.html

 contains the text "dogs chase cats"
 contains the text "cats chase birds"
 contains the text "birds don't bother anyone"

I'd like to know how to search the content of those three pages for the 
string "cats" in the text body.


I'd like to then output the URLs, whose html files contain "cats", as a 
plain text list. Like this:


:
http://bob.com/dogs.html
http://bob.com/cats.html

Kind regards,

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


Re: [Tutor] How to run multiline shell command within python

2013-01-15 Thread Matty Sarro
Have you looked a the pexpect class? It works like gangbusters, especially
if you're trying to run something with an interactive shell.

http://www.noah.org/wiki/pexpect


On Thu, Jan 10, 2013 at 9:25 AM, Karim  wrote:

>  On 10/01/2013 09:31, Hugo Arts wrote:
>
> On Thu, Jan 10, 2013 at 7:01 AM, Karim  wrote:
>
>>
>>
>> Hello all,
>>
>> I want to run multiline shell command within python without using a
>> command file but directly execute several lines of shell.
>> I already use *subprocess.checkoutput("csh -f my_file.csh".split())* but
>> I want to know if it is posssible to avoid making file and execute
>> shell lines of code directly.
>>
>>
>  Yes, this is very possible. Specify shell=True as an argument and you
> can do anything you can do in a shell:
>
>   >>> commands = """echo hello
> ... echo hello | wc -l
> ... ps aux | grep python"""
> >>> b = subprocess.check_output(commands, shell=True)
> >>> print(b.decode('ascii'))
>  hello
> 1
> hugo  1255  1.0  0.6 777316 49924 ?Sl   09:14   0:08
> /usr/bin/python2 /usr/bi
> hugo  6529  0.0  0.0  42408  7196 pts/0S+   09:23   0:00 python
> hugo  6559  0.0  0.0  10656  1128 pts/0S+   09:28   0:00 grep
> python
>
>  >>>
>
>  watch out though, accepting user input into the commands variable will
> lead to shell injection, which can be a dangerous security vulnerability.
>
>  HTH,
> Hugo
>
>
> Many thanks Hugo. It makes my day!
> In my case there are no possibilities for shell injection. It is internal
> to a class.
>
> Regards
> Karim
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] UnicodeWarning: Comparing String

2013-01-15 Thread -

Hi,

I am trying to check if a Unicode string matches data from an Easygui 
"enterbox". I spent one hour looking online how to solve this problem 
and I'm still stuck because I don't understand all of the explanations I 
find. Lots of trial and error didn't help either.


I use Python 2.5.1.

===This is the error message:===

UnicodeWarning: Unicode equal comparison failed to convert both 
arguments to Unicode - interpreting them as being unequal


I do understand that everything has to be in Unicode for my code to 
work, but I do not know how to properly change my code.


===This is my code:===


# coding: iso-8859-1

from easygui import *
import sys

good_answer = "pureté"  # notice the accute accent

answer_given = enterbox("Type your answer!\n\nHint: 'pureté'")

if answer_given == good_answer:
msgbox("Correct! The answer is 'pureté'")
else:
msgbox("Bug!")


Thank you in advance!

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


Re: [Tutor] UnicodeWarning: Comparing String

2013-01-15 Thread Prasad, Ramit
ialec...@gmail.com wrote:
> Hi,
> 
> I am trying to check if a Unicode string matches data from an Easygui 
> "enterbox". I spent one hour looking
> online how to solve this problem and I'm still stuck because I don't 
> understand all of the explanations I find.
> Lots of trial and error didn't help either.
> 
> I use Python 2.5.1.
> 
> ===This is the error message:===
> 
> 
> UnicodeWarning: Unicode equal comparison failed to convert both arguments to 
> Unicode - interpreting them as
> being unequal
> 
> I do understand that everything has to be in Unicode for my code to work, but 
> I do not know how to properly
> change my code.

That is not the full text of the error message. The full text
includes a stack trace and should always be included verbatim. Copy
and paste rather than retyping as accuracy of the message is 
important in helping you.

> 
> ===This is my code:===
> 
> 
> # coding: iso-8859-1
> 
> from easygui import *
> import sys
> 
> good_answer = "pureté"  # notice the accute accent
> 
> answer_given = enterbox("Type your answer!\n\nHint: 'pureté'")

If good_answer is always manually defined (rather than being retrieved
from somewhere, you can just manually set it to Unicode by prefixing
the string with a 'u'.

good_answer = u'pureté' # acute accent and now Unicode!

Otherwise you will have to use one of the below lines.

good_answer.decode() # converts to Unicode.
# answer_given.encode() # converts from Unicode to format

> 
> if answer_given == good_answer:
>     msgbox("Correct! The answer is 'pureté'")
> else:
>     msgbox("Bug!")
> 
> Thank you in advance!

Unicode is fairly complex, but maybe this[0] will help if you run into 
problems (or just come back to the list). You can find the list of
encoding formats (called codecs) here[1].

[0] http://docs.python.org/howto/unicode.html
[1] http://docs.python.org/2/library/codecs.html#standard-encodings

Please do not post in HTML or with a colored background.
This list prefers the usage of plain text emails as HTML can
remove indentation and since Python's indentation is 
significant it makes it much more difficult to help you. 
Also, top posting discouraged in favor of either bottom or
in-line posting. Thanks!


~Ramit


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] new user question

2013-01-15 Thread Alan Gauld

On 13/01/13 16:10, Adel Afzal wrote:

 contains the text "dogs chase cats"
 contains the text "cats chase birds"
 contains the text "birds don't bother anyone"

I'd like to know how to search the content of those three pages for the
string "cats" in the text body.


Look at the urllib modules in the standard library.
Also Beautiful Soup is a third part module that many find useful for 
reading HTML, especially if it is not cleanly formatted.


That assumes that although new to python you are familiar with HTML and 
parsing in general. If not then you may need more hand holding after 
reading the docs. If so come back with some specific questions so we can 
gauge your level of understanding and respond accordingly.




--
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] Assigning multi line value to a variable

2013-01-15 Thread Rohit Mediratta

Hi,
   I am using Centos 6.3 and python 2.6.6.

When I try to assign a variables value inside a multiple line message, it does 
not work.

>>>cardName = "ActualCardName"
>>>data = """ 
 """
>>> print data
 
 


I would like %cardName to be replaced by "actualCardName". As you can see I am 
trying to use XML and therefore I will have several such replacements that I 
will need to do.


If someone can suggest a clean way for me to achieve my goal, that would be 
great.

Please note, my input is going to be several lines long.

thanks,
Rohit

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


[Tutor] Question regarding lists and manipulating items in lists.

2013-01-15 Thread Scurvy Scott
Hello guys, I'm using Ubuntu 12.10 and Python 2.7 right now. I'm working on
code using the Mingus module but this question isn't specific to this
module, per se.

What I'm trying to do is to generate the fibonacci numbers up to a given N
and then do modulo 12 on each number in order to create a list of numbers
for the Mingus module to convert to notes. What I would like to do is store
each note created in a different element in a list so that I can later
manipulate it, say by removing certain note letters from the list at will.
That way the scales created by the program can become more useful, for
example I could remove all notes that aren't present in say a Harmonic
Minor scale, and only keep the list items that do exist in that scale in a
given key. That way it becomes, in a way, like the computer is writing your
guitar solos! Pretty neat I think.

Anyways, the problem I'm having is I'm not really sure how to search a list
for multiple elements and remove just those elements. Below is my code so
far, and information y'all could provide would be appreciated. Thanks.

import mingus.core.notes as notes
#fibonacci
def fib(num1,num2):
a, b = 0, 1
for i in xrange(num1,num2):
c = b % 12 #modulo 12 on each generated fibonacci number
a_list= [notes.int_to_note(c)] #using Mingus to translate the Fib mod12
numbers into notes and then (I think) storing each one as an element in a
list?
a, b = b, a+b #this is just the algorithm for the fibonacci numbers
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assigning multi line value to a variable

2013-01-15 Thread Oscar Benjamin
On 15 January 2013 23:12, Rohit Mediratta  wrote:
> Hi,
>I am using Centos 6.3 and python 2.6.6.
>
> When I try to assign a variables value inside a multiple line message, it
> does not work.
>
cardName = "ActualCardName"
data = """ 
>  """
 print data
>  
> 
>
>
> I would like %cardName to be replaced by "actualCardName". As you can see I
> am trying to use XML and therefore I will have several such replacements
> that I will need to do.

How about this?

>>> data = """ 
...  """
>>>
>>> print data
 

>>> print data % {'cardName':'actualCardName'}
 



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


Re: [Tutor] Assigning multi line value to a variable

2013-01-15 Thread Steven D'Aprano

On 16/01/13 10:12, Rohit Mediratta wrote:


Hi,
I am using Centos 6.3 and python 2.6.6.

When I try to assign a variables value inside a multiple line message, it does 
not work.


cardName = "ActualCardName"
data = """

   """

print data

  
 


I would like %cardName to be replaced by "actualCardName". As you can see
 I am trying to use XML and therefore I will have several such replacements
that I will need to do.



The error you show has nothing to do with "multiple line message", since it 
also doesn't work with a single line message:

py> x = "Hello"
py> msg = "%x"
py> print msg
%x


The way to fix this is the same regardless of whether the message is one line 
or a million lines. Python does not automatically substitute variables into 
strings, since that is a bad idea. However you can tell Python to substitute 
any values you like, using the % operator and string interpolation:

py> a = "spam"
py> b = "eggs"
py> msg = "Breakfast today is %s and %s."
py> print msg % (a, b)
Breakfast today is spam and eggs.


String interpolation codes include %s for strings, %d for integers, and %f for 
floats (among others).

You can also name the interpolation codes instead of substituting them by 
position:

py> values = {"bird": "parrot", "colour": "green"}
py> msg = "The %(colour)s %(bird)s ate the fruit."
py> print msg % values
The green parrot ate the fruit.


This leads to a clever trick: if you name the interpolation codes with the 
variable names you want, you can get the values of variables using the locals() 
function:


py> a = 42
py> b = "Hello World"
py> msg = "My variables include %(a)s and %(b)s."
py> print msg % locals()
My variables include 42 and Hello World.


More information about the string interpolation can be read here:

http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

If you are familiar with C, you should find this very familiar.

Other alternatives include the string format method, and string templates:

http://docs.python.org/2/library/stdtypes.html#str.format
http://docs.python.org/2/library/string.html#template-strings

For example:

py> import string
py> msg = string.Template("My variables include $a and $b but not $c.")
py> print msg.safe_substitute(locals())
My variables include 42 and Hello World but not $c.



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


Re: [Tutor] Question regarding lists and manipulating items in lists.

2013-01-15 Thread Oscar Benjamin
On 15 January 2013 23:40, Scurvy Scott  wrote:
[SNIP]
>
> Anyways, the problem I'm having is I'm not really sure how to search a list
> for multiple elements and remove just those elements. Below is my code so
> far, and information y'all could provide would be appreciated. Thanks.

Perhaps you'd like to use a list comprehension to filter out the
values you are interested in:

>>> my_list = [1,4,12,3,5,2,1,45,6,32]
>>> my_filtered_list = [x for x in my_list if x%2] # only the odd numbers
>>> print my_filtered_list
[1, 3, 5, 1, 45]

>
> import mingus.core.notes as notes
> #fibonacci
> def fib(num1,num2):
> a, b = 0, 1
> for i in xrange(num1,num2):
> c = b % 12 #modulo 12 on each generated fibonacci number
> a_list= [notes.int_to_note(c)] #using Mingus to translate the Fib mod12
> numbers into notes and then (I think) storing each one as an element in a
> list?
> a, b = b, a+b #this is just the algorithm for the fibonacci numbers

Please post in plain-text rather than html as it screws up the code
formatting (see above).


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


Re: [Tutor] Assigning multi line value to a variable

2013-01-15 Thread Prasad, Ramit
Rohit Mediratta wrote:
> 
> Hi,
>    I am using Centos 6.3 and python 2.6.6.
> 
> When I try to assign a variables value inside a multiple line message, it 
> does not work.
> 
> >>>cardName = "ActualCardName"
> >>>data = """ 
>      """
> >>> print data
>  
>     
> 
> 
> I would like %cardName to be replaced by "actualCardName". As you can see I 
> am trying to use XML and therefore I
> will have several such replacements that I will need to do.
> 


data = """ 
""".format( cardName )

See here for more information on formatting syntax.
http://docs.python.org/2/library/string.html#formatstrings

> 
> If someone can suggest a clean way for me to achieve my goal, that would be 
> great.
> 
> Please note, my input is going to be several lines long.
> 
> thanks,
> Rohit


~Ramit


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] Question regarding lists and manipulating items in lists.

2013-01-15 Thread Steven D'Aprano

On 16/01/13 10:40, Scurvy Scott wrote:
[...]

Anyways, the problem I'm having is I'm not really sure how to search a list
for multiple elements and remove just those elements. Below is my code so
far, and information y'all could provide would be appreciated. Thanks.


Actually, your problem so far is that you aren't even producing a list of
elements at all, you keep creating a *single* element, then throwing it
away when you generate the next Fibonacci number.

Also, you, or more likely Gmail, lost the indentation in your code, so I'm
going to have to guess what you intended rather than what you have. That's
because you are sending HTML email, which eats spaces.



import mingus.core.notes as notes
#fibonacci
def fib(num1,num2):
a, b = 0, 1
for i in xrange(num1,num2):
c = b % 12 #modulo 12 on each generated fibonacci number
a_list= [notes.int_to_note(c)] #using Mingus to translate the Fib mod12
numbers into notes and then (I think) storing each one as an element in a
list?
a, b = b, a+b #this is just the algorithm for the fibonacci numbers



Firstly, I recommend that you follow the principle "separation of concerns".
Keep a separate function for each part of the problem:

* generate Fibonacci numbers;
* turn them into notes;


So here I extract out of your code (untested!) a generator which produces
an infinite series of Fibonacci numbers, one at a time:

def fib():
a, b = 0, 1
while True:
yield b
a, b = b, a+b


This is untested, I may have got it wrong.

Next, a function to generate notes from those Fibonacci numbers:


def make_notes(num_notes):
it = fib()
notes = []  # start with an empty list
for i in range(num_notes):
n = next(it) % 12  # get the next Fibonacci number, modulo 12
notes.append(notes.int_to_note(n))
return notes


That returns a list of notes.


Does that help? Start with that, and see how you go.



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


Re: [Tutor] Question regarding lists and manipulating items in lists.

2013-01-15 Thread Oscar Benjamin
On 15 January 2013 23:53, Scurvy Scott  wrote:
>> > Anyways, the problem I'm having is I'm not really sure how to search a list
>> > for multiple elements and remove just those elements. Below is my code so
>> > far, and information y'all could provide would be appreciated. Thanks.
>>
[SNIP]
>
> What I meant to say is, I want to be able to search the generated list
> for a specific set of strings so like
>
> list = ['a','b','c','d','e','f','g']
> search = ['b','d','g']
> list.del[search]

How about:

new_list = [element for element in list if element not in search]

(It would be more efficient to use a set but it's not strictly necessary)


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


Re: [Tutor] Question regarding lists and manipulating items in lists.

2013-01-15 Thread Scurvy Scott
On Tue, Jan 15, 2013 at 4:01 PM, Steven D'Aprano  wrote:
> On 16/01/13 10:40, Scurvy Scott wrote:
> [...]
>
>> Anyways, the problem I'm having is I'm not really sure how to search a
>> list
>> for multiple elements and remove just those elements. Below is my code so
>> far, and information y'all could provide would be appreciated. Thanks.
>
>
> Actually, your problem so far is that you aren't even producing a list of
> elements at all, you keep creating a *single* element, then throwing it
> away when you generate the next Fibonacci number.
>
> Also, you, or more likely Gmail, lost the indentation in your code, so I'm
> going to have to guess what you intended rather than what you have. That's
> because you are sending HTML email, which eats spaces.
>
>
>
>> import mingus.core.notes as notes
>> #fibonacci
>> def fib(num1,num2):
>> a, b = 0, 1
>> for i in xrange(num1,num2):
>> c = b % 12 #modulo 12 on each generated fibonacci number
>> a_list= [notes.int_to_note(c)] #using Mingus to translate the Fib mod12
>> numbers into notes and then (I think) storing each one as an element in a
>> list?
>> a, b = b, a+b #this is just the algorithm for the fibonacci numbers
>
>
>
> Firstly, I recommend that you follow the principle "separation of concerns".
> Keep a separate function for each part of the problem:
>
> * generate Fibonacci numbers;
> * turn them into notes;
>
>
> So here I extract out of your code (untested!) a generator which produces
> an infinite series of Fibonacci numbers, one at a time:
>
> def fib():
>
> a, b = 0, 1
> while True:
> yield b
>
> a, b = b, a+b
>
>
> This is untested, I may have got it wrong.
>
> Next, a function to generate notes from those Fibonacci numbers:
>
>
> def make_notes(num_notes):
> it = fib()
> notes = []  # start with an empty list
> for i in range(num_notes):
> n = next(it) % 12  # get the next Fibonacci number, modulo 12
> notes.append(notes.int_to_note(n))
> return notes
>
>
> That returns a list of notes.
>
>
> Does that help? Start with that, and see how you go.
>
>
>
> --
> Steven
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Steve
After playing with your example I keep being told that list has no
attribute int_to_note. I know what the problem is, I just don't know
how to fix it.
Also, I believe I've fixed my email so it will no longer be in HTML or
anything fancy, just plain text.

So right now my code is:

import mingus.core.notes as notes


#fibonacci
def fib():
a, b = 0, 1
while True:
yield b
a, b = b, a+b


def make_notes(num_notes):
it = fib()
notes = []
for i in range(num_notes):
n = next(it) % 12
notes.append(notes.int_to_note(n))
return notes

Which is pretty different from what my original code was. The
generator function doesn't actually do anything when called, it just
tells me it's a generator function and where it's located. And like I
said the listing function doesn't want to comply with the module being
called under append method. My code was working almost as I intended
it to, it just wasn't creating a list properly of everything, which I
didn't notice until after one of you guys mentioned it to me. Now I
see it and I can sorta visualize what I'm supposed to do, but can't
see what to do to fix it, if that makes any sense.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question regarding lists and manipulating items in lists.

2013-01-15 Thread Scurvy Scott

>> So here I extract out of your code (untested!) a generator which produces
>> an infinite series of Fibonacci numbers, one at a time:
>>
>> def fib():
>>
>> a, b = 0, 1
>> while True:
>> yield b
>>
>> a, b = b, a+b
>>
>>
>> This is untested, I may have got it wrong.
>>
>> Next, a function to generate notes from those Fibonacci numbers:
>>
>>
>> def make_notes(num_notes):
>> it = fib()
>> notes = []  # start with an empty list
>> for i in range(num_notes):
>> n = next(it) % 12  # get the next Fibonacci number, modulo 12
>> notes.append(notes.int_to_note(n))
>> return notes
>>
>>
>> That returns a list of notes.
>>
>>
>> Does that help? Start with that, and see how you go.
>>
>>
>>
>> --
>> Steven
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> Steve
> After playing with your example I keep being told that list has no
> attribute int_to_note. I know what the problem is, I just don't know
> how to fix it.
> Also, I believe I've fixed my email so it will no longer be in HTML or
> anything fancy, just plain text.
>
> So right now my code is:
>
> import mingus.core.notes as notes
>
>
> #fibonacci
> def fib():
> a, b = 0, 1
> while True:
> yield b
> a, b = b, a+b
>
>
> def make_notes(num_notes):
> it = fib()
> notes = []
> for i in range(num_notes):
> n = next(it) % 12
> notes.append(notes.int_to_note(n))
> return notes
>
> Which is pretty different from what my original code was. The
> generator function doesn't actually do anything when called, it just
> tells me it's a generator function and where it's located. And like I
> said the listing function doesn't want to comply with the module being
> called under append method. My code was working almost as I intended
> it to, it just wasn't creating a list properly of everything, which I
> didn't notice until after one of you guys mentioned it to me. Now I
> see it and I can sorta visualize what I'm supposed to do, but can't
> see what to do to fix it, if that makes any sense.


On a hunch I've gone back to my original code, and feel I've almost
got the list properly working.

import mingus.core.notes as notes


#fibonacci
def fib(num1,num2):
a, b = 0, 1
for i in xrange(num1,num2):
c = b % 12
a_list= []
a, b = b, a+b
while True:
a_list.append(notes.int_to_note(c))
print a_list

The problem here, which most of you will probably see immediately, is
that all this does is infinitely append the same note to the list over
and over again, which is not my intention. This is the closest I've
gotten the code to actually working somewhat correctly as far as this
list is concerned. Any hints would be helpful.

I also think Oscars solution with searching the list for items in the
'search' list and then just removing them is the most elegant so far
but I'm still open to anything you guys can show me, that's why you're
the tutors and I came here to ask, right?

I appreciate any information y'all can share.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] reading from text files

2013-01-15 Thread Gina

I have version 3

I am trying to read a text file ("the_file.txt") and then write a new 
file where the characters are in uppercase

but i don't know how/where i should use the .upper
I think i should use it where the ** is, but i got errors when i 
tried that



text_file = open("the_file.txt", "r")
print(text_file.read())
text_file.close()

new_file = open("the_file_upper.txt", "w+")
new_file.write(*)
print(new_file.read())
new_file.close()


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


[Tutor] text files

2013-01-15 Thread Gina

I have version 3

I am trying to read a text file ("the_file.txt") and then write a new 
file where the characters are in uppercase

but i don't know how/where i should use the .upper
I think i should use it where the ** is, but i got errors when i 
tried that



text_file = open("the_file.txt", "r")
print(text_file.read())
text_file.close()

new_file = open("the_file_upper.txt", "w+")
new_file.write(*)
print(new_file.read())
new_file.close()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] text files

2013-01-15 Thread Dave Angel

On 01/15/2013 09:31 PM, Gina wrote:

I have version 3

I am trying to read a text file ("the_file.txt") and then write a new 
file where the characters are in uppercase

but i don't know how/where i should use the .upper
I think i should use it where the ** is, but i got errors when i 
tried that



text_file = open("the_file.txt", "r")
print(text_file.read())
text_file.close()

new_file = open("the_file_upper.txt", "w+")
new_file.write(*)
print(new_file.read())
new_file.close()


Did you have a good reason to send a second identical message after 
waiting only 25 minutes for a response from the first?  How about if you 
must do so, say SOMETHING new in the message, and make it a reply to the 
first, so they get threaded together and have the same subject line.


Anyway, your first problem is you never save any data from the first 
file.  You're trying to do two very separate things in the same line.  
You read the data and immediately print it, without putting it any place 
you can reference it.  Try something like:


indata = text_file.read()
print(indata) #optional

Now, you can do something to indata before writing it to the output 
file.  See how you get along with that.


Another point:  If you get an error, be explicit.  Show us what you 
tried, and quote the exact error (including the traceback of course).  
Otherwise it's like making an anonymous call to the police:


"Hello, somebody burglarized some house yesterday.  Can you please tell 
me what they did with the toaster they broke?"  And then hanging up 
immediately.


BTW, thanks for sending a text message instead of html.

--
DaveA

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


Re: [Tutor] Question regarding lists and manipulating items in lists.

2013-01-15 Thread Andreas Perstinger

On 16.01.2013 01:23, Scurvy Scott wrote:
> After playing with your example I keep being told that list has no
> attribute int_to_note. I know what the problem is, I just don't know
> how to fix it.
[SNIP]
> So right now my code is:
>
> import mingus.core.notes as notes
  ^
On this line you import your module and give it the name "notes".

> def make_notes(num_notes):
>it = fib()
>notes = []
^

Inside your function "notes" is a list.

>for i in range(num_notes):
>n = next(it) % 12
>notes.append(notes.int_to_note(n))
 ^

Since "notes" is a list inside the function, Python tries to find the 
method "int_to_note" for a list and fails. But I think you want to use 
the function which is defined in your module.


You have to either rename your module reference or your list.

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