python-3.x - 将单引号添加到变量的值
问题描述
假设我们有一个变量 foo = "text"
。您将如何将其转换为 foo = '"text"'
? 有没有办法在不使用库的情况下做到这一点 urllib2
?
编辑:下面是一个脚本:
def most_common(lst):
if(len(lst) > 0):
return max(set(lst), key=lst.count)
else:
return 0
test_list = list(df['a'])) # a is a column that can take the the values '"apple"', '"pear"', '"carrot"'
test_list_most = most_common(test_list) # returns "apple"
但是假设我们要过滤数据框:
df = len(df[df['a'] == test_list_most].index) # length would be 0
这就是这个问题的上下文以及为什么我们要在双引号周围添加单引号。
解决方案
你的意思是:
print(f"'{foo}'")
输出:
'text'
或者:
print(f'"{foo}"')
输出:
"text"
推荐阅读
- javascript - await 单个 Promise 与异步迭代器的异步解析顺序
- google-colaboratory - 机器学习和模型训练
- google-apps-script - 我正在使用取决于相对列的 If 语句 - 无法使其工作
- python - 如果两列在三列中具有相同的值,则无法在 pandas 中获得结果,然后保留第一个 col 值,否则其他值取决于条件
- d - 如何覆盖默认结构构造函数
- selenium - 为什么 Selenium webdriver 没有打开网页?
- swift - 无法将类型“()”的返回表达式转换为返回类型“T”
- html - 如果单词内容增加,如何到达p标签的下一行
- ios - SwiftUI 中的平面导航最佳实践
- sql - T-SQL:执行批处理时出错。错误消息是:从 MyTable 中选择 * 的算术溢出