Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 455
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 1
Kenil Vasani
Asked: December 11, 20202020-12-11T20:37:36+00:00 2020-12-11T20:37:36+00:00In: Python

TypeError: Mismatch between array dtype (‘object’) and format specifier (‘%.18e’)

  • 1

I have the following array:

X = np.array([image_array_to_vector1,image_array_to_vector2,image_array_to_vector3,image_array_to_vector4])

A print out of X looks as follows:

[array([167, 167, 169, ...,   1,   1,   1], dtype=uint8)
 array([42, 43, 43, ..., 41, 36, 34], dtype=uint8)
 array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)
 array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)]

When I try to save the data as txt:

X_to_text_file = np.savetxt('x.txt',X)

I get the following:

File "/Library/Python/2.7/site-packages/numpy/lib/npyio.py", line 1258, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')

Why is that? How can I solve the issue?

Thanks.

arraysnumpypythonsavetext
  • 1 1 Answer
  • 11 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Rohit Patel

      Rohit Patel

      • 0 Questions
      • 98 Answers
      • 0 Best Answers
      • 0 Points
      View Profile
      Best Answer
      Rohit Patel
      2020-12-11T20:35:26+00:00Added an answer on December 11, 2020 at 8:35 pm

      It’s a little difficult to duplicate this without some example data, but here is what I came up with.

      arr = np.array([np.array([1,2,3]), np.array([1,2,3,4])])
      arr
      array([array([1, 2, 3]), array([1, 2, 3, 4])], dtype=object)
      np.savetxt('x.txt', arr)
      TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')
      

      As pointed out by @Artier there is a little snippet at the end of the accepted answer in Write object array to .txt file
      that points out you can just save the array as a string with fmt='%s'. Using this format seems to solve the problem for me (again I can’t recreate your issue exactly without a sample of data).

      np.savetxt('x.txt', arr, fmt='%s')
      

      I would point out that if you are looking to save disparate arrays and want a single location to keep them savez is very useful.

      • 8
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      Unable to resolve dependency tree error when installing npm packages

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com