numpy.savetxt tuple index out of range?

2024/10/4 17:28:12

I'm trying to write a few lines into a text file, and here's the code I used:

import numpy as np# Generate some test data
data = np.arange(0.0,1000.0,50.0)with file('test.txt', 'w') as outfile:      outfile.write('# something')for data_slice in data: np.savetxt(outfile, data_slice, fmt='%1.4e')outfile.write('# New slice\n')

When the code runs up to the line with savetxt, I get this error:

     IndexError: tuple index out of range

Any idea why this happens? I tried removing the "fmt" part, but I get the same thing.

Answer

the problem is that numpy.save expect an array with some shape information, while you pass it just a number.

if you want to pass one element at the time (but I suggest you to save the whole array) you have to convert it first to a numpy array with a shape of at least one

np.savetxt(outfile, array(data_slice).reshape(1,), fmt='%1.4e')

this is because the shape of a single number is a void tuple, and to write to file it try to split along the first dimension

array(1).shape == tuple()
#True

to save the whole array it is sufficient to do:

np.savetxt(outfile, data, fmt='%1.4e')
https://en.xdnf.cn/q/70584.html

Related Q&A

Retrieve list of USB items using Python

How can I retrieve the items plugged into the computer through USB using python? Ive searched around on here and found some old examples which dont appear to work anymore as they are over 5 years old.…

History across ipdb sessions

This question has been asked before, but I couldnt find a good answer. So, I am trying to ask again.I would like my ipdb to remember commands across sessions. Right now, it can pull up commands execute…

Python Distributed Computing (works)

Im using an old thread to post new code which attempts to solve the same problem. What constitutes a secure pickle? this?sock.pyfrom socket import socket from socket import AF_INET from socket import…

Django - Stream request from external site as received

How can Django be used to fetch data from an external API, triggered by a user request, and stream it directly back in the request cycle without (or with progressive/minimal) memory usage?BackgroundAs…

django rest framework - always INSERTs, never UPDATES

I want to be able to UPDATE a user record by POST. However, the id is always NULL. Even if I pass the id it seems to be ignoredView Code:JSON POSTED:{"id": 1, "name": "Craig Ch…

Copy fields from one instance to another in Django

I have the following code which takes an existing instance and copies, or archives it, in another model and then deletes it replacing it with the draft copy. Current Codedef archive_calc(self, rev_num,…

Python selenium get Developer Tools →Network→Media logs

I am trying to programmatically do something that necessarily involves getting the "developer tools"→network→media logs. I will spare you the details, long story short, I need to visit thou…

deep copy nested iterable (or improved itertools.tee for iterable of iterables)

PrefaceI have a test where Im working with nested iterables (by nested iterable I mean iterable with only iterables as elements). As a test cascade considerfrom itertools import tee from typing import …

ImportError: No module named cv2.cv

python 3.5 and windows 10I installed open cv using this command :pip install opencv_python-3.1.0-cp35-cp35m-win_amd64.whlThis command in python works fine :import cv2But when i want to import cv2.cv :i…

Why arent persistent connections supported by URLLib2?

After scanning the urllib2 source, it seems that connections are automatically closed even if you do specify keep-alive. Why is this?As it is now I just use httplib for my persistent connections... bu…