[Tutor] (no subject)

2018-03-28 Thread naoki_morihira
I want to install 3rd party module, ex openpyxl.
And I executed the following command in windows command prompt as follows:
pip install openpyxl
But pip is not recognized as executable command at windows.
I also tried the same way in python command line.
But the result is the same.
SyntaxError: invalid syntax

What should I do to use openpyxl ?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2018-03-28 Thread Steven D'Aprano
On Wed, Mar 28, 2018 at 03:08:00PM +0900, naoki_morih...@softbank.ne.jp wrote:
> I want to install 3rd party module, ex openpyxl.
> And I executed the following command in windows command prompt as follows:
> pip install openpyxl
> But pip is not recognized as executable command at windows.

What version of Python are you using?

If you have Python 3.4 or better, or Python 2.7.9, you can say:

  python -m ensurepip

at the Windows command prompt to install pip. If there are no 
installation errors, then you can run 

  pip install openpyxl

at the Windows command prompt. No internet is needed for the first 
command, but for the second you will need internet access.

https://docs.python.org/3/library/ensurepip.html

On Windows, if you have trouble running the "python" command, it might 
be easier to use "py" instead:

https://docs.python.org/3/using/windows.html#from-the-command-line


-- 
Steve

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


Re: [Tutor] (no subject)

2018-03-28 Thread shubham goyal
Use this link to install python package and make sure to add the path in
environment variables (just click the option it asks when you run the
setup, Add Python to path or something).
Recent python packages include pip also so you will get your problem solved.

https://www.python.org/ftp/python/3.6.4/python-3.6.4-amd64.exe

In your case pip module is not installed or not added to windows
environment variables thats why its showing pip not recognized as a command.
do as mentioned and you should able to install whatever.

On Wed, Mar 28, 2018 at 11:38 AM,  wrote:

> I want to install 3rd party module, ex openpyxl.
> And I executed the following command in windows command prompt as follows:
> pip install openpyxl
> But pip is not recognized as executable command at windows.
> I also tried the same way in python command line.
> But the result is the same.
> SyntaxError: invalid syntax
>
> What should I do to use openpyxl ?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sentiment analysis read from a file

2018-03-28 Thread Alan Gauld via Tutor
On 28/03/18 11:07, theano orf wrote:
> I am new in python and I am having problems of how to read a txt file and
> insert the data in a list, 

Just a quick response, but your data is more than a text file its a CSV
file so the rules change slightly. Especially since you are using the csv
module.

Your data file is not a CSV file - it is just space separated and the
string is not quoted so the CSV default mode of operation won;t
work on this data as you seem to expect it to,. You will need to
specify the separator (as what? A space wiill split on each word...)
CSV might not be the best option here a simple string split combined
with slicing might be better.

> with open("training.txt", 'r') as file:

The CSV module prefers binary files so open it with mode 'rb' not 'r'

> reviews = list(csv.reader(file))

Try printing the first 2 lines of reviews to check what you have.
I suspect it's not what you think.

>positive_review = [r[1] for r in reviews if r[0] == str(1)]

str(1) is just '1' so you might as well just use that.

> after the print I only take an empty array. Why is this happening? I am
> attaching also the training.txt file

See the comments above about your data format.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Sentiment analysis read from a file

2018-03-28 Thread Peter Otten
Alan Gauld via Tutor wrote:

> On 28/03/18 11:07, theano orf wrote:
>> I am new in python and I am having problems of how to read a txt file and
>> insert the data in a list,
> 
> Just a quick response, but your data is more than a text file its a CSV
> file so the rules change slightly. Especially since you are using the csv
> module.
> 
> Your data file is not a CSV file - it is just space separated and the
> string is not quoted so the CSV default mode of operation won;t
> work on this data as you seem to expect it to,. You will need to
> specify the separator (as what? A space wiill split on each word...)
> CSV might not be the best option here a simple string split combined
> with slicing might be better.

>>> next(open("training.txt"))
'1\tThe Da Vinci Code book is just awesome.\n'

