Python创建cvs文件,包含标签和图片数据

    在深度学习或者机器学习的时候,常常需要对数据进行整理和分类,最常见的是通过对数据路径和标签写入

到一个整合的txt或者csv文件中,训练进行读取。

 1 #coding=utf-8
 2 #!/usr/bin/env python
 4 import sys
 5 import os.path
 6
 7 # This is a tiny script to help you creating a CSV file from a face
 8 # database with a similar hierarchie:
 9 #
11 #  .
12 #  |-- README
13 #  |-- s1
14 #  |   |-- 1.pgm
15 #  |   |-- ...
16 #  |   |-- 10.pgm
17 #  |-- s2
18 #  |   |-- 1.pgm
19 #  |   |-- ...
20 #  |   |-- 10.pgm
21 #  ...
22 #  |-- s40
23 #  |   |-- 1.pgm
24 #  |   |-- ...
25 #  |   |-- 10.pgm
26 #
27
28 if __name__ == "__main__":
29
30     if len(sys.argv) != 3:
31         print "usage: create_csv <base_path>"
32         sys.exit(1)
33
34     BASE_PATH=sys.argv[1]
35     SEPARATOR=";"
36     fh = open(sys.argv[2], ‘w‘)
37
38     label = 0
39     for dirname, dirnames, filenames in os.walk(BASE_PATH):
40         for subdirname in dirnames:
41             subject_path = os.path.join(dirname, subdirname)
42             for filename in os.listdir(subject_path):
43                 abs_path = "%s/%s" % (subject_path, filename)
44                 print "%s%s%d" % (abs_path, SEPARATOR, label)
45                 fh.write(abs_path)
46                 fh.write(SEPARATOR)
47                 fh.write(str(label))
48                 fh.write("\n")
49             label = label + 1
50     fh.close()

  调用方式如下

  

python create.py d:/test/jaf d:/a.csv

  生成文件结果如下图所示

  通过替换可以将斜杠调整一致

时间: 2024-07-29 12:27:32

Python创建cvs文件,包含标签和图片数据的相关文章

通过python创建域名解析文件excel格式(可用来导入阿里云)

通过python创建excel文件,可导入阿里云域名解析!因工作需要编写的脚本,没啥难度,分享出来供大家参考示例代码:#!/usr/bin/env python#coding:utf-8 import xlsxwriter serverid = list(range(001,016)) ym = ".域名" iplist = ["192.168.112.100","192.168.112.101","192.168.112.102&qu

python之从文件中按行读取数据

#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' # 从文件中按行读取数据 file = open("D:\坚果云\我的坚果云\\2019年计划.txt") while 1: lines = file.readlines(100000) if not lines: break for line in lines: print(line) 原文地址:https://www.cnblogs.c

利用python 创建XML文件

1 #coding=utf-8 2 from xml.etree import ElementTree 3 4 import pdb 5 6 def printNodeInfo(node): 7 8 #node.tag 标签名称 9 #node.text 文本属性 10 print 'node.tag: %s' %node.tag 11 12 #node.attrib 属性字典 13 for key in node.attrib: 14 print '%s %s' %(key,node.attr

python创建xml文件

本文是一个使用ElementTree有关类库,生成xml文件的例子 # *-* coding=utf-8 from xml.etree.ElementTree import ElementTree from xml.etree.ElementTree import Element from xml.etree.ElementTree import SubElement from xml.etree.ElementTree import dump from xml.etree.ElementTre

python 读取cvs 文件中多行数据

有的时候我们需要去读取多行多列数据 def duqu(): yiyuan = open('yiyuan_id2.csv','r') lines = yiyuan.readlines() yiyuan.close() for line in lines: id = line.split(',')[0] name = line.split(',')[1] print(id,name)duqu() 1.定义读取函数2.定义变量yiyuan,以只读的方式打开文件3.定义变量lines,通过读取多行数据赋

python 创建文件夹并将图片下载到新建的文件夹中

新建文件夹: import os import requests response = requests.get(url) dirname="test" #创建名为test的文件夹 os.mkdir(dirname) #此处相当于文件路径 test/image.jpg,也就是在test文件夹中的image.jpg文件 filename=dirname+'/'+'image.jpg' #打开文件夹并写入图片 with open(filename,'wb') as f: f.write(r

根据html生成Word文件,包含图片

根据html内容生成word,并自动下载下来.使用到了itext-1.4.6.jar import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; import java.net.URLEncoder; import

Python基础:文件的操作

一.python可以在不导入任何模块的情况下对文件完成读.写.追加,替换(稍复杂). 二.打开文件: 1.f = open('text.txt') 这种方式打开,操作完后需要使用f.colse()关闭. 2.with open('text.txt') as f: 这种方式打开,运行完系统自动关闭,回收内存.建议使用. 三.文件打开模式: python操作文件的时候,默认是只读的,需要指定各种模式才能进行相应的操作,模式列表: r 以只读方式打开文件 r+ 先读,再写模式 rb 以二进制格式只读打

python爬取微博图片数据存到Mysql中遇到的各种坑\python Mysql存储图片

本人长期出售超大量微博数据,并提供特定微博数据打包,Message to [email protected] 前言   由于硬件等各种原因需要把大概170多万2t左右的微博图片数据存到Mysql中.之前存微博数据一直用的非关系型数据库mongodb,由于对Mysql的各种不熟悉,踩了无数坑,来来回回改了3天才完成. 挖坑填坑之旅 建表 存数据的时候首先需要设计数据库,我准备设计了3个表 微博表:[id, userid, blog_text, lat, lng, created_time, res