如何在这串代码的基础实现添加3个新的excel表格进去from py2neo import * graph = Graph("http://localhost:7474", auth=('neo4j', 'zcy111111')) graph.delete_all() import csv row_list =[['id', 'name', 'age', 'sex', 'address'], [0, 'John', '23', 'man', 'Beijing'], [1, 'Bob', '23', 'man', 'Shanghai'], [2, 'Ben', '23', 'man', 'Shanghai'], [3, 'Mike', '23', 'man', 'Beijing'], [4, 'John', '23', 'man', 'Beijing'], [5, 'Robert J', '23', 'man', 'Beijing'], [6, 'Zhangsan', '23', 'man', 'ShanDong'], [7, 'Lisisi', '23', 'man', 'ShanDong'], [8, 'Liuyiyi', '23', 'man', 'Beijing'], [9, 'Heyi', '23', 'man', 'JiangSu'],] with open('data.csv', 'w', newline='') as f: writer = csv.writer(f) writer.writerows(row_list) print("输入完成") with open('data.csv', 'r') as f: reader = csv.reader(f) data = list(reader) print(data[1]) # 测试
时间: 2023-07-14 19:13:40 浏览: 157
可以通过以下步骤添加新的Excel表格:
1. 在代码所在的目录下创建新的Excel文件,例如 `data2.xlsx`、`data3.xlsx`。
2. 将新的数据写入新的Excel文件中,可以使用 `pandas` 库读取数据并将数据写入Excel文件中,也可以使用 `openpyxl` 库直接读写Excel文件。
3. 在代码中使用 `csv` 或其他适当的库读取新的Excel文件,并将其数据写入Neo4j数据库中。具体实现可参考以下示例代码:
```python
from py2neo import *
import csv
# 创建图数据库连接
graph = Graph("http://localhost:7474", auth=('neo4j', 'zcy111111'))
graph.delete_all()
# 添加第一个Excel表格
row_list =[['id', 'name', 'age', 'sex', 'address'],
[0, 'John', '23', 'man', 'Beijing'],
[1, 'Bob', '23', 'man', 'Shanghai'],
[2, 'Ben', '23', 'man', 'Shanghai'],
[3, 'Mike', '23', 'man', 'Beijing'],
[4, 'John', '23', 'man', 'Beijing'],
[5, 'Robert J', '23', 'man', 'Beijing'],
[6, 'Zhangsan', '23', 'man', 'ShanDong'],
[7, 'Lisisi', '23', 'man', 'ShanDong'],
[8, 'Liuyiyi', '23', 'man', 'Beijing'],
[9, 'Heyi', '23', 'man', 'JiangSu'],]
with open('data.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(row_list)
print("第一个Excel表格已添加")
with open('data.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # 跳过表头
for row in reader:
node = Node("Person", id=row[0], name=row[1], age=row[2], sex=row[3], address=row[4])
graph.create(node)
print("第一个Excel表格数据已写入Neo4j数据库")
# 添加第二个Excel表格
row_list =[['id', 'name', 'age', 'sex', 'address'],
[10, 'Lucy', '24', 'woman', 'Beijing'],
[11, 'Lily', '24', 'woman', 'Shanghai'],
[12, 'Sophie', '24', 'woman', 'Shanghai'],
[13, 'Emily', '24', 'woman', 'Beijing'],
[14, 'Lucy', '24', 'woman', 'Beijing'],
[15, 'Kate', '24', 'woman', 'Beijing'],
[16, 'Wangwu', '24', 'man', 'ShanDong'],
[17, 'Zhaoliu', '24', 'man', 'ShanDong'],
[18, 'Sunqi', '24', 'man', 'Beijing'],
[19, 'Zhangsan', '24', 'man', 'JiangSu'],]
with open('data2.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(row_list)
print("第二个Excel表格已添加")
with open('data2.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # 跳过表头
for row in reader:
node = Node("Person", id=row[0], name=row[1], age=row[2], sex=row[3], address=row[4])
graph.create(node)
print("第二个Excel表格数据已写入Neo4j数据库")
# 添加第三个Excel表格
row_list =[['id', 'name', 'age', 'sex', 'address'],
[20, 'Tom', '25', 'man', 'Beijing'],
[21, 'Jerry', '25', 'man', 'Shanghai'],
[22, 'Jack', '25', 'man', 'Shanghai'],
[23, 'Alice', '25', 'woman', 'Beijing'],
[24, 'Tom', '25', 'man', 'Beijing'],
[25, 'Jenny', '25', 'woman', 'Beijing'],
[26, 'Zhangsan', '25', 'man', 'ShanDong'],
[27, 'Lisi', '25', 'man', 'ShanDong'],
[28, 'Wangwu', '25', 'man', 'Beijing'],
[29, 'Lucy', '25', 'woman', 'JiangSu'],]
with open('data3.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(row_list)
print("第三个Excel表格已添加")
with open('data3.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # 跳过表头
for row in reader:
node = Node("Person", id=row[0], name=row[1], age=row[2], sex=row[3], address=row[4])
graph.create(node)
print("第三个Excel表格数据已写入Neo4j数据库")
```
阅读全文