【发布时间】:2022-06-14 17:11:15
【问题描述】:
我有一个txt 文件
-A
--this is for A1
--not just start with 2
--A3
-B
--B1
-C
--See1
--Yahoo
我希望这个txt文件的数据变成pythondict,
喜欢
{
'A':['this is for A1', 'not just start with 2', 'A3'],
'B':['B1'],
'C':['See1', 'Yahoo']
}
我可以改变这个,但结果 dict 就像
{
'A\r':['this is for A1', 'not just start with 2', 'A3'],
'B\r':['B1'],
'C\r':['See1', 'Yahoo']
}
键中有一个额外的\r
我该如何解决这个问题?
【问题讨论】:
-
请包含用于将
.txt文件转换为您的字典的代码。那里可能有一个错误导致额外的\r值 -
在 Windows 上,由于历史原因,行以
\r\n而不是\n像许多其他操作系统一样终止。这可能与问题有关吗?
标签: python