So the delimiter would be TAB:

>>> import csv
>>> next(csv.reader(open("training.txt"), delimiter="\t"))
['1', 'The Da Vinci Code book is just awesome.']

>> with open("training.txt", 'r') as file:
> 
> The CSV module prefers binary files so open it with mode 'rb' not 'r'

That's no longer true for Python 3:

>>> next(csv.reader(open("training.txt", "rb"), delimiter="\t"))
Traceback (most recent call last):
  File "", line 1, in 
_csv.Error: iterator should return strings, not bytes (did you open the file 
in text mode?)

However, as csv still does its own newline handling it's a good idea to get 
into the habit of opening the file with newline="" as explained here:

https://docs.python.org/dev/library/csv.html#id3

>> reviews = list(csv.reader(file))
> 
> Try printing the first 2 lines of reviews to check what you have.
> I suspect it's not what you think.
> 
>>positive_review = [r[1] for r in reviews if r[0] == str(1)]
> 
> str(1) is just '1' so you might as well just use that.
> 
>> after the print I only take an empty array. Why is this happening? I am
>> attaching also the training.txt file
> 
> See the comments above about your data format.
> 


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


Re: [Tutor] (no subject)

2018-03-28 Thread Mats Wichmann
On 03/28/2018 04:32 AM, Steven D'Aprano wrote:
> On Wed, Mar 28, 2018 at 03:08:00PM +0900, naoki_morih...@softbank.ne.jp wrote:
>> I want to install 3rd party module, ex openpyxl.
>> And I executed the following command in windows command prompt as follows:
>> pip install openpyxl
>> But pip is not recognized as executable command at windows.
> 
> What version of Python are you using?
> 
> If you have Python 3.4 or better, or Python 2.7.9, you can say:
> 
>   python -m ensurepip
> 
> at the Windows command prompt to install pip. If there are no 
> installation errors, then you can run 
> 
>   pip install openpyxl
> 
> at the Windows command prompt. No internet is needed for the first 
> command, but for the second you will need internet access.
> 
> https://docs.python.org/3/library/ensurepip.html
> 
> On Windows, if you have trouble running the "python" command, it might 
> be easier to use "py" instead:
> 
> https://docs.python.org/3/using/windows.html#from-the-command-line
> 
> 

I would add... modern Python on Windows includes pip, but pip is not in
the same directory as Python. So if you told the installer to add Python
to the path you could have something like (this is an example):

C:\Users\Foo\AppData\Local\Programs\Python\Python36-32

in your PATH, but pip is in the path

C:\Users\Foo\AppData\Local\Programs\Python\Python36-32\Tools

you can add the latter to your PATH as well; or, use

  python -m pip dosomething

in place of

  pip dosomething

That is, even if Windows doesn't have pip in its path, Python shguld
know how to find it as a module.



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


Re: [Tutor] Sentiment analysis read from a file

2018-03-28 Thread Rajesh Balel
seems you have "tab  separated data

with open('Training.txt') as f:
  my_data = [x.strip().split('\t') for x in f.readlines()]

for x in my_data: print x,

Regards
Rajesh


On Wed, Mar 28, 2018 at 10:14 AM, Peter Otten <__pete...@web.de> wrote:

