Python_报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape - 翻滚的小强 - 博客园 (cnblogs.com)https://www.cnblogs.com/rychh/p/9743864.html经常会遇到数据的读取,数据的读取是数据处理使用pandas数据读取汇总,以泰坦尼克号的数据为样本,具体见方法.
方法一:
import os
import urllib.request
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers
from sklearn import preprocessing
import matplotlib.pyplot as plt
网页直接下载获取:
#下载的网页路径
url='http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.xls'
filepath="titanic3.xls" #文件路径
if not os.path.isfile(filepath):
result = urllib.request.urlretrieve(url,filepath) #该函数可以将远程数据下载到本地
print(result)
方法二:在数据已经在离线电脑里了如在绝对地址filepath="C:\Users\Victor\Titanic\train.csv",当前地址里直接有 train.csv,使用当前地址方法:
train = pd.read_csv("train.csv",header=0) # one way to read
print(train.describe())
方法三:使用绝对地址方法:
f=open(r'C:\Users\Victor\Titanic\train.csv')#加上r,声明字符串,不用转义处理
df=pd.read_csv(f)
方法四:直接绝对地址使用
filepath="C:\\Users\\Victor\\Titanic\\train.csv"
all_df = pd.read_csv(filepath)
所有方法的结果如下图:
对于读取离线地址的绝对地址使用主要有三种方法:
一:更换为绝对路径的写法
func1("C:\\Users\\renyc")
二:显式声明字符串不用转义(加r)
func1(r"C:\Users\renyc")
三:使用Linux的路径/
func1("C:/Users/renyc")
参考链接:Pandas数据分析①——数据读取(CSV/TXT/JSON)_cindy407的博客-CSDN博客_pandas读取csvhttps://blog.csdn.net/cindy407/article/details/90747049?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~OPENSEARCH~default-1.highlightwordscore&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~OPENSEARCH~default-1.highlightwordscore Python_报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape - 翻滚的小强 - 博客园 (cnblogs.com)https://www.cnblogs.com/rychh/p/9743864.html