基础知识(八)python读写数据相关文件

import cv2
import csv
import numpy as np
from pandas.io.parsers import read_csv
from matplotlib import pyplot as plt


def writedata(listdata,filepath="newlabel.txt"):
    fr=open(filepath,'w')
    fr.writelines(listdata)
    fr.close()
def readdata(filepath):
    filepath
    fr=open(filepath,'r')
    filesplit=[]
    for line in fr.readlines():
        s=line.split()
        filesplit.append(s);
    fr.close()
    return filesplit
 
def IsSubString(SubStrList,Str):  
    ''''' 
    #判断字符串Str是否包含序列SubStrList中的每一个子字符串 
    #>>>SubStrList=['F','EMS','txt'] 
    #>>>Str='F06925EMS91.txt' 
    #>>>IsSubString(SubStrList,Str)#return True (or False) 
    '''  
    flag=True  
    for substr in SubStrList:  
        if not(substr in Str):  
            flag=False  
  
    return flag  
#~ #----------------------------------------------------------------------  
def GetFileList(FindPath,FlagStr=[]):  
    ''''' 
    #获取目录中指定的文件名 
    #>>>FlagStr=['F','EMS','txt'] #要求文件名称中包含这些字符 
    #>>>FileList=GetFileList(FindPath,FlagStr) # 
    '''  
    import os  
    FileList=[]  
    FileNames=os.listdir(FindPath)  
    if (len(FileNames)>0):  
       for fn in FileNames:  
           if (len(FlagStr)>0):  
               #返回指定类型的文件名  
               if (IsSubString(FlagStr,fn)):  
                   fullfilename=os.path.join(FindPath,fn)  
                   FileList.append(fullfilename)  
           else:  
               #默认直接返回所有文件名  
               fullfilename=os.path.join(FindPath,fn)  
               FileList.append(fullfilename)  
  
    #对文件名排序  
    if (len(FileList)>0):  
        FileList.sort()  
  
    return FileList 



cv2.namedWindow('facerect')
label=readdata('fold_0_data.txt')

labelcor=[]
for i in range(100):
    if i==0:
        
        
        continue
    FileList=GetFileList('imagedata\\'+label[i][0],['jpg']) 
    for imgp in FileList:
        if label[i][1] in imgp:
            newl=[]
            newl.append(imgp)
            newl.append(label[i][5])
            newl.append(label[i][4])
            labelcor.append(newl) 
            break
#去除一张图片中有多个人,多个标签的图片
twocout=[]
for l in labelcor:
    twocout.append(l[0])
    
from collections import Counter
counter=Counter(twocout)

indexnew=[]
for key in counter:
    if counter[key]==1:
        indexnew.append(twocout.index(key))
        
        
resultfile=[]     
for i in indexnew:
    l=labelcor[i]
    s = '\t'.join(l) + '\n'
    resultfile.append(s)


    
writedata(resultfile)
'''img = plt.imread(l[0])
    plt.imshow(img)
    plt.title(l[1])             
    while True:  
        if cv2.waitKey(0)==13:
            break'''

cv2.destroyAllWindows()

你可能感兴趣的:(基础知识(八)python读写数据相关文件)