Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error due to version
#1
I found this python code to read data and create GMSH .geo file. However, when I use it i get an error

# script to create a .geo file for a multi element airfoil
# Read the coordinates to two lists
mainCoords = []
flapCoords = []
with open('gaw.txt') as fin:
    fin.readline() # skip header
    fin.readline() # skip header
    for l in fin:
        coords = map(float,l.split())
        print('coords')
        mainCoords.append(coords[:2])
        if len(coords) > 2:
            flapCoords.append(coords[2:])

# Create a GMSH file and write the points in GMSH format
with open('multi.geo','w') as fout:
    for i, c in enumerate(mainCoords):
        fout.write('Point (%i) = {%f, %f, 0, 1};\n'%(i+1,c[0],c[1]))
    for i, c in enumerate(flapCoords):
        fout.write('Point (%i) = {%f, %f, 0, 1};\n'%(i+len(mainCoords)+1,c[0],c[1]))

# Create two splines 
    fout.write('Spline (1) = {%s};\n' % ','.join(map(str,range(1,len(mainCoords)+1))))
    fout.write('Spline (2) = {%s};\n' % ','.join(map(str,range(len(mainCoords)+1,len(mainCoords)+len(flapCoords)+1))))
I get the following error :
Error:
runfile('D:/AERO_AUTO/CFD_HKN/gmsh-4.3.0-Windows64/hkn/Multi_element.py', wdir='D:/AERO_AUTO/CFD_HKN/gmsh-4.3.0-Windows64/hkn') coords Traceback (most recent call last): File "<ipython-input-5-d8984351fb2d>", line 1, in <module> runfile('D:/AERO_AUTO/CFD_HKN/gmsh-4.3.0-Windows64/hkn/Multi_element.py', wdir='D:/AERO_AUTO/CFD_HKN/gmsh-4.3.0-Windows64/hkn') File "E:\aconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile execfile(filename, namespace) File "E:\aconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "D:/AERO_AUTO/CFD_HKN/gmsh-4.3.0-Windows64/hkn/Multi_element.py", line 11, in <module> mainCoords.append(coords[:2]) TypeError: 'map' object is not subscriptable
I think the error is due to python version (i use 3., but not sure Any help will be greatly helpful.
Thanks
Narahari
Reply
#2
you are right
the simplest solution is
        coords = list(map(float,l.split()))
another option
coords = [float(n) for n in l.split()]
also you can always use 2to3 tool to auto convert the script to py3
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You can't slice a map object (i.e. coords[:2]) because it's not a normal sequence, but basically a generator. If you really want to do that, then you'll need to consume that generator (i.e. go through it and get all the items). You can do that by passing it to, say, the list function that will create a list from the items:

>>> l1 = [1, 2, 3]                                                                                       
>>> m = map(lambda x: x * 2, l1)  
>>> m
<map object at 0x7ff814caac50>                                                                                          
>>> l2 = list(m)
>>> l2
[2, 4, 6]
>>> 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on Python Version? ErnestTBass 6 3,490 Dec-09-2020, 04:02 PM
Last Post: ErnestTBass
  switch from version 3.5.3 to 3.8.6 get import error python001 9 3,962 Oct-31-2020, 03:02 PM
Last Post: python001
  Error while trying to see Python version with "sys.executable" karkas 5 5,978 Feb-09-2020, 01:55 AM
Last Post: karkas
  Can I upload a new version without previously deleting ancient version sylas 6 4,240 Nov-08-2017, 03:26 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020