> Alan Gauld via Tutor wrote:
>
> > On 28/03/18 11:07, theano orf wrote:
> >> I am new in python and I am having problems of how to read a txt file
> and
> >> insert the data in a list,
> >
> > Just a quick response, but your data is more than a text file its a CSV
> > file so the rules change slightly. Especially since you are using the csv
> > module.
> >
> > Your data file is not a CSV file - it is just space separated and the
> > string is not quoted so the CSV default mode of operation won;t
> > work on this data as you seem to expect it to,. You will need to
> > specify the separator (as what? A space wiill split on each word...)
> > CSV might not be the best option here a simple string split combined
> > with slicing might be better.
>
> >>> next(open("training.txt"))
> '1\tThe Da Vinci Code book is just awesome.\n'
>
> So the delimiter would be TAB:
>
> >>> import csv
> >>> next(csv.reader(open("training.txt"), delimiter="\t"))
> ['1', 'The Da Vinci Code book is just awesome.']
>
> >> with open("training.txt", 'r') as file:
> >
> > The CSV module prefers binary files so open it with mode 'rb' not 'r'
>
> That's no longer true for Python 3:
>
> >>> next(csv.reader(open("training.txt", "rb"), delimiter="\t"))
> Traceback (most recent call last):
>   File "", line 1, in 
> _csv.Error: iterator should return strings, not bytes (did you open the
> file
> in text mode?)
>
> However, as csv still does its own newline handling it's a good idea to get
> into the habit of opening the file with newline="" as explained here:
>
> https://docs.python.org/dev/library/csv.html#id3
>
> >> reviews = list(csv.reader(file))
> >
> > Try printing the first 2 lines of reviews to check what you have.
> > I suspect it's not what you think.
> >
> >>positive_review = [r[1] for r in reviews if r[0] == str(1)]
> >
> > str(1) is just '1' so you might as well just use that.
> >
> >> after the print I only take an empty array. Why is this happening? I am
> >> attaching also the training.txt file
> >
> > See the comments above about your data format.
> >
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sentiment analysis read from a file

2018-03-28 Thread Alan Gauld via Tutor
On 28/03/18 18:14, Peter Otten wrote:
>> Just a quick response, but your data is more than a text file its a CSV
 next(open("training.txt"))
> '1\tThe Da Vinci Code book is just awesome.\n'
>
> So the delimiter would be TAB:

Ah! On my screen it looked like a space...
> The CSV module prefers binary files so open it with mode 'rb' not 'r'
> That's no longer true for Python 3:

And i should have remembered that, I'm too used to using CSV on v2.

I did say it was a quick response, I didn't actually try anything :-)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

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


[Tutor] Pi approximation

2018-03-28 Thread Roger Lea Scherer
In one of my lessons I am asked to compare approximations for pi. I got
everything to work properly and my attempt is successful and matches
Python's approximation up to 15 digits to the right of the decimal, but I
suspect I can do this programmatically rather than the repetitious way I
did.

I tried "for i in range(10):"; then I tried "c += c" so it would be a sum.
Those attempts did not work. I tried math.fsum and though the documentation
says it is for iterables I could not get it to work as I desired. I
received an error that said TypeError: 'float' object is not iterable

I included all the code so I wouldn't neglect any you might need. Can you
help again?

Thank you.

# compare various approximations of pi
import math
import random

# simplest estimate
a = 22/7
print(a)

# next simplest
b = 355/113
print(b)

