文本文件有超过50K行使用此格式
M:org.apache.mahout.common.RandomUtilsTest:testHashDouble():['(O)java.lang.Double:<init>(double)', '(M)java.lang.Double:hashCode()', '(S)org.apache.mahout.common.RandomUtils:hashDouble(double)', '(S)org.apache.mahout.common.RandomUtilsTest:assertEquals(long,long)', '(O)java.lang.Double:<init>(double)']
M:org.apache.mahout.common.RandomUtilsTest:testHashFloat():['(M)java.util.Random:nextLong()', '(M)java.util.Random:nextLong()', '(M)java.util.Random:nextLong()', '(S)org.apache.mahout.common.RandomUtilsTest:assertEquals(java.lang.String,long,long)']
M:org.apache.mahout.math.AbstractVectorTest:testAssignBinaryFunction():['(I)org.apache.mahout.math.Vector:assign(org.apache.mahout.math.Vector,org.apache.mahout.math.function.DoubleDoubleFunction)', '(O)java.lang.StringBuilder:<init>()', '(I)org.apache.mahout.math.Vector:getQuick(int)', '(S)org.apache.mahout.math.AbstractVectorTest:assertEquals(java.lang.String,double,double,double)']
M:org.apache.mahout.math.AbstractVectorTest:testAssignBinaryFunction2():['(S)org.apache.mahout.math.function.Functions:plus(double)', '(I)org.apache.mahout.math.Vector:assign(org.apache.mahout.math.function.DoubleFunction)', '(S)org.apache.mahout.math.AbstractVectorTest:assertEquals(java.lang.String,double,double,double)']
如何将这些数据读取并格式化到字典中,以便[]中的所有方法都是单独的值,并且[](测试方法)之前的字符串是键?如何在将它们存储为字典中的值之前删除“”
下面是用于填充文本文件的代码。现在,我尝试获取该txt文件数据并将其读入/解析回另一个字典
d = {}
with open("filtered.txt") as input:
for line in input:
(key, val) = line.strip().split(" ")
if str(key) in d:
d[str(key)].append(val)
else:
d[str(key)] = [val]
keys = []
for key in d:
keys.append(key)
keys.sort()
input.close()
with open('mahout-coverage.txt', 'w') as outfile:
for key in keys:
outfile.writelines('{}:{}'.format(key, d[key]) + "\n")
使用ast.literal_eval可以将字符串列表转换为
list
输出:
json模块可用于将python字典存储到文件中,然后加载该文件,并在将其写入文件之前将其解析为相同的数据类型
参考:json.dump()和json.load()
相关问题 更多 >
编程相关推荐