ValueError: invalid literal for int() with base 10 when using meshconvert.py

Hi, I get the following error when I run the command:

dolfin-convert mymesh.msh mymesh.xml

Traceback (most recent call last):
  File "/usr/bin/dolfin-convert", line 132, in <module>
    main(sys.argv[1:])
  File "/usr/bin/dolfin-convert", line 79, in main
    meshconvert.convert2xml(ifilename, ofilename, iformat=iformat)
  File "/usr/lib/python3/dist-packages/dolfin_utils/meshconvert/meshconvert.py", line 1301, in convert2xml
    convert(ifilename, XmlHandler(ofilename), iformat=iformat)
  File "/usr/lib/python3/dist-packages/dolfin_utils/meshconvert/meshconvert.py", line 1322, in convert
    gmsh2xml(ifilename, handler)
  File "/usr/lib/python3/dist-packages/dolfin_utils/meshconvert/meshconvert.py", line 271, in gmsh2xml
    num_elements = int(line)
ValueError: invalid literal for int() with base 10: '7 5905 1 5905\n'

I am using ubuntu 18.04 and installed FEniCS by the command

sudo apt-get install --no-install-recommends fenics

My fenics version is (1:2019.1.0.1~ppa1~bionic1).

I am using a geometry and a mesh that was taken from tutorial on the web and was converted succesfully to dolphin .xml format so I do not have made any mistake when creating the geo files.

I have solved this problem by installing Gmsh using apt-get in Ubuntu instead of using the binaries downloaded from the Gmsh page, and generating the mesh this way.

The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that’s not an integer to the int() function . In other words it’s either empty, or has a character in it other than a digit.

You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

if val.isdigit():

The other way to overcome this issue is to wrap your code inside a Python try…except block to handle this error.

Python2.x and Python3.x

Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: invalid literal for int() with base 10 .

With Python2.x , int(str(3/2)) gives you “1”. With Python3.x , the same gives you (“1.5”): ValueError: invalid literal for int() with base 10: “1.5”.

I had the same problem and solved it by downloading an earlier version of gmsh (3.0.6) and produced the mesh that way. Then dolfin-convert can handle it.

Instead of installing an older version of Gmsh, you may save your mesh as Gmsh MSH (.msh) file with Version 2 ASCII format option (it appears as you are saving the file in Gmsh). Then, you can use “dolfin-convert themesh.msh themesh.xml”. It worked for me.

1 Like

@scengizci Thank you! Saving in Version 2 ASCII format works for me, too.

This resolved my issue as well. I will just state the command line option for building the .msh file from the .geo geometry file in the format you have specified.

gmsh -3 my3dmesh.geo -format msh2