python将txt文件的内容逐行读取转化成数组

例:将train5bottle.names的每行内容提取出来转化成数组
python将txt文件的内容逐行读取转化成数组_第1张图片
转换代码:

result = [] 
with open(r'E:\HISI\darknet-master\build\darknet\x64\data\train5bottle.names' ,'r') as f:
    for line in f:
     result.append(line.strip().split(',')[0])  #a.append(b):是将b原封不动的追加到a的末尾上,会改变a的值
        #strip()用于移除字符串头尾指定的字符(默认为空格或者换行符)或字符序列
    print(result) 
print(result[0])

#运行结果:
['0degree', '6degree', '12degree', '18degree', '24degree', '30degree', '36degree', '42degree', '48degree', '54degree', '60degree', '66degree', '72degree', '78degree', '84degree', '90degree', '96degree', '102degree', '108degree', '114degree', '120degree', '126degree', '132degree', '138degree', '144degree', '150degree', '156degree', '162degree', '168degree', '174degree', '180degree']
0degree

你可能感兴趣的:(python,python,list)