Toykan Ozdeger
Toykan Ozdeger

Reputation: 169

Add integer to an array in Python

I am trying to add an integer to an array but I am getting an error. Here is my code:

import numpy as np
import h5py

for i in range(1, 621):
    with h5py.File("C:\\A" + str(i) + ".out") as f:
        data = np.array(f['rxs']['rx1']['Ey'])
        data.append(0)
    np.savetxt("C:\\A" + str(i) + ".csv", data, delimiter = ",")

For this I keep getting an error saying: "AttributeError: 'numpy.ndarray' object has no attribute 'append'"

I have also tried concatenate with an array consisting just 1 integer but it doesn't work. I used these lines for that:

data = np.array(f['rxs']['rx1']['Ey'])
b = np.array([[0]])
np.concatenate(data, b)

I get this error for this one: "TypeError: only integer scalar arrays can be converted to a scalar index"

The original aim of my code is to convert HDF files to CSV files which works if I don't try to change the array.

Could you please help?

Upvotes: 3

Views: 15298

Answers (1)

Edgar H
Edgar H

Reputation: 1518

You are not dealing with a python list but a numpy array.

To solve the problem at hand you can use numpy.append

data = np.append(data, 0)

You can also not create a numpy array to begin with. What is the type of f['rxs']['rx1']['Ey']? (You can find out with print(type(f['rxs']['rx1']['Ey'])))

if it is a list, you can simply do

data = f['rxs']['rx1']['Ey']
data.append(0)

Upvotes: 4

Related Questions