python常见错误之:ValueError: Invalid \escape

本文解析了在使用Python将JSON格式数据转换为字典时遇到的常见错误,特别是当JSON字符串中包含特殊字符“/”时如何避免读取异常。通过修改字符串替换方法,确保数据正确转换。
Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

报错是在json转字典的时候,即

 url = "%s/?GetResult&x=%s&y=%s"
        resp = urllib.urlopen(url).read()
        jsonData = json.loads(resp)

在json转换时,要将 “/” 转为 “//”,即

 url = "%s/?GetResult&x=%s&y=%s"
        resp = urllib.urlopen(url).read()
        jsonData = json.loads(resp.replace('\\', '\\\\'), strict=False)

可能会/x将字符串当做16进制数据读取导致异常
很简单的报错,简单记录一下

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

<>:24: SyntaxWarning: invalid escape sequence '\d' <>:35: SyntaxWarning: invalid escape sequence '\d' <>:42: SyntaxWarning: invalid escape sequence '\d' <>:55: SyntaxWarning: invalid escape sequence '\d' <>:24: SyntaxWarning: invalid escape sequence '\d' <>:35: SyntaxWarning: invalid escape sequence '\d' <>:42: SyntaxWarning: invalid escape sequence '\d' <>:55: SyntaxWarning: invalid escape sequence '\d' C:\Users\11378\AppData\Local\Temp\ipykernel_1840\986476665.py:24: SyntaxWarning: invalid escape sequence '\d' features = chunk.filter(regex='P\d+').values C:\Users\11378\AppData\Local\Temp\ipykernel_1840\986476665.py:35: SyntaxWarning: invalid escape sequence '\d' features = self.scaler.transform(chunk.filter(regex='P\d+').values) C:\Users\11378\AppData\Local\Temp\ipykernel_1840\986476665.py:42: SyntaxWarning: invalid escape sequence '\d' features = self.scaler.transform(chunk.filter(regex='P\d+').values) C:\Users\11378\AppData\Local\Temp\ipykernel_1840\986476665.py:55: SyntaxWarning: invalid escape sequence '\d' features = self.scaler.transform(chunk.filter(regex='P\d+').values) 标准化处理: 1it [00:00, 83.17it/s] 增量训练: 1it [00:00, 77.13it/s] 阈值计算: 1it [00:00, 67.85it/s] C:\Users\11378\AppData\Local\Temp\ipykernel_1840\986476665.py:24: SyntaxWarning: invalid escape sequence '\d' features = chunk.filter(regex='P\d+').values C:\Users\11378\AppData\Local\Temp\ipykernel_1840\986476665.py:35: SyntaxWarning: invalid escape sequence '\d' features = self.scaler.transform(chunk.filter(regex='P\d+').values) C:\Users\11378\AppData\Local\Temp\ipykernel_1840\986476665.py:42: SyntaxWarning: invalid escape sequence '\d' features = self.scaler.transform(chunk.filter(regex='P\d+').values) C:\Users\11378\AppData\Local\Temp\ipykernel_1840\986476665.py:55: SyntaxWarning: invalid escape sequence '\d' features = self.scaler.transform(chunk.filter(regex='P\d+').values) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[18], line 89 86 input_file = 'D://SF//YBSJ//OTDR_data2.csv' 87 output_file = 'D://SF//k-means//OTDR_results2.csv' ---> 89 results = generate_anomalies(input_file, output_file) 90 print("\n前50个异常样本预览:") 91 print(results.head(50).to_string(index=False)) Cell In[18], line 72, in generate_anomalies(input_path, output_path, target) 69 detector = OTDRAnomalyDetector(target_anomalies=target) 71 # 三步处理流程 ---> 72 detector._streaming_fit(input_path) 73 result_df = detector._generate_output(input_path) 75 # 确保输出数量 Cell In[18], line 46, in OTDRAnomalyDetector._streaming_fit(self, file_path) 43 distances.extend(self.kmeans.transform(features).min(axis=1)) 45 distances = np.array(distances) ---> 46 self.dynamic_threshold = np.percentile(distances, 100*(1 - self.target_anomalies/len(distances))) File D:\python\Lib\site-packages\numpy\lib\_function_base_impl.py:4260, in percentile(a, q, axis, out, overwrite_input, method, keepdims, weights, interpolation) 4258 q = asanyarray(q) # undo any decay that the ufunc performed (see gh-13105) 4259 if not _quantile_is_valid(q): -> 4260 raise ValueError("Percentiles must be in the range [0, 100]") 4262 if weights is not None: 4263 if method != "inverted_cdf": ValueError: Percentiles must be in the range [0, 100]
05-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值