Re: [Tutor] Index Out of Range?List

2017-05-04 Thread Mats Wichmann

> atm_chg.append(float( line.split()[-1] ))
> 
> 
> np.asarray({atm_chg})
> 
> Execution still generates the errors:
> 
> runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py',
> wdir='/home/comp/Apps/Python/Testing')

that means you have a blank line it's reading, the result of splitting
an empty line is an empty list, which you can't index since it has no
members.

$ python
Python 2.7.13 (default, Jan 12 2017, 17:59:37)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ''.split()
[]
>>> ''.split()[-1]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: list index out of range
>>>


you're going to need to do a little input validation, always a good idea
anyway.  that is inside this loop:

  for line in f.readlines():
  atm_chg.append(float( line.split()[-1] ))

check there's something usable in line before you split-and-use it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Index Out of Range?List

2017-05-04 Thread Alan Gauld via Tutor
On 04/05/17 13:50, Stephen P. Molnar wrote:
>> I'll assume that's where the problem lies.
>> Try checking if the string is not empty before using it:
>>
>>   for line in f.readlines():
>>   if line:
>>  atm_chg.append(float( line.split()[-1] ))

> I have edited the code:
> 
>  for line in f.readlines():
>  atm_chg.append(float( line.split()[-1] ))

> Execution still generates the errors:

That's because you didn't include the change that
I thought might fix the error. See above.


-- 
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] Index Out of Range?List

2017-05-04 Thread Stephen P. Molnar

On 05/03/2017 08:51 PM, Alan Gauld via Tutor wrote:

On 04/05/17 00:32, Stephen P. Molnar wrote:


import numpy as np

name = input("Enter Molecule ID: ")
name = str(name)


You don't need the str(), input always returns a string.


name_in =name[:]+'.lac.dat'


And you don't need the [:]. Just use

name_in = name + '.lac.dat'


print(name_in)

atm_chg = []



"""
atm_chg = open(name_in,'r')
for line in atm_chg:
  print(line, end=' ')
"""

This creates a 3 line string which is not assigned to any object.
It is not executable code and will not be executed. Maybe you
are doing it as a way of commenting out a block? If so it would be
better in a post to just delete it, it just adds confusion
otherwise. (Well, it confused me! :-)


with open(name_in) as f:
  # skip two lines
  f.readline()
  f.readline()
  for line in f.readlines():
  atm_chg.append(float( line.split()[-1] ))

p.asarray({atm_chg})


Where did p come from?
Should it be np? asarray() sounds like it might be a numpy thing.
And I'm not sure what the {atm_chg} is supposed to do - create
a single element set using your list maybe? I get an "unhashable"
error if I try it at the  prompt.


When it is run I get:

IndexError: list index out of range


I'm pretty sure you get more than that, please post the full
error text, it's much harder to diagnose problems with just
the summary.

Since the only indexing you do is in this line


  atm_chg.append(float( line.split()[-1] ))


I'll assume that's where the problem lies.
Try checking if the string is not empty before using it:

  for line in f.readlines():
  if line:
 atm_chg.append(float( line.split()[-1] ))



However, the Variable Explorer shows:


I have no idea what the Variable Explorer is?
Is it part of your IDE? Or of numpy?
If the IDE which IDE are you using?


   [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]

One line of which is exactly what I want as input to the next step in
the larger calculation.

Now, my question is how do I extract just one line of this file?


Any particular line? And which file are you talking about?
The data should be in the list variable, atm_chg.
In which case the first line is therefore:  atm_chg[0]

Or you can process each line using the usual for loop:

for line in atm_chg:
 # use line here


Thanks for your reply.

The Variable Explorer is part of the Spyder IDE.

I have edited the code:

import numpy as np


name = input("Enter Molecule ID: ")
#name = str(name)

name_in =name[:]+'.lac.dat'
print(name_in)

atm_chg = []

with open(name_in) as f:
# skip two lines
f.readline()
f.readline()
for line in f.readlines():
atm_chg.append(float( line.split()[-1] ))


np.asarray({atm_chg})

Execution still generates the errors:

runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', 
wdir='/home/comp/Apps/Python/Testing')


Enter Molecule ID: A
A.lac.dat
Traceback (most recent call last):

  File "", line 1, in 
runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', 
wdir='/home/comp/Apps/Python/Testing')


  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 866, in runfile

execfile(filename, namespace)

  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 102, in execfile

exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/comp/Apps/Python/Testing/ReadFile_2.py", line 27, in 
atm_chg.append(float( line.split()[-1] ))

IndexError: list index out of range

from the input file:

runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', 
wdir='/home/comp/Apps/Python/Testing')


Enter Molecule ID: A
A.lac.dat
Traceback (most recent call last):

  File "", line 1, in 
runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', 
wdir='/home/comp/Apps/Python/Testing')


  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 866, in runfile

execfile(filename, namespace)

  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 102, in execfile

exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/comp/Apps/Python/Testing/ReadFile_2.py", line 27, in 
atm_chg.append(float( line.split()[-1] ))

IndexError: list index out of range

Finally, here is the code for which I need input data for the 
calculation of the Integrated Charge Transform:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  8 15:17:07 2017

@author: comp

Copyright (c) 2017 Stephen P. Molnar, Ph.D.

"""

import matplotlib.pyplot as plt
import numpy as np
# you don't need pandas, np.genfromtxt() reads this type of txt files.

plt.ion() # interactive plotting, stopps plt.show() 

Re: [Tutor] Index Out of Range?List

2017-05-04 Thread Alan Gauld via Tutor
On 04/05/17 13:21, Stephen P. Molnar wrote:

> Here are the error messages:
> 
> Enter Molecule ID: A
> A.lac.dat
> Traceback (most recent call last):
> 
>File "", line 1, in 
>  runfile('/home/comp/Apps/Python/Testing/ReadFile_1.py', 
> wdir='/home/comp/Apps/Python/Testing')

There appears to be a missing opening paren? Or a surplus closing one?
Either way this line is not in the code you sent us. It may be coming
from your IDE - which seems to be spyder.

Can you run the code from a command prompt so we see the unpolluted
Python output. IDEs are useful but they sometimes add in extra layers
that obfuscate things.

> "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py",
>  
> line 102, in execfile
>  exec(compile(f.read(), filename, 'exec'), namespace)
> 
>File "/home/comp/Apps/Python/Testing/ReadFile_1.py", line 32, in 
>  atm_chg.append(float( line.split()[-1] ))
> 
> IndexError: list index out of range

But it does look like it's this line that causes the error
so I'd try the check for emptiness as described in my previous post.


-- 
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] Index Out of Range?List

2017-05-04 Thread Stephen P. Molnar

On 05/03/2017 08:32 PM, Steven D'Aprano wrote:

On Wed, May 03, 2017 at 07:32:53PM -0400, Stephen P. Molnar wrote:

[...]

When it is run I get:

IndexError: list index out of range


That alone is useless to us. Please post the full traceback, starting
from the line

 Traceback (most recent call last):

Among other things, it will show us the line of code that causes the
error. Otherwise, we're just guessing.

You might also like to consider a simple debugging technique: print
the index and the length of the list just before trying to use them.
E.g. if you want to extract the nth item of a list, write:

 print(n, len(a_list))
 value = a_list[n]


(And yes, I completely agree that the error message should show that
information, but it currently doesn't.)



Thanks for the reply.

Here are the error messages:

Enter Molecule ID: A
A.lac.dat
Traceback (most recent call last):

  File "", line 1, in 
runfile('/home/comp/Apps/Python/Testing/ReadFile_1.py', 
wdir='/home/comp/Apps/Python/Testing')


  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 866, in runfile

execfile(filename, namespace)

  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 102, in execfile

exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/comp/Apps/Python/Testing/ReadFile_1.py", line 32, in 
atm_chg.append(float( line.split()[-1] ))

IndexError: list index out of range

--
Stephen P. Molnar, Ph.D.Life is a fuzzy set
www.molecular-modeling.net  Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Index Out of Range?List

2017-05-03 Thread Alan Gauld via Tutor
On 04/05/17 00:32, Stephen P. Molnar wrote:

> import numpy as np
> 
> name = input("Enter Molecule ID: ")
> name = str(name)

You don't need the str(), input always returns a string.

> name_in =name[:]+'.lac.dat'

And you don't need the [:]. Just use

name_in = name + '.lac.dat'

> print(name_in)
> 
> atm_chg = []

> """
> atm_chg = open(name_in,'r')
> for line in atm_chg:
>  print(line, end=' ')
> """
This creates a 3 line string which is not assigned to any object.
It is not executable code and will not be executed. Maybe you
are doing it as a way of commenting out a block? If so it would be
better in a post to just delete it, it just adds confusion
otherwise. (Well, it confused me! :-)

> with open(name_in) as f:
>  # skip two lines
>  f.readline()
>  f.readline()
>  for line in f.readlines():
>  atm_chg.append(float( line.split()[-1] ))
> 
> p.asarray({atm_chg})

Where did p come from?
Should it be np? asarray() sounds like it might be a numpy thing.
And I'm not sure what the {atm_chg} is supposed to do - create
a single element set using your list maybe? I get an "unhashable"
error if I try it at the  prompt.

> When it is run I get:
> 
> IndexError: list index out of range

I'm pretty sure you get more than that, please post the full
error text, it's much harder to diagnose problems with just
the summary.

Since the only indexing you do is in this line

>  atm_chg.append(float( line.split()[-1] ))

I'll assume that's where the problem lies.
Try checking if the string is not empty before using it:

 for line in f.readlines():
 if line:
atm_chg.append(float( line.split()[-1] ))


> However, the Variable Explorer shows:

I have no idea what the Variable Explorer is?
Is it part of your IDE? Or of numpy?
If the IDE which IDE are you using?

>   [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
> 
> One line of which is exactly what I want as input to the next step in 
> the larger calculation.
> 
> Now, my question is how do I extract just one line of this file?

Any particular line? And which file are you talking about?
The data should be in the list variable, atm_chg.
In which case the first line is therefore:  atm_chg[0]

Or you can process each line using the usual for loop:

for line in atm_chg:
# use line here

-- 
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] Index Out of Range?List

2017-05-03 Thread Steven D'Aprano
On Wed, May 03, 2017 at 07:32:53PM -0400, Stephen P. Molnar wrote:

[...]
> When it is run I get:
> 
> IndexError: list index out of range

That alone is useless to us. Please post the full traceback, starting 
from the line 

Traceback (most recent call last):

Among other things, it will show us the line of code that causes the 
error. Otherwise, we're just guessing.

You might also like to consider a simple debugging technique: print 
the index and the length of the list just before trying to use them. 
E.g. if you want to extract the nth item of a list, write:

print(n, len(a_list))
value = a_list[n]


(And yes, I completely agree that the error message should show that 
information, but it currently doesn't.)


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


[Tutor] Index Out of Range?List

2017-05-03 Thread Stephen P. Molnar
I have a section of Python 3 code that is intended to be part of a 
larger program which essentially inputs the file:


LOEWDIN ATOMIC CHARGES
--
   0 C :   -0.780631
   1 H :0.114577
   2 Br:0.309802
   3 Cl:0.357316
   4 F :   -0.001065

The code is:

import numpy as np


name = input("Enter Molecule ID: ")
name = str(name)

name_in =name[:]+'.lac.dat'
print(name_in)

atm_chg = []
"""
atm_chg = open(name_in,'r')
for line in atm_chg:
print(line, end=' ')
"""
#all_contents = []
with open(name_in) as f:
# skip two lines
f.readline()
f.readline()
for line in f.readlines():
atm_chg.append(float( line.split()[-1] ))

p.asarray({atm_chg})

When it is run I get:

IndexError: list index out of range

However, the Variable Explorer shows:

 [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]

One line of which is exactly what I want as input to the next step in 
the larger calculation.


Now, my question is how do I extract just one line of this file?

Thanks in advance.

--
Stephen P. Molnar, Ph.D.Life is a fuzzy set
www.molecular-modeling.net  Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor