python-3.x - 从非正式字符串表示中获取干净的字符串
问题描述
我无法将字符串拆分为行。我收到一个似乎被定义为的字符串
txt = str(b"line1 \n line2")
正如这里所描述的,它被转换为字符串的非正式表示。
就像我收到一个定义为的脚本
txt = "b'line1 \n line2'"
如何将我的数据转换为
txt = "line1 \n line2"
不做丑陋的事情
txt = txt[2:-1]
编辑:看到标记为重复,但另一个问题似乎不适用:
txt = str(b"line1\nline2")
txt.decode('ascii')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-89-0ccdab40ee23> in <module>
----> 1 txt.decode('ascii')
AttributeError: 'str' object has no attribute 'decode'
解决方案
推荐阅读
- static-files - 在渲染之前替换 Spark.staticFileLocation(path) 中的字符串/内容
- node.js - 在服务器上安装 node.js 时 nodejs/bin/node 不存在
- javascript - 为什么在使用对象属性进行查询时从 MongoDB 获取一个空数组?
- powershell-remoting - 为远程服务器运行 WinRM quickconfig
- html - mouseover - 当 sideNav 打开时更改元素
- sql - 如何在 Hive 中加入具有多列的单个表?
- r - 将 data.table 因子列转换为字符列的最快方法
- python - 有没有办法通过 Airflow API 创建/修改连接
- android - 无法在 Android Studio 中创建图像资源
- node.js - 尝试使用谷歌示例创建公司时出错