juypter读取文本文件、CSV文件、JSON文件、XML文件、数据库文件代码
时间: 2023-08-07 13:04:42 浏览: 95
1. 读取文本文件:
```python
with open("file.txt", "r") as f:
text = f.read()
```
2. 读取CSV文件:
```python
import csv
with open("file.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(row)
```
3. 读取JSON文件:
```python
import json
with open("file.json", "r") as f:
data = json.load(f)
```
4. 读取XML文件:
```python
import xml.etree.ElementTree as ET
tree = ET.parse("file.xml")
root = tree.getroot()
for child in root:
print(child.tag, child.attrib)
```
5. 读取数据库文件:
```python
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute("SELECT * FROM table_name")
rows = c.fetchall()
for row in rows:
print(row)
conn.close()
```
以上是基本的读取文件和数据库的代码示例,具体需要根据实际情况进行调整。
阅读全文