读取CSV文件,有时可能会遇见编码问题。
.join("###")是自动在(字符串)数组之间加上###,放在set里用来去重
def CsvProcess(self):
flag = True
# 去重处理 产国、大类、类型
with open(self.CMS_data_path, encoding="utf-8-sig") as f:
reader = csv.reader(f)
print(type(reader))
for row in reader:
if flag:
flag = False
continue
# print(type(row))
tmp = '###'.join(row)
self.CMS.add(tmp)
print(self.CMS)
# 生成 query语句 产国、大类、类型
num = 0
for nodes in set(self.CMS):
nodes = nodes.split('###')
c = nodes[0]
m = nodes[1]
s = nodes[2]
query = "CREATE (subclass:类型""{name:\"" + c + "-" + m + "-" + s + "\",产国:\"" + c + "\",大类:\"" + m + "\",类型:\"" + s + "\"})"
num = num + 1
print("产国、大类、类型数量:",num,"query: ", query)
try:
result = self.g.run(query)
print(result)
except Exception as e:
print(e)
读取json有时候也需要这设置编码
def jsonProcess(self):
with open(self.country_data_path, encoding="utf-8") as f:
reader = csv.reader(f)
print(type(reader))
for row in reader:
self.country.add(row[0])
print(row)