鱼C论坛

 找回密码
 立即注册
查看: 1812|回复: 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
  1. #in_data_path=
  2. #out1_data_path=
  3. #out2_data_path=

  4. with open(in_data_path,encoding='UTF-8') as in_data,\
  5.         open(out1_data_path,encoding='UTF-8',mode='w') as out1_data,\
  6.         open(out2_data_path,encoding='UTF-8',mode='w') as out2_data:
  7.     for i in in_data:
  8.         if i[-1]=='1':
  9.             out1_data.write(i)
  10.         else:
  11.             out2_data.write(i)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-14 17:21:46 | 显示全部楼层
请先学会提问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 17:24:17 From FishC Mobile | 显示全部楼层
本帖最后由 hrp 于 2020-3-14 18:31 编辑
  1. list1,list2=[],[]
  2. with open('a.txt','rt',encoding='utf-8') as f:
  3.     list0=f.readlines()
  4.     for i on list0:
  5.         if i[-2] == '1':
  6.             list1.append(i)
  7.         elif i[-2] == '2':
  8.             list2.append(i)
  9. f1 = open('f1.txt','wt',encoding='utf-8')
  10. f2 = open('f2.txt','wt',encoding='utf-8')
  11. f1.writelines(list1)
  12. f2.writelines(list2)
  13. f1.close()
  14. f2.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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



小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  4. with open(in_data_path,encoding='UTF-8') as in_data,\
  5.         open(out1_data_path,encoding='UTF-8',mode='w') as out1_data,\
  6.         open(out2_data_path,encoding='UTF-8',mode='w') as out2_data:
  7.     for i in in_data:
  8.         if i[-1]=='1':
  9.             out1_data.write(i)
  10.         else:
  11.             out2_data.write(i)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-14 08:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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