嘿,我正在使用python中的py2neo包在neo4j中创建一组节点。类似的代码片段适用于另一组节点,但在这种情况下不起作用。
from py2neo import *
rf = open('dataset.txt','r')
sf = rf.read().split('\n')
rf.close()
L = []
for i in range(len(sf)):
X = sf[i].split('\t')
L.append(X)
for i in range(len(L)):
L[i][0] = int(L[i][0])
L[i][1] = int(L[i][1])
L[i][4] = int(L[i][4])
L[i][5] = float(L[i][5])
L[i][6] = float(L[i][6])
L[i][8] = float(L[i][8])
L[i][9] = float(L[i][9])
L[i][10] = float(L[i][10])
L[i][19] = float(L[i][19])
def conGraph():
authenticate("localhost:7474","neo4j","neo")
graph = Graph("http://localhost:7474/db/data/")
return graph
def createProducts():
graph = conGraph()
L1, L2, L3, L4, L5, L6 = [], [], [], [], [], []
for i in range(len(L)):
if L[i][17] not in L1:
L1.append(L[i][17])
L2.append(L[i][15])
L3.append(L[i][16])
L4.append(L[i][18])
L5.append(float(L[i][9]))
L6.append(float(L[i][19]))
for i in range(len(L1)):
p = Node("Product", name = L1[i], category = L2[i], subcategory = L3[i], container = L4[i], unitprice = L5[i], basemargin = L6[i])
graph.create(p)
createProducts()
仅创建了第一个节点,然后发生了以下错误:
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\Desktop\AstroMite\lambda\test.py", line 44, in <module>
createProducts()
File "C:\Documents and Settings\Administrator\Desktop\AstroMite\lambda\test.py", line 41, in createProducts
p = Node("Product", name = L1[i], category = L2[i], subcategory = L3[i], container = L4[i], unitprice = L5[i], basemargin = L6[i])
File "C:\Python27\lib\site-packages\py2neo\core.py", line 1458, in __init__
PropertyContainer.__init__(self, **properties)
File "C:\Python27\lib\site-packages\py2neo\core.py", line 1223, in __init__
self.__properties = PropertySet(properties)
File "C:\Python27\lib\site-packages\py2neo\core.py", line 1110, in __init__
self.update(iterable, **kwargs)
File "C:\Python27\lib\site-packages\py2neo\core.py", line 1168, in update
self[key] = value
File "C:\Python27\lib\site-packages\py2neo\core.py", line 1139, in __setitem__
dict.__setitem__(self, key, cast_property(value))
File "C:\Python27\lib\site-packages\py2neo\types.py", line 55, in cast_property
value = ustr(value)
File "C:\Python27\lib\site-packages\py2neo\util.py", line 210, in ustr
return s.decode(encoding)
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xae in position 8: invalid start byte
我搜索了相关问题,但未找到任何具体答案。
答案 0 :(得分:0)
您的错误与neo4j或py2neo无关。您在阅读dataset.txt
文件时遇到问题。
Python假定文件是utf-8编码的,并尝试使用此字符编码对文件进行解码。它找到一个不是utf-8编码的字符,因此不能被解码。
您的文件很可能不是utf-8编码的。所以你必须找出字符编码,然后用例如:
打开文件f = open('dataset.txt', encoding = "ISO-8859-1")
有很多相关的问题:
UnicodeDecodeError: 'utf-8' codec can't decode byte