将具有多个值的字符串转换成字典?

时间:2019-02-18 15:19:36

标签: python string pandas dictionary

我有一个数据框,例如:

Path                          Attribute
/home/accessbile/wheelchair   type:threshold;width:42;gaurded:no
/home/accessible/wheelcahir   type:threshold;width:45
/home/accessbile/armchair     weight(lbs):100;width:30
/home/accessible/armchair     type:foldind;weight(lbs):100;width:30

我需要获得相对于路径的值(类型,宽度,重量,gaurded之类的..)的属性中的唯一计数

输出:

    PATH                          Attributes  count


/home/accessbile/wheelchair     type          2
/home/accessbile/wheelchair     width         2
/home/accessbile/wheelchair    gaurded        1
/home/accessbile/armchair      weight(lbs)    2
/home/accessbile/armchair      width          2
/home/accessbile/armchair      type           1 

尝试通过ast.eval将属性转换为字典,并将键作为唯一计数..但不起作用

1 个答案:

答案 0 :(得分:0)

首先,检查Imports Microsoft.Office.Interop Module Module1 Sub GetWeightedAverage() If Not Globals.ThisAddIn.Application.Selection.GetType Is GetType(Excel.Range) Then Throw New CustomExceptions.WrongSelectionException("You must select cells.") Dim Selection As Excel.Range = Globals.ThisAddIn.Application.Selection If Selection.Columns.Count <> 2 Then Throw New CustomExceptions.WrongSelectionException("You must select 2 columns.") Dim TotalValues As Double = 0 Dim TotalWeight As Double = 0 For Each Row As Excel.Range In Selection.Rows TotalValues += Row.Cells(1, 1).value2 * Row.Cells(0, 1).value2 TotalWeight += Row.Cells(1, 2).value2 Next MsgBox("Weighted average is {result}.".Replace("{result}", TotalValues / TotalWeight)) End Sub End Module Namespace Global.CustomExceptions <Serializable()> Public Class WrongSelectionException : Inherits System.Exception Public Sub New() MyBase.New() End Sub Public Sub New(ByVal message As String) MyBase.New(message) End Sub Public Sub New(ByVal message As String, ByVal innerException As System.Exception) MyBase.New(message, innerException) End Sub <System.Security.SecuritySafeCritical()> Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) MyBase.New(info, context) End Sub End Class End Namespace 中所有行的拼写,修复后,可以执行以下操作:

Path