python - Python2 中的 Python3 f 字符串替代
问题描述
我有这个 python3 代码(用f
字符串格式化):
folder = r"C:\Users\test"
for _,g in df.groupby(df['ID'].notna().cumsum()):
g.iloc[:,1:].dropna(how='all').to_csv(f"{folder}\\{g.iloc[0,0]}.txt",index=False)
我正在尝试在 python2.7 中格式化它:
python2.7中的mycode:
folder = r"C:\Users\test"
for _,g in df.groupby(df['ID'].notna().cumsum()):
g.iloc[:,1:].dropna(how='all').to_csv("{}".format(folder+\\(g.iloc[0,0])+str(".txt")),index=False)
我有这个错误:
我做错了什么?感谢您的关注和帮助。
解决方案
您必须将变量从{ }
to移动format()
并保持在字符串中
"{}\\{}.txt".format(folder, g.iloc[0,0])
代替
f"{folder}\\{g.iloc[0,0]}.txt"
您可以在https://pyformat.info/上了解更多信息
推荐阅读
- python - boolean.conjugate() 有什么用?
- vba - MSXML2.ServerXMLHTTP.6.0 PATCH 请求的 Open 方法存在问题
- linux - /proc/ 中的 VmLck(锁定内存)与 VmPin(固定内存)
/地位 - sqf - 使用 SQF 在 Arma 3 中编辑虚拟军械库
- python - 从字符串中获取时间
- javascript - 如何导出变量\数组\函数?
- docker - 断开 SSH 后如何杀死/停止远程 Docker 容器
- php - Laravel:注册和登录不能在在线服务器上工作
- c# - C# UWP 如何从另一个进程更改内容控制背景?
- nuget - Nuget 在输出中不包含辅助(许可证)文件