按年份和 ID 拆分 txt 文件,并将每个新的 txt 文件重命名为 "Year_ID.txt"



我有一堆txt文件(逗号分隔(,我想使用第1列(年份(和第3列(ID(中的公共组标识符将文件拆分为单独的文本文件。此外,我想将新的文件名保存为";第12_列3.txt";。我不想为这些文件保留任何标头。我尝试了许多其他问题的脚本/建议,但似乎都不起作用。我是python的新手,任何建议都会很有帮助。非常感谢。

文件格式:

1.0,9.0,0.0,0.0,5.0,13.2143.2993.8529934630001,18.005554199200002,92.599998741,0.00.0159.8820557911.0,9.0,0.0,1.0,5.0,13.3142.8992.4,19.0,91.5013544438,0.0,0.0202.6450724021.0,9.0,0.0,2.0,5.0,13.4142.5989.0,21.2,90.4027104135,0.0,0.0235.397877811.0,9.0,0.0,3.0,5.0,13.5142.2986.5,22.7,89.3040663832,0.00.0268.746810812000041.0,11.0,1.0,1.0,5.0,11.5175.6995.6,18.7,18.5200004578,0.0,0.0680.611388461.0,11.0,1.0,5.0,5.0,12.2174.1988.9,23.4,18.5200004578,0.0,0.0645.0406469611.0,11.0,1.0,6.0,5.0,12.4173.9986.5,24.9,18.5200004578,0.0,0.0654.79816281699991.0,9.0,2.0,4.0,5.0,10.7146.8986.0,23.2,68.3182237413,0.00.0364.7243007561.0,9.0,2.0,5.0,5.0,10.8146.2982.9,25.0,66.8777792189,0.0317.156397048

所以我的输出应该是:文件1:

1.0,9.0,0.0,0.0,5.0,13.2143.2993.8529934630001,18.005554199200002,92.599998741,0.00.0159.8820557911.0,9.0,0.0,1.0,5.0,13.3142.8992.4,19.0,91.5013544438,0.0,0.0202.6450724021.0,9.0,0.0,2.0,5.0,13.4142.5989.0,21.2,90.4027104135,0.00.0235.39787781

文件2:

1.0,11.0,1.0,1.0,5.0,11.5175.6995.6,18.7,18.5200004578,0.0,0.0680.611388461.0,11.0,1.0,5.0,5.0,12.2174.1988.9,23.4,18.5200004578,0.0,0.0645.0406469611.0,11.0,1.0,6.0,5.0,12.4173.9986.5,24.9,18.5200004578,0.00.0654.7981628169999

文件3:

1.0,9.0,2.0,4.0,5.0,10.7146.8986.0,23.2,68.3182237413,0.00.0364.7243007561.0,9.0,2.0,5.0,5.0,10.8146.2982.9,25.0,66.8777792189,0.0317.156397048

假设:

  1. 所有条目都是统一的
  2. 条目包含在2d列表中
  3. 所有条目的长度至少为3(包括两个分隔字段(

轻微问题:

  • 在File1中,第二个条目前面应该有"2055791"吗?这意味着列表条目对于您想要的内容来说不是太统一。如果是这种情况,那么我建议在手动之前清理数据,或者添加到该代码中,这样它就可以忽略这一点
#grab the full list
full_list = []
#grab every value of column 1
col_one_list = [a[0] for a in full_list]
#grab every value of column 3
col_three_list = [b[2] for b in full_list]

#sort by them
for i in col_one_list:
for j in col_three_list:
separate_list = []
for entry in full_list:
if (entry[0] == i and entry[2] == j):
separate_list.append(entry)
with open(str(i) + "_" +str(j)+".txt", "w" ) as file:
for item in separate_list:
file.write("%sn" % item)

这就足够了。

相关内容

  • 没有找到相关文章

最新更新