我有一些最初看起来像这样的代码(它是for循环中迭代查询结果的一部分):
<DataContract()> _
Public Class PeopleList
<DataMember(Name:="people")> _
Public People_List As List(Of People)
End Class
<DataContract()> _
Public Class People
<DataMember(Name:="name")> _
Public Person_Name As String
<DataMember(Name:="att")> _
Public Attempts As String
End Class
Public Sub ScrapeFile()
Try
Dim thisScrape As New PeopleList
thisScrape = DirectCast(JSONSerialization.jsonDeserializeURI(Of PeopleList)("http://www.sitenotusedyet.com"), PeopleList)
Catch ex As Exception
MsgBox("Shit")
End Try
End Sub
Public Shared Function JsonDeserialize(Of T)(jsonString As String) As T
Dim ser As New DataContractJsonSerializer(GetType(T))
Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(jsonString))
Dim obj As T = DirectCast(ser.ReadObject(ms), T)
Return obj
End Function
Public Shared Function jsonDeserializeURI(Of T)(ByVal strURI As String) As T
Dim strHTML As String
Dim objWC As New System.Net.WebClient
Dim obj As T
Try
obj = JSONSerialization.JsonDeserialize(Of T)("{""people"":{""00-0029619"":{""name"":""J.Lane"",""att"":1},""00-0021206"":{""name"":""J.Street"",""att"":3}}}")
Catch ex As System.Exception
Throw ex
End Try
Return obj
End Function
我有时会得到以下异常(总是在同一行):
title['name'] += 'x'
我将代码更改为更详细:
<class 'psycopg2.extras.DictRow'>
Traceback (most recent call last):
File "rtk_film_nos.py", line 231, in <module>
main()
File "rtk_film_nos.py", line 150, in main
title['name'] += 'x'
TypeError: list indices must be integers, not str
异常改为:
foo = title['name']
foo += 'x'
print type(title)
title['name'] = foo
如果我在try /中包装代码,除非它会很高兴地打印标题['name']的内容。
我不知道自己做错了什么。似乎由于某种原因python(v2.6.6)决定将dict视为列表,但我不知道为什么。
答案 0 :(得分:3)
DictRow
类是list
的子类,until version 2.3.0您不能使用它来按名称分配元素。
您可以通过直接查找列索引来解决此问题:
title[title._index['name']] = foo
或者,对于增强的作业:
title[title._index['name']] += 'x'