文件分类 几种文件分类的形式

Posted wang102030

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件分类 几种文件分类的形式相关的知识,希望对你有一定的参考价值。

 

文件分类的一般流程

# 1,需要处理的文件
# 2.创建保存文件的列表
# 3.写入文件
# 4.关闭文件

 

 

 

 

 

事例一.

QQlist=[5,6,7,8,9,10,11,"小垃圾"]
filepath = r"G:我的pythonpython基础day12QQQQGood.txt"
file = open(filepath,"rb")
mylist = file.readlines()
file.close()

# 创建文件对象 存放到列表中
filelist=[]
for i in QQlist:
    QQfilepath = "G:\我的python\python基础\day12\QQ\QQ位数分类\"+str(i)+"位QQ.txt"
    QQfile = open(QQfilepath,"wb")
    filelist.append(QQfile)

# 分类位置
for line in mylist:
    bakline = line
    line = line.decode("utf-8")
    linelist = line.split("----")
    length = len(linelist[0])  # 取账号的长度

    if length == 5:
        filelist[0].write(bakline)
    elif length == 6:
        filelist[1].write(bakline)
    elif length == 7:
        filelist[2].write(bakline)
    elif length == 8:
        filelist[3].write(bakline)
    elif length == 9:
        filelist[4].write(bakline)
    elif length == 10:
        filelist[5].write(bakline)
    elif length == 11:
        filelist[6].write(bakline)
    else:
        filelist[7].write(bakline)
# 关闭文件
for  QQfile  in filelist:
    QQfile.close()

 

事例二

import codecs

# 1,需要处理的文件
filepath = r"G:我的python尹成python基础day12kaifangkaifangGood.txt"
file = codecs.open(filepath,"rb","utf-8","ignore")
mylist = file.readlines()
file.close()
print("内存加载结束,准备区域划分")


# 2.创建保存文件的列表
area = [ age for age in range(1000,2018)]
length = len(area)

areafilelist = []
for data in area:
    areafilepath = "G:\我的python\尹成\python基础\day12\kaifang\年龄\" + str(data) + ".txt"
    areafile = open(areafilepath,"wb")
    areafilelist.append(areafile)


# 3.写入文件
for line in mylist:
    linelist = line.split(",")
    chstr = linelist[1][6:10]  # 取出一个字符
    for i in range(length):
        if str(area[i]) == chstr:
            areafilelist[i].write(line.encode("utf-8"))


# 4.关闭文件
for data in areafilelist:
    data.close()

 

事例三 (按照身份证号码取)

import codecs
filepath = r"G:我的pythonpython基础day12kaifangkaifangGood.txt"
file = codecs.open(filepath,"rb","utf-8","ignore")
mylist = file.readlines()
file.close()
print("内存加载结束,准备区域划分")

# 分类器
area=[[11,"北京"],[12,"天津"],[13,"河北"],[14,"山西"],[15,"内蒙古"],[21,"辽宁"],[22,"吉林"],[23,"黑龙江"],[31,"上海"],[32,"江苏"],[33,"浙江"],[34, "安徽"],[35,"福建"],[36,"江西"],[37,"山东"],[41,"河南"],[42,"湖北"],
         [43, "湖南"],[44,"广东"],[45,"广西"],[46,"海南"],[50,"重庆"],
         [51, "四川"],[52,"贵州"],[53,"云南"],[54,"西藏"],[61,"陕西"],
         [62, "甘肃"],[63,"青海"],[64,"宁夏"],[65,"新疆"],[71,"台湾"],
         [81, "香港"],[82,"澳门"]]
length=len(area)#长度

# 创建区域文件的对象
areafilelist = []
for data in area:
    kffilefath = "G:\我的python\python基础\day12\kaifang\省份\" + data[1] + ".txt"
    kffile = open(kffilefath,"wb")
    areafilelist.append(kffile)

for line in mylist:
    linelist = line.split(",")  # 字符串切割
    chstr = linelist[1][0:2]  # 取出月份
    # 判断身份证所在的地区
    for i in range(length):
        if str(area[i][0]) == chstr:
            areafilelist[i].write(line.encode("utf-8"))
            break


# 关闭文件
for line in areafilelist:
    line.close()
length=len(area)#长度

# 创建区域文件的对象
areafilelist = []
for data in area:
    kffilefath = "G:\我的python\尹成\python基础\day12\kaifang\区域\" + data[1] + ".txt"
    kffile = open(kffilefath,"wb")
    areafilelist.append(kffile)

# 分类写入数据
for line in mylist:
    linelist = line.split(",")  # 字符串切割
    chstr = linelist[1][0:2]  # 取出2个字符
    # 判断身份证所在的地区
    for i in range(length):
        if str(area[i][0]) == chstr:
            areafilelist[i].write(line.encode("utf-8"))
            break


# 关闭文件
for line in areafilelist:
    line.close()

 

以上是关于文件分类 几种文件分类的形式的主要内容,如果未能解决你的问题,请参考以下文章

文本分类

齐博分类系统漏洞分析

部分类有多种形式

信息以文件形式存储,文件用啥分类分层存放

java.io 文件分类

操作系统复习专题