# from wikipedia:
# In 1910, the Indian mathematician Srinivasa Ramanujan found several
rapidly converging infinite series
c = (2*math.sqrt(2)/9801) * (((math.factorial(4*0))*(1103+26390*0)) /
((math.factorial(0)**4)*(396**(4*0
d = (2*math.sqrt(2)/9801) * (((math.factorial(4*1))*(1103+26390*1)) /
((math.factorial(1)**4)*(396**(4*1
e = (2*math.sqrt(2)/9801) * (((math.factorial(4*2))*(1103+26390*2)) /
((math.factorial(2)**4)*(396**(4*2
f = (2*math.sqrt(2)/9801) * (((math.factorial(4*3))*(1103+26390*3)) /
((math.factorial(3)**4)*(396**(4*3
g = (2*math.sqrt(2)/9801) * (((math.factorial(4*4))*(1103+26390*4)) /
((math.factorial(4)**4)*(396**(4*4
h = (2*math.sqrt(2)/9801) * (((math.factorial(4*5))*(1103+26390*5)) /
((math.factorial(5)**4)*(396**(4*5
i = (2*math.sqrt(2)/9801) * (((math.factorial(4*6))*(1103+26390*6)) /
((math.factorial(6)**4)*(396**(4*6
j = (2*math.sqrt(2)/9801) * (((math.factorial(4*7))*(1103+26390*7)) /
((math.factorial(7)**4)*(396**(4*7
k = (2*math.sqrt(2)/9801) * (((math.factorial(4*8))*(1103+26390*8)) /
((math.factorial(8)**4)*(396**(4*8
l = (2*math.sqrt(2)/9801) * (((math.factorial(4*9))*(1103+26390*9)) /
((math.factorial(9)**4)*(396**(4*9
m = c + d + e + f + g + h + i + j + k + l
print(1/m)

print(math.pi)


-- 
Roger Lea Scherer
623.255.7719
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pi approximation

2018-03-28 Thread boB Stepp
On Wed, Mar 28, 2018 at 2:09 PM, Roger Lea Scherer  wrote:
> In one of my lessons I am asked to compare approximations for pi. I got
> everything to work properly and my attempt is successful and matches
> Python's approximation up to 15 digits to the right of the decimal, but I
> suspect I can do this programmatically rather than the repetitious way I
> did.
>
> I tried "for i in range(10):"; then I tried "c += c" so it would be a sum.
> Those attempts did not work. I tried math.fsum and though the documentation
> says it is for iterables I could not get it to work as I desired. I
> received an error that said TypeError: 'float' object is not iterable
>
> I included all the code so I wouldn't neglect any you might need. Can you
> help again?

You are using the formula in the Wikipedia article, right?  Mimic in
Python what you are doing by manually.  Do something like:

pi_approx = 0.0
for k in range(10):
pi_approx += your formula from the Wikipedia article

print(pi_approx)

You might even want to make a function out of the above so that you
can try iterating over different ending values for k:

def calc_pi(loop_value):
pi_approx = 0.0
for k in range(loop_value):
pi_approx += your formula
return pi_approx

print(calc_pi(10))

You actually had all the pieces mentioned.  You just need to put them
together, looping just like you would do if you were calculating by
hand.

HTH!

boB

>
> # compare various approximations of pi
> import math
> import random
>
> # simplest estimate
> a = 22/7
> print(a)
>
> # next simplest
> b = 355/113
> print(b)
>
> # from wikipedia:
> # In 1910, the Indian mathematician Srinivasa Ramanujan found several
> rapidly converging infinite series
> c = (2*math.sqrt(2)/9801) * (((math.factorial(4*0))*(1103+26390*0)) /
> ((math.factorial(0)**4)*(396**(4*0
> d = (2*math.sqrt(2)/9801) * (((math.factorial(4*1))*(1103+26390*1)) /
> ((math.factorial(1)**4)*(396**(4*1
> e = (2*math.sqrt(2)/9801) * (((math.factorial(4*2))*(1103+26390*2)) /
> ((math.factorial(2)**4)*(396**(4*2
> f = (2*math.sqrt(2)/9801) * (((math.factorial(4*3))*(1103+26390*3)) /
> ((math.factorial(3)**4)*(396**(4*3
> g = (2*math.sqrt(2)/9801) * (((math.factorial(4*4))*(1103+26390*4)) /
> ((math.factorial(4)**4)*(396**(4*4
> h = (2*math.sqrt(2)/9801) * (((math.factorial(4*5))*(1103+26390*5)) /
> ((math.factorial(5)**4)*(396**(4*5
> i = (2*math.sqrt(2)/9801) * (((math.factorial(4*6))*(1103+26390*6)) /
> ((math.factorial(6)**4)*(396**(4*6
> j = (2*math.sqrt(2)/9801) * (((math.factorial(4*7))*(1103+26390*7)) /
> ((math.factorial(7)**4)*(396**(4*7
> k = (2*math.sqrt(2)/9801) * (((math.factorial(4*8))*(1103+26390*8)) /
> ((math.factorial(8)**4)*(396**(4*8
> l = (2*math.sqrt(2)/9801) * (((math.factorial(4*9))*(1103+26390*9)) /
> ((math.factorial(9)**4)*(396**(4*9
> m = c + d + e + f + g + h + i + j + k + l
> print(1/m)
>
> print(math.pi)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pi approximation

2018-03-28 Thread boB Stepp
I see I wrote the below a little too quickly!  Don't forget to take
the reciprocal when printing.  You might want to modify my naming of
variables to reflect this.  And return the reciprocal, which actually
gives the pi approximation in the function form.

On Wed, Mar 28, 2018 at 9:08 PM, boB Stepp  wrote:
> On Wed, Mar 28, 2018 at 2:09 PM, Roger Lea Scherer  wrote:
>> In one of my lessons I am asked to compare approximations for pi. I got
>> everything to work properly and my attempt is successful and matches
>> Python's approximation up to 15 digits to the right of the decimal, but I
>> suspect I can do this programmatically rather than the repetitious way I
>> did.
>>
>> I tried "for i in range(10):"; then I tried "c += c" so it would be a sum.
>> Those attempts did not work. I tried math.fsum and though the documentation
>> says it is for iterables I could not get it to work as I desired. I
>> received an error that said TypeError: 'float' object is not iterable
>>
>> I included all the code so I wouldn't neglect any you might need. Can you
>> help again?
>
> You are using the formula in the Wikipedia article, right?  Mimic in
> Python what you are doing by manually.  Do something like:
>
> pi_approx = 0.0
> for k in range(10):
> pi_approx += your formula from the Wikipedia article
>
> print(pi_approx)
>
> You might even want to make a function out of the above so that you
> can try iterating over different ending values for k:
>
> def calc_pi(loop_value):
> pi_approx = 0.0
> for k in range(loop_value):
> pi_approx += your formula
> return pi_approx
>
> print(calc_pi(10))
>
> You actually had all the pieces mentioned.  You just need to put them
> together, looping just like you would do if you were calculating by
> hand.
>
> HTH!
>
> boB
>
>>
>> # compare various approximations of pi
>> import math
>> import random
>>
>> # simplest estimate
>> a = 22/7
>> print(a)
>>
>> # next simplest
>> b = 355/113
>> print(b)
>>
>> # from wikipedia:
>> # In 1910, the Indian mathematician Srinivasa Ramanujan found several
>> rapidly converging infinite series
>> c = (2*math.sqrt(2)/9801) * (((math.factorial(4*0))*(1103+26390*0)) /
>> ((math.factorial(0)**4)*(396**(4*0
>> d = (2*math.sqrt(2)/9801) * (((math.factorial(4*1))*(1103+26390*1)) /
>> ((math.factorial(1)**4)*(396**(4*1
>> e = (2*math.sqrt(2)/9801) * (((math.factorial(4*2))*(1103+26390*2)) /
>> ((math.factorial(2)**4)*(396**(4*2
>> f = (2*math.sqrt(2)/9801) * (((math.factorial(4*3))*(1103+26390*3)) /
>> ((math.factorial(3)**4)*(396**(4*3
>> g = (2*math.sqrt(2)/9801) * (((math.factorial(4*4))*(1103+26390*4)) /
>> ((math.factorial(4)**4)*(396**(4*4
>> h = (2*math.sqrt(2)/9801) * (((math.factorial(4*5))*(1103+26390*5)) /
>> ((math.factorial(5)**4)*(396**(4*5
>> i = (2*math.sqrt(2)/9801) * (((math.factorial(4*6))*(1103+26390*6)) /
>> ((math.factorial(6)**4)*(396**(4*6
>> j = (2*math.sqrt(2)/9801) * (((math.factorial(4*7))*(1103+26390*7)) /
>> ((math.factorial(7)**4)*(396**(4*7
>> k = (2*math.sqrt(2)/9801) * (((math.factorial(4*8))*(1103+26390*8)) /
>> ((math.factorial(8)**4)*(396**(4*8
>> l = (2*math.sqrt(2)/9801) * (((math.factorial(4*9))*(1103+26390*9)) /
>> ((math.factorial(9)**4)*(396**(4*9
>> m = c + d + e + f + g + h + i + j + k + l
>> print(1/m)
>>
>> print(math.pi)



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