2

I have a .txt file made like this:

(12,13,14,15)
(1,2,4,5)
(1,2,3,4,5)

and so on. I would like to store the file into a numpy array of shape (N,4), discarding those tuples which have more than 4 elements. I've tried with np.genfromtxt('filename.txt', delimiter=',', invalid_raise=False) but I get NaN for the first and the last term of each line due to the presence of '(' and ')'. How can I solve it?

emm gi
  • 45
  • 6
  • that's not `csv` format. Use basic python to get a list of list, and make the array from that. – hpaulj Apr 02 '21 at 08:13

3 Answers3

1

there is a simple in built library used to get tuple from string.

from ast import literal_eval as make_tuple
tuple_list=[]
a=open("try.txt","r").readlines()
for i in a:
    if i!="":
        temp=make_tuple(i)
        if len(temp)<=4:
            tuple_list.append(temp)
            
print(tuple_list)

try.txt consists of your tuples and final result is a list of all the tuples with length less than equal to 4.

output:

[(12, 13, 14, 15), (1, 2, 4, 5)]

Ref: Parse a tuple from a string?

Yash
  • 1,271
  • 1
  • 7
  • 9
0

You first need to remove the curved braces which can be achieved with the strip method of str:

"(1,2,3,4)".strip("()")
# "1,2,3,4"

Here is a working solution:

import numpy as np 

with open("filename.txt") as f:
  data = f.readlines()
  data = [line.strip("()\n").split(",") for line in data]
  array = np.array(data).astype(int)

Louis Lac
  • 5,298
  • 1
  • 21
  • 36
0

Try below solution:

import numpy as np
#Read and split files with new line character
with open("temp.txt","r") as f:
    temp = f.read().split("\n")
# convert tuple stored as string to tuple whoes length = 4
temp = [eval(elements) for elements in temp if len(eval(elements))==4]
# convert list to numpy array of size (N,4)
arr=np.array(temp)
Hetal Thaker
  • 717
  • 5
  • 12