Issues when trying to obtain a numpy array from concatenate text files

Issue

I have the following code:

filenames = ['proximityzones_snap000.txt', 'proximityzones_snap001.txt', 'proximityzones_snap002.txt']
with open('output_zones.txt', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

Here I just concatenate two .txt files into one. The result is:

['1.546874999999991633e+04 4.406249999999975262e+04 7.812500000000000000e+01 2.171874999999995634e+04 3.359375000000010186e+04 2.906250000000005093e+04 3.148437500000007640e+04 2.820312500000004002e+04 3.585937500000004366e+04 3.796874999999997090e+04 3.257812500000008731e+04 3.812499999999996362e+04 3.210937500000008367e+04 3.835937499999995634e+04 4.046874999999987631e+04 2.179687499999995634e+04 4.281249999999979627e+04 3.296875000000009459e+04 2.187499999999995998e+04 3.156250000000007640e+04 2.093749999999994543e+04 2.671875000000002183e+04 3.281250000000009459e+04 3.640625000000002910e+04 4.562499999999969441e+04 3.000000000000005457e+04 3.843749999999994907e+04 3.171875000000007640e+04 2.203124999999995998e+04 2.656250000000001819e+04 3.492187500000008004e+04 4.062500000000000455e+03 2.039062499999994179e+04 3.234375000000008731e+04 2.984375000000005457e+04 2.062499999999994179e+04 2.265624999999996726e+04 3.390625000000010186e+04 3.085937500000006548e+04 2.710937500000002547e+04 4.093749999999986176e+04 4.570312499999968713e+04 3.148437500000007640e+04 2.101562499999994907e+04 1.734374999999990541e+04 3.648437500000002183e+04 2.781250000000003274e+04 2.343749999999997817e+04 2.187499999999995998e+04 2.054687499999994179e+04 2.171874999999995634e+04 3.320312500000009459e+04 3.195312500000008004e+04 3.296875000000009459e+04 2.492187499999999636e+04 3.320312500000009459e+04 2.445312499999999272e+04 1.374999999999993997e+04 3.359375000000010186e+04 3.023437500000005821e+04 3.921874999999991996e+04 3.773437499999997817e+04 2.687500000000002183e+04 2.750000000000002910e+04 3.132812500000007276e+04 3.257812500000008731e+04 3.789062499999997090e+04 2.132812499999995271e+04 4.367187499999975989e+04 2.687500000000002183e+04 3.125000000000007276e+04 2.140624999999995271e+04 4.234374999999981083e+04 2.296874999999997453e+04 3.296875000000009459e+04 3.320312500000009459e+04 3.218750000000008367e+04 3.843749999999994907e+04 2.789062500000003638e+04 3.226562500000008367e+04 3.804687499999996362e+04 1.679687499999990177e+04 2.265624999999996726e+04 3.296875000000009459e+04 3.671875000000001455e+04 1.718749999999990177e+04 4.476562499999972351e+04 1.992187499999993452e+04 3.460937500000008731e+04 2.460937499999999272e+04 3.289062500000009459e+04 3.156250000000007640e+04 2.484374999999999636e+04 2.734375000000002910e+04 3.328125000000009459e+04 3.828124999999995634e+04 3.796874999999997090e+04 4.320312499999977445e+04 3.312500000000009459e+04 1.851562499999991633e+04 1.851562499999991633e+04\n',
 '4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04\n']

I want to transform this result.txt into float and use every number as an element of an array. My problem is that it fails when converting the data into float so to ‘clean’ the data from spaces, \n and "" I tried the following:

with open('output_zones.txt') as f:
    contents = f.readlines()
    s=''.join(map(str, contents))
    s[1].strip("\'")
    data = s.replace('\n',"")
    data=np.array(string)

This way, I obtain:

array('9.374999999999990905e+03 1.124999999999996726e+04 7.812500000000000000e+01 1.117187499999996908e+04 9.531249999999987267e+03 9.921874999999983629e+03 9.453124999999989086e+03 8.828124999999998181e+03 9.609374999999987267e+03 1.062499999999997635e+04 1.023437499999997999e+04 1.015624999999997999e+04 7.812500000000000909e+03 1.093749999999997272e+04 1.070312499999997453e+04 1.187499999999995998e+04 9.140624999999992724e+03 9.453124999999989086e+03 9.218749999999992724e+03 9.921874999999983629e+03 8.515625000000000000e+03 9.140624999999992724e+03 9.374999999999990905e+03 1.101562499999997090e+04 9.999999999999983629e+03 8.749999999999998181e+03 9.453124999999989086e+03 9.609374999999987267e+03 1.218749999999995816e+04 1.132812499999996726e+04 9.609374999999987267e+03 3.750000000000000000e+03 1.039062499999997817e+04 9.531249999999987267e+03 9.453124999999989086e+03 1.124999999999996726e+04 5.859375000000000000e+03 1.195312499999995998e+04 4.687500000000000000e+03 1.062499999999997635e+04 9.296874999999990905e+03 1.171874999999996362e+04 8.671874999999998181e+03 1.187499999999995998e+04 1.054687499999997635e+04 4.296875000000000000e+03 3.828125000000000455e+03 9.218749999999992724e+03 1.171874999999996362e+04 9.140624999999992724e+03 1.007812499999998181e+04 1.156249999999996544e+04 8.749999999999998181e+03 1.062499999999997635e+04 7.500000000000000000e+03 1.078124999999997272e+04 1.085937499999997272e+04 1.054687499999997635e+04 1.054687499999997635e+04 1.109374999999997090e+04 9.453124999999989086e+03 9.687499999999987267e+03 1.093749999999997272e+04 9.687499999999987267e+03 7.421875000000000909e+03 8.906249999999996362e+03 6.093750000000000000e+03 1.171874999999996362e+04 8.359375000000001819e+03 6.875000000000000909e+03 1.132812499999996726e+04 9.687499999999987267e+03 7.343750000000000909e+03 1.101562499999997090e+04 1.203124999999995816e+04 1.203124999999995816e+04 1.164062499999996362e+04 1.070312499999997453e+04 9.765624999999985448e+03 9.921874999999983629e+03 9.531249999999987267e+03 9.999999999999983629e+03 9.765624999999985448e+03 8.984374999999994543e+03 1.109374999999997090e+04 8.828124999999998181e+03 9.999999999999983629e+03 1.085937499999997272e+04 1.023437499999997999e+04 1.023437499999997999e+04 7.890625000000000909e+03 1.023437499999997999e+04 1.039062499999997817e+04 1.093749999999997272e+04 8.984374999999994543e+03 1.039062499999997817e+04 1.132812499999996726e+04 9.843749999999985448e+03 8.749999999999998181e+03
.
.
.
4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04 4.039062499999988358e+04', dtype='<U7572')

But I still have the same problem because when I check the size of the array, it gives me 1. On the other hand, when I want to convert it into float, it pops up an error:

ValueError: could not convert string to float:'9.374999999999990905e+03 1.124999999999996726e+04 7.812500000000000000e+01 1.117187499999996908e+04 9.531249999999987267e+03 9.921874999999983629e+03 9.453124999999989086e+03 8.828124999999998181e+03 9.609374999999987267e+03 1.062499999999997635e+04...

Does anybody know how to fix this? I would appreciate it. (Btw, I’m still a noob in python)

Solution

You can use numpy.loadtxt

import numpy as np

with open('data.txt') as fp:
  lines = (x for line in fp for x in line.split())
  arr = np.loadtxt(lines)

Answered By – deadshot

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published