我有一个文本文件如下所示:
01:Pronoun
02:I
03:We
04:Self
05:You
06:Other
07:Negate
08:Assent
09:Article
10:Preps
11:Number
12:Affect
...
现在我想编一本字典。。这样的字典:
^{pr2}$这是我到目前为止得到的代码,但它似乎不能像我想要的那样工作。。。在
with open ('LIWC_categories.text','rU') as document1:
categoriesLIWC = {}
for line in document1:
line = line.split()
if not line:
continue
categoriesLIWC[line[0]] = line[1:]
如果不希望包含冒号,可以在冒号上拆分以获取键和值
key, value = line.split(':')
需要向split()传递分隔符字符串。在这种情况下,它将是“:”。在
在拆分.string()将在空白处自动拆分,但您的行中没有空格。如果要在键中使用:键,则可以始终将其与
还有
^{pr2}$应该是
相关问题 更多 >
编程相关推荐