为了账号安全,请及时绑定邮箱和手机立即绑定

如何将文本文件中的输入格式化为python中的defaultdict

如何将文本文件中的输入格式化为python中的defaultdict

呼唤远方 2022-11-01 15:18:42
具有这种格式的文本文件有超过 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)']如何读取这些数据并将其格式化为字典,以便 [] 中的所有方法都是单独的值,而 [ (测试方法)之前的字符串是键?在将它们作为值存储在字典中之前,我将如何删除它们?#Python这是用于填充文本文件的代码。现在我正在尝试获取该 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")
查看完整描述

2 回答

?
aluckdog

TA贡献1847条经验 获得超7个赞

json 模块可用于将 python 字典存储到文件中,然后加载文件并在将其写入文件之前将其解析为相同的数据类型。


d = {}

with open('filtered.txt') as input:

    for line in input:

        key, value = line.strip().split("():")

        key = "{}()".format(key)

        d[key] = value


print(d)


# It would be better and easy if you write the data to the file using json module

import json


with open('data.txt', 'w') as json_file:

  json.dump(d, json_file)


# Later you can read the file using the json module itself

with open('data.txt') as f:

  # this data would be a dicitonay which can be easily managed.

  data = json.load(f)

参考:json.dump()json.load()


查看完整回答
反对 回复 2022-11-01
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

使用ast.literal_eval您可以将字符串列表转换为list


from collections import defaultdict

import ast

with open('tst.txt') as fp:

    d = defaultdict(list)

    for line in fp:

        k, v = line[: line.index('):') + 1], ast.literal_eval(line[line.index(':[') + 1:])

        d[k] += v

print(dict(d))

输出:


{

M:org.apache.mahout.common.RandomUtilsTest:testHashDoubl :  ['(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:testHashFloa :  ['(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:testAssignBinaryFunctio :  ['(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:testAssignBinaryFunction :  ['(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)']

}


查看完整回答
反对 回复 2022-11-01
  • 2 回答
  • 0 关注
  • 213 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号