path = "../data/cities.csv" # 随便找的一个csv文件
import csv
f = open(path)
data = csv.reader(f) # ①
for line in data:
print(line)
['name', ' area', ' population', ' longd', ' latd']
['Nanjing', ' 6582.31', ' 8004680', ' 118.78', ' 32.04']
['Wuxi', ' 4787.61', ' 6372624', ' 120.29', ' 31.59']
['Xuzhou', ' 11764.88', ' 8580500', ' 117.2', ' 34.26']
['Changzhou', ' 4384.57', ' 4591972', ' 119.95', ' 31.79']
['Soochow', ' 8488.42', ' 10465994', ' 120.62', ' 31.32']
['Nantong', ' 8001', ' 7282835', ' 120.86', ' 32.01']
['Lianyungang', ' 7615.29', ' 4393914', ' 119.16', ' 34.59']
['Huaian', ' 9949.97', ' 4799889', ' 119.15', ' 33.5']
['Yancheng', ' 16972.42', ' 7260240', ' 120.13', ' 33.38']
['Yangzhou', ' 6591.21', ' 4459760', ' 119.42', ' 32.39']
['Zhenjiang', ' 3840.32', ' 3113384', ' 119.44', ' 32.2']
['Taizhou', ' 5787.26', ' 4618558', ' 119.9', ' 32.49']
['Suqian', ' 8555', ' 4715553', ' 118.3', ' 33.96']
import pandas as pd
# 使用pandas库读取csv文件
df = pd.read_csv(path)
df
name | area | population | longd | latd | |
---|---|---|---|---|---|
0 | Nanjing | 6582.31 | 8004680 | 118.78 | 32.04 |
1 | Wuxi | 4787.61 | 6372624 | 120.29 | 31.59 |
2 | Xuzhou | 11764.88 | 8580500 | 117.20 | 34.26 |
3 | Changzhou | 4384.57 | 4591972 | 119.95 | 31.79 |
4 | Soochow | 8488.42 | 10465994 | 120.62 | 31.32 |
5 | Nantong | 8001.00 | 7282835 | 120.86 | 32.01 |
6 | Lianyungang | 7615.29 | 4393914 | 119.16 | 34.59 |
7 | Huaian | 9949.97 | 4799889 | 119.15 | 33.50 |
8 | Yancheng | 16972.42 | 7260240 | 120.13 | 33.38 |
9 | Yangzhou | 6591.21 | 4459760 | 119.42 | 32.39 |
10 | Zhenjiang | 3840.32 | 3113384 | 119.44 | 32.20 |
11 | Taizhou | 5787.26 | 4618558 | 119.90 | 32.49 |
12 | Suqian | 8555.00 | 4715553 | 118.30 | 33.96 |
path = "../data/多分类鸢尾花.xlsx"
flower_df = pd.read_excel(path)
flower_df.head()
萼片长 | 萼片宽 | 花瓣长 | 花瓣宽 | 种类 | |
---|---|---|---|---|---|
0 | 5.0 | 3.3 | 1.4 | 0.2 | 山鸢尾 |
1 | 6.7 | 3.1 | 4.4 | 1.4 | 变色鸢尾 |
2 | 6.3 | 2.7 | 4.9 | 1.8 | 维吉尼亚鸢尾 |
3 | 4.4 | 2.9 | 1.4 | 0.2 | 山鸢尾 |
4 | 7.7 | 2.6 | 6.9 | 2.3 | 维吉尼亚鸢尾 |
# 使用pillow库
from PIL import Image # ○16
color_image = Image.open("../data/starryNight.jpg") # ○17
color_image
Image.open
的参数可以是文件路径fp
,也可以的字节数组bytes
,下面演示从图床中读取文件并展示
import requests
from io import BytesIO
req = requests.get("https://pic-1257412153.cos.ap-nanjing.myqcloud.com/beautiful/大炎,赦封神明!_87744911.jpg")
if req.status_code != 200:
print("图片请求错误,请尝试加入header")
byte_img = BytesIO(req.content)
img = Image.open(byte_img)
img
读取方式:imread(filename, flag)
cv::ImreadModes {
cv::IMREAD_UNCHANGED = -1,
cv::IMREAD_GRAYSCALE = 0,
cv::IMREAD_COLOR = 1,
cv::IMREAD_ANYDEPTH = 2,
cv::IMREAD_ANYCOLOR = 4,
cv::IMREAD_LOAD_GDAL = 8,
cv::IMREAD_REDUCED_GRAYSCALE_2 = 16,
cv::IMREAD_REDUCED_COLOR_2 = 17,
cv::IMREAD_REDUCED_GRAYSCALE_4 = 32,
cv::IMREAD_REDUCED_COLOR_4 = 33,
cv::IMREAD_REDUCED_GRAYSCALE_8 = 64,
cv::IMREAD_REDUCED_COLOR_8 = 65,
cv::IMREAD_IGNORE_ORIENTATION = 128
}
# 使用opencv库查看图片
import cv2
# 得到的是numpy数组
img = cv2.imread("../data/starryNight.jpg", -1) # 不改变
img
array([[[ 53, 90, 104],
[ 31, 34, 39],
[ 54, 39, 36],
...,
[124, 161, 183],
[138, 170, 189],
[122, 150, 167]],
[[ 65, 89, 101],
[ 32, 20, 26],
[ 54, 25, 21],
...,
[138, 172, 195],
[134, 166, 185],
[132, 159, 179]],
[[ 79, 85, 96],
[ 53, 26, 30],
[ 84, 37, 33],
...,
[121, 156, 176],
[131, 162, 183],
[134, 164, 183]],
...,
[[ 90, 149, 164],
[121, 128, 148],
[110, 129, 144],
...,
[134, 171, 193],
[116, 154, 178],
[140, 177, 203]],
[[ 99, 174, 183],
[ 86, 105, 120],
[121, 146, 156],
...,
[114, 148, 171],
[103, 139, 163],
[144, 179, 205]],
[[ 69, 149, 160],
[102, 125, 140],
[136, 159, 167],
...,
[ 99, 133, 156],
[102, 136, 159],
[142, 176, 200]]], dtype=uint8)
import matplotlib.pyplot as plt
%matplotlib inline
# %config InlineBackend.figure_format = 'svg'
plt.imshow(img)
plt.xticks([]), plt.yticks([])
(([], []), ([], []))
注意到图片并不是我们想要的效果
这是因为:opencv的接口使用BGR,而matplotlib.pyplot 则是RGB模式
b,g,r = cv2.split(img)
img2 = cv2.merge([r,g,b])
plt.imshow(img2)
plt.xticks([]), plt.yticks([])
plt.show()