Python Pandas 在导入 ODS 文件时删除换行符,我想保留它们

发布于 2025-01-17 04:10:29 字数 626 浏览 3 评论 0原文

Python 3.8.10
Pandas 1.4.1

大家好,

我有一个 ods(odf) 格式的电子表格。 我正在使用 pandas 导入此数据,它似乎删除了所有换行符,但我想保留它们。

此测试脚本复制了该问题:

#!/usr/bin/env python3

import pandas as pd

sheet = pd.read_excel("./test.ods", engine='odf')

print(sheet)
print('--------')
print(sheet.loc[0]['A'])

test.ods 如下所示: 图像链接

输出如下:

                A                   B   C
0  TestAbcEfgHijk  lallala12345121212  12
--------
TestAbcEfgHijk

我在做一些愚蠢的事情还是这是一个错误?

编辑:
我使用的是 Linux,如果有什么区别的话

Python 3.8.10
Pandas 1.4.1

Hi everyone,

I have a spread sheet in ods(odf) format.
I am importing this data using pandas and it seems to be removing all newline characters but I want to keep them.

This test script replicates the issue:

#!/usr/bin/env python3

import pandas as pd

sheet = pd.read_excel("./test.ods", engine='odf')

print(sheet)
print('--------')
print(sheet.loc[0]['A'])

test.ods looks like this:
link to image

output is like this:

                A                   B   C
0  TestAbcEfgHijk  lallala12345121212  12
--------
TestAbcEfgHijk

Am I doing something dumb or is this a bug?

edit:
I am on Linux if it makes any difference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2025-01-24 04:10:29

是的,这似乎是 pandas 使用的 odfpy 中的一个错误 https://github.com/eea/ odfpy/issues/114
如果我将文件保存为 xlsx 那么我就没有问题。
暂时我会使用xlsx。

这是一些显示 odfpy 中问题的代码。

#!/usr/bin/env python3

import sys
from odf.opendocument import load
from odf.table import Table, TableRow, TableCell

infile = sys.argv[1]
doc = load(infile)

cell= doc.getElementsByType(Table)[0].getElementsByType(TableRow)[1].getElementsByType(TableCell)[0]

print(cell)

Yeah this seems to be a bug in odfpy used by pandas https://github.com/eea/odfpy/issues/114.
If I save the file as xlsx then I have no issues.
For the time being I will use xlsx.

Here's some code showing the issue in odfpy.

#!/usr/bin/env python3

import sys
from odf.opendocument import load
from odf.table import Table, TableRow, TableCell

infile = sys.argv[1]
doc = load(infile)

cell= doc.getElementsByType(Table)[0].getElementsByType(TableRow)[1].getElementsByType(TableCell)[0]

print(cell)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文