我有一个文本文件,格式如下,行数很多,
[ABC] [text text] [1234]
[DEF] [text text: text] [2345]
....
我想将列拆分为数组。
names = [ ABC , DEF]
text = [text text, text text: text]
values = [1234, 2345]
我正在尝试使用numpy.genfromtxt但我不确定如何设置分隔符,因为文本内容中有空格。是否有可能以某种方式将分隔符定义为“[]”。
答案 0 :(得分:0)
以下是将列转置为行的示例。
>>> import numpy as np
>>> s = "[ABC] [text text] [1234]\n[DEF] [text text: text] [2345]"
>>> lines = s.split('\n')
# this is were the delimiters are split apart --vvv
>>> rows = [line.lstrip('[').rstrip(']').split('] [') for line in lines] # list comprehension
>>> rows
[['ABC', 'text text', '1234'], ['DEF', 'text text: text', '2345']]
>>> np_rows = np.array(rows)
>>> np_rows.T
array([['ABC', 'DEF'],
['text text', 'text text: text'],
['1234', '2345']],
dtype='|S15')
>>> np_rows.transpose()
array([['ABC', 'DEF'],
['text text', 'text text: text'],
['1234', '2345']],
dtype='|S15')