Open littleblubber opened 2 months ago
在 Polars 中,你无需使用 with open()
来打开文件进行写入操作。Polars 提供了直接写入文件的方法,比如 df.write_excel()
(或其他格式),该方法会直接处理文件路径。
正确的用法是:
df.write_excel("dataframe.xlsx")
这样,Polars 会自动处理文件的打开和关闭,而不需要手动通过 with open()
进行处理。因此,使用 with open()
是不必要的。
In Polars, you don't need to use with open()
to open a file for a write operation.Polars provides methods to write directly to a file, such as df.write_excel()
(or other formats), which will deal with the file path directly.
The correct usage is:
``python df.write_excel(“dataframe.xlsx”)
This way, Polars handles the opening and closing of the file automatically, rather than having to do it manually with `with open()`. Therefore, using `with open()` is not necessary.
Translated with DeepL.com (free version)
@v-wei40680 sorry, can you post in English if possible? Here is a translation of your post (using Google Translate):
In Polars, you don't need to use
with open()
to open files for writing. Polars provides methods for writing files directly, such asdf.write_excel()
(or other formats), which directly handle file paths.The correct usage is:
df.write_excel("dataframe.xlsx")
Thanks both; that's correct, however, that solution does not work if I'm using fsspec/s3fs file system classes to open the file and write.
As mentioned above, this means that df.write_excel
behaviour is not compatible with other write methods which support working with file system classes.
Hello new here. Can i take this up?
Checks
Reproducible example
Log output
Issue description
_xl_setup_workbook
incorrectly defaults to treating file objects likeio.BufferedWriter
,fsspec.implementations.local.LocalFileOpener
, ors3fs.S3File
as paths.This is because the
isinstance(workbook, BytesIO)
condition condition isFalse
for these objects.By contrast,
write_csv
andwrite_json
methods work fine with file-like objects such as the ones mentioned above.Expected behavior
Similar to
write_csv
orwrite_json
, the expected behaviour in_xl_setup_workbook
is to explicitly check that theworkbook
is a string/path-like object, otherwise defaulting to treating theworkbook
as file-like.I think this could be accomplished by modifying the existing if-else statement along the following lines:
Installed versions