如何读取元组文件?

How to read a file of tuples?

我有一个这样制作的 .txt 文件:

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

等等。我想将文件存储到形状为 (N,4) 的 numpy 数组中,丢弃那些元素超过 4 个的元组。 我试过 np.genfromtxt('filename.txt', delimiter=',', invalid_raise=False) 但由于存在“(”和“)”,我在每行的第一个和最后一个术语中得到 NaN。我该如何解决?

您首先需要删除曲括号,这可以通过 str 的 strip 方法实现:

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

这是一个可行的解决方案:

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)

有一个简单的内置库用于从字符串中获取元组。

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 由您的元组组成 最终结果是所有长度小于等于 4 的元组的列表。

输出:

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

参考:Parse a tuple from a string?

尝试以下解决方案:

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)