贴一个做数据清洗时写的代码,
做数据处理时,原文件数据在进行处理时需要转换成一定格式,
原始文件数据:123.txt
1,3,4
2,3,5
1,2,3,5
2,5
利用Python转换成二维列表:
#!/usr/bin/env python #coding=utf-8 def loadDataSet(): file = open("123.txt", "r") List_row = file.readlines() list_source = [] for list_line in List_row: list_line = list(list_line.strip().split(',')) s = [] for i in list_line: s.append(int(i)) list_source.append(s) return list_source
运行程序后结果如下:
[[1,3,4],[2,3,5],[1,2,3,5],[2,5]]