鱼C论坛

 找回密码
 立即注册
查看: 1344|回复: 4

[已解决]txt数据分类

[复制链接]
发表于 2020-3-14 16:47:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.104250,0.120000,0.748967,2
1.114000,0.120000,0.747900,1
1.123750,0.120000,0.728500,2
1.133500,0.120000,0.735633,1
1.143250,0.120000,0.728067,1
1.153000,0.120000,0.736200,1
1.162750,0.120000,0.738233,1
1.172500,0.120000,0.736400,1
1.182250,0.120000,0.737300,1
1.192000,0.120000,0.740733,1
1.201750,0.120000,0.740100,1
1.211500,0.120000,0.738467,1
1.221250,0.120000,0.738333,1
1.231000,0.120000,0.738867,2
1.240750,0.120000,0.736933,2
1.250500,0.120000,0.736067,2
1.260250,0.120000,0.734933,2
1.270000,0.120000,0.734033,2
1.279750,0.120000,0.734033,2

凭借最后一位1和2来判断,将所有数据分类到两个txt文本里面
最佳答案
2020-3-14 20:00:26
#in_data_path=
#out1_data_path=
#out2_data_path=

with open(in_data_path,encoding='UTF-8') as in_data,\
        open(out1_data_path,encoding='UTF-8',mode='w') as out1_data,\
        open(out2_data_path,encoding='UTF-8',mode='w') as out2_data:
    for i in in_data:
        if i[-1]=='1':
            out1_data.write(i)
        else:
            out2_data.write(i)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-14 17:21:46 | 显示全部楼层
请先学会提问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 17:24:17 From FishC Mobile | 显示全部楼层
本帖最后由 hrp 于 2020-3-14 18:31 编辑
list1,list2=[],[]
with open('a.txt','rt',encoding='utf-8') as f:
    list0=f.readlines()
    for i on list0:
        if i[-2] == '1':
            list1.append(i)
        elif i[-2] == '2':
            list2.append(i)
f1 = open('f1.txt','wt',encoding='utf-8')
f2 = open('f2.txt','wt',encoding='utf-8')
f1.writelines(list1)
f2.writelines(list2)
f1.close()
f2.close()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 17:24:26 | 显示全部楼层
本帖最后由 jackz007 于 2020-3-14 17:35 编辑
str1 = '''
1.104250,0.120000,0.748967,2
1.114000,0.120000,0.747900,1
1.123750,0.120000,0.728500,2
1.133500,0.120000,0.735633,1
1.143250,0.120000,0.728067,1
1.153000,0.120000,0.736200,1
1.162750,0.120000,0.738233,1
1.172500,0.120000,0.736400,1
1.182250,0.120000,0.737300,1
1.192000,0.120000,0.740733,1
1.201750,0.120000,0.740100,1
1.211500,0.120000,0.738467,1
1.221250,0.120000,0.738333,1
1.231000,0.120000,0.738867,2
1.240750,0.120000,0.736933,2
1.250500,0.120000,0.736067,2
1.260250,0.120000,0.734933,2
1.270000,0.120000,0.734033,2
1.279750,0.120000,0.734033,2'''
d = [x . strip() for x in str1 . split('\n') if len(x . strip())]
d1 , d2 = [x + '\n' for x in d if x[-1] == '1'] , [x + '\n' for x in d if x[-1] == '2']
with open('file1.txt' , 'wt') as f : f . writelines(d1)
with open('file2.txt' , 'wt') as f : f . writelines(d2)


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 20:00:26 | 显示全部楼层    本楼为最佳答案   
#in_data_path=
#out1_data_path=
#out2_data_path=

with open(in_data_path,encoding='UTF-8') as in_data,\
        open(out1_data_path,encoding='UTF-8',mode='w') as out1_data,\
        open(out2_data_path,encoding='UTF-8',mode='w') as out2_data:
    for i in in_data:
        if i[-1]=='1':
            out1_data.write(i)
        else:
            out2_data.write(i)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-28 13:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表