如何将文件写入文件名未编码的文件系统?
目前,写入 /myfolder/my file.csv
变为 /myfolder/my%20file.csv
。我希望文件名是my file.csv
。
代码片段:
rs = (grequests.get(u, headers=headers, stream=True, hooks={'response': addFilenameToResponse}) for u in urls)
sp_results = grequests.map(rs)
for res in sp_results:
byteStream = io.BytesIO(res.content)
result_df = pd.read_csv(byteStream, sep=",", encoding='utf8')
outputloc = downloads_folder + res.filePathFull + '/' + res.filename
outputloc = outputloc.replace('/', '\\') # switching for Windows 10
result_df.to_csv(outputloc, index=False, encoding=None)
...
我尝试将 to_csv
输出为 encoding=None
但这不起作用。除此之外,the docs似乎没有显示任何其他方法。
问题是,我们然后从另一个旧版工具中读取文件名,并且该工具对编码的文件名进行重新编码,从而生成 /myfolder/my%2520file.csv
(%
编码为 %25
)。
最佳答案
添加 grequests
部分似乎澄清了问题(与 pandas
无关)。
%20
is a url encoding for the space character 。要删除它,您可以使用 urllib.parse.unquote
。在Python3中:
from urllib import parse
...
outputloc = parse.unquote(outputloc)
result_df.to_csv(outputloc, index=False, encoding=None)
关于python - pandas to_csv — 不编码文件名或路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50051492/