python 数据读入 常用的 程序 段落

来源:互联网 发布:如何用eclipse写java 编辑:程序博客网 时间:2024/05/22 12:16

1. hdf5文件的创建与载入

try:

    with  h5py.File('X.h5') as hf:
        X, Y = hf['imgs'][:], hf['labels'][:]
    print("Loaded images from X.h5")
    
except (IOError,OSError, KeyError):  
    print("Error in reading X.h5. Processing all images...")
    root_dir = '/home/jia/Desktop/assignment2_normalization/GTSRB_Train_images/Final_Training/Images'
    imgs = []
    labels = []

    all_img_paths = glob.glob(os.path.join(root_dir, '*/*.ppm'))   # 将读入所有以*/*.ppm形式为名字的图片
    #打乱图片路径顺序
    np.random.shuffle(all_img_paths)    #将所有图片路径进行随机打乱
    for img_path in all_img_paths:
        try:

            img = preprocess_img(io.imread(img_path))

           
            # io.imread 读入的数据是 uint8
            
            label = get_class(img_path)
            imgs.append(img)
            labels.append(label)

            if len(imgs)%1000 == 0: print("Processed {}/{}".format(len(imgs), len(all_img_paths)))
        except (IOError, OSError):
            print('missed', img_path)
            pass

    X = np.array(imgs, dtype='float32')
    Y = np.eye(NUM_CLASSES, dtype='uint8')[labels]
    # Y = ***[labels] 生成one-hot编码的方式
    with h5py.File('X.h5','w') as hf:
        hf.create_dataset('imgs', data=X)

        hf.create_dataset('labels', data=Y)


此外, h5py实例对象hf.keys()是文件中对象名的预览, hf.item()是对文件包含的所有数据信息的统计预览

2. python 读取txt或者csv


import numpy as np
file = open("1.txt","r")
list_arr = file.readlines()
l = len(list_arr)
for i in range(l):
    list_arr[i] = list_arr[i].strip()
    list_arr[i] = list_arr[i].strip('[]')
    list_arr[i] = list_arr[i].split(", ")
a = np.array(list_arr)
a = a.astype(int)
print (a)
file.close()


读txt文件 转载自  http://www.cnblogs.com/muyouking/p/6399971.html


fname=input('Enter filename:') // 读取字符,与C++中的cin>>类似try:                                                  // try...expect是python中的异常处理语句,try中写    fobj=open(fname,'r')                   //  待检测的操作语句except IOError:                               // expect中写差错处理语句    print '*** file open error:'else:                                              // else中处理正常情况    for eachLine in fobj:        print eachLine    fobj.closeinput('Press Enter to close') 

   

读txt文件内容到列表


f = open('123.txt', 'r')              #文件为123.txtsourceInLines = f.readlines()  #按行读出文件内容f.close()new = []                                   #定义一个空列表,用来存储结果for line in sourceInLines:    temp1 = line.strip('\n')       #去掉每行最后的换行符'\n'    temp2 = temp1.split(',')     #以','为标志,将每行分割成列表    new.append(temp2)          #将上一步得到的列表添加到new中

 

print new
 
最后输出结果是:[['aaa''bbb''ccc'], ['ddd''eee''fff']],注意列表里存的是字符串'aaa',不是变量名aaa。

 

写txt文件

复制代码
fname=input('Enter filename:')try:    fobj=open(fname,'a')                 # 这里的a意思是追加,这样在加了之后就不会覆盖掉源文件中的内容,如果是w则会覆盖。except IOError:    print '*** file open error:'else:    fobj.write('\n'+'fangangnang')   #  这里的\n的意思是在源文件末尾换行,即新加内容另起一行插入。    fobj.close()                              #   特别注意文件操作完毕后要closeinput('Press Enter to close') 
复制代码

 


新建文件

复制代码
import oswhile True:    fname=input('fname>')    if os.path.exists(fname):        print "Error:'%s' already exists" %fname    else:        break#下面两句才是最重点。。。。也即open函数在打开目录中进行检查,如果有则打开,否则新建fobj=open(fname,'w')fobj.close()


3. 读取config文件 

paths = {}
with open('../path.config', 'r') as f:
    for line in f:
        line1 = line.replace('\n', '')
        name, path = line1.split(': ')

        print name, path
        paths[name] = path


strip(rm):删除s字符串中开头、结尾处,rm字符。当rm为空时默认删除空白符(包括'\n', '\r',  '\t',  ' ')

split(del):通过指定分隔符(del)对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符串。


4. 逐行读取txt文件,并且进行操作,然后再写入txt文件

with open('/home/jia/Desktop/CS231N_AND_traffic_sign_keras1.2/FullIJCNN2013/gt.txt', 'r') as f:
    lines = f.readlines()
for i in range(len(lines)):
    lines[i] = '/data/imgs/' + lines[i]
with open('/home/jia/Desktop/CS231N_AND_traffic_sign_keras1.2/FullIJCNN2013/my_data.txt', 'w') as f:
    f.writelines(lines)



5. 检查文件路径是否存在,新建文件夹

import os

os.path.exist('/home/jia/my.py') # return Ture or False

os.path.isfile('home/jia') # 判断文件路径是否是文件,此处返回False,因为jia是文件夹而不是文件

os.makedirs('home/jia/data') # 创建新文件夹


6. Pickle模块可以用来序列化(永久存储)python中几乎所有的数据类型(列表,字典,集合,类等)

 # 其中要注意的是,在load(file)的时候,要让python能够找到类的定义,否则会报错。 如果del Config,则载入会报错

class Config:    def __init__(self):        self.verbose = True        self.network = 'resnet50'        # setting for data augmentation        self.use_horizontal_flips = False        self.use_vertical_flips = False        self.rot_90 = False        # anchor box scales        self.anchor_box_scales = [24, 34, 54]C = Config()C.network = 'mobilenet'with open(config_output_filename, 'wb') as config_f:   # save config to pickle file = 'config.pickle'    pickle.dump(C, config_f)  # 永久保存类的实例Cwith open(config_output_filename, 'rb') as f_in:     C = pickle.load(f_in)print C.network    # 读取并载入pickle文件保持的数据

此外,pickle.dump()的对象还可以是字典等其他数据类型, 如C = {'X_train': x_train, 'Y_train':y_train}, 也是可以一样操作, pickle.load()之后, X = C['X_train'], array 数组的值将传递给了X

阅读全文
0 0