【发布时间】:2020-06-14 14:25:50
【问题描述】:
我有这个 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 中的我的代码:
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)
我遇到了这个错误:
我做错了什么?感谢您的关注和帮助。
【问题讨论】:
-
试试这个,
"{folder}\\{file}.txt".format(folder=folder, file=g.iloc[0,0]) -
\\不是运算符;它是字符串文字的一部分。 -
你必须在字符串
"{}\\{}.txt".format(folder, g.iloc[0,0])中使用\\ -
顺便说一句:见页面pyformat.info
-
@sushant,谢谢你我尝试了你的建议,但我得到了错误 'AttributeError: 'Series' object has no attribute 'notna'
标签: python python-3.x pandas python-2.7