# 将txt文件读入列表
def txt_to_list(path):
"""
path:txt文件路径
返回:列表
列表内容为:['1024-1.xyz', '1024-2.xyz', ...]共二百个元素
""
with open(path)as txt:
cont = txt.read()
# 去除第一个字符 [ 和最后一个字符 ]
newcont = cont[1:] + cont[:-1]
# print(newcont)
list_cont = []
for i in range(200):
# 当取到最后一个元素时,索引用-1表示
# 不然会出错,我也不知道为什么
if i == 199:
i = -1
# 以","分隔,并去除首尾空格,写入列表alph
alph = newcont.split(',')[i].strip()
# print(alph)
alph = alph[1:-1]
list_cont.append(alph)
return list_cont
# 将txt文件读入numpy数组
def txt_to_numpy(filename, row, colu):
"""
filename:是txt文件路径
row:txt文件中数组的行数
colu:txt文件中数组的列数
"""
file = open (filename)
lines = file.readlines()
# print(lines)
# 初始化datamat
datamat = np.zeros((row, colu))
row_count = 0
for line in lines:
# 写入datamat
line = line.strip().split(' ')
datamat[row_count,:] = line[:]
row_count += 1
return datamat