python 数据读入 常用的 程序 段落
发布日期:2021-11-21 04:41:27 浏览次数:40 分类:技术文章

本文共 6315 字,大约阅读时间需要 21 分钟。

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 = []    # glob.glob 返回所有匹配的文件路径列表(list)    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 npfile = 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. 检查文件路径是否存在,新建文件夹

pytho 2

import osos.path.exist('/home/jia/my.py') # return Ture or Falseos.path.isfile('home/jia') # 判断文件路径是否是文件,此处返回False,因为jia是文件夹而不是文件os.makedirs('home/jia/data') # 创建新文件夹

python 3 中使用os.makedirs,如果路径文件存在则打开,如果不存在则新建。

下面是一个记录loss的log文件的创建,打开,写入和关闭。

os.makedirs(checkpoint_path, exist_ok=True)logger = open(os.path.join('./' + checkpoint_path, 'log'), 'a+')logger.write('\ntrain_loss of epoch {} : {}'.format(epoch, train_loss)) # 在已存在文本的下一行加入logger.flush() # 冲洗内存logger.close() # 关闭文件

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

7.python读取和存储dict()与.json格式文件

如果存储在json文件中的就是以dict形式存储的,则读取和写入可以直接整体操作,不需分行或者关键字进行。

with open("jsondata.json","w") as file: #打开一个名为jsondata.json文本,只能写入状态 如果没有就创建    json.dump(data,file) #data转换为json数据格式并写入文件    file.close()#关闭文件with open("jsondata.json","r") as file:#打开文本读取状态    l =  json.load(file) #解析读到的文本内容 转为python数据 以一个变量接收    print(l) #打印变量    file.close() #关闭文件

8.python顺序的读取文件夹下名称有序的文件

import ospath="/home/test/"  #待读取的文件夹path_list=os.listdir(path)path_list.sort() #对读取的路径进行排序for filename in path_list:	print(os.path.join(path,filename))--------------------- 作者:小银是猪 来源:CSDN 原文:https://blog.csdn.net/Merdy_xi/article/details/78409632 版权声明:本文为博主原创文章,转载请附上博文链接!

9. 使用python下载网页链接数据:

from urllib import requestfilename = [["training_images","train-images-idx3-ubyte.gz"],["test_images","t10k-images-idx3-ubyte.gz"],["training_labels","train-labels-idx1-ubyte.gz"],["test_labels","t10k-labels-idx1-ubyte.gz"]]def download_mnist():    base_url = "http://yann.lecun.com/exdb/mnist/"    for name in filename:        print("Downloading "+name[1]+"...")        request.urlretrieve(base_url+name[1], name[1])    print("Download complete.")

 

 

转载地址:https://blog.csdn.net/xiaojiajia007/article/details/72794691 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ubuntu安装谷歌拼音输入法, ubuntu 软件更新源 source.list, package无法找到安装,ubuntu system setting问题: ubuntu desktop 安装
下一篇:Install caffe on Ubuntu 14.04 with GTX 1080

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月18日 18时03分51秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章