考虑以下最小示例:
@dataclass
class ExportEngine:
def __post_init__(self):
self.list = pandas.DataFrame(columns=list(MyObject.CSVHeaders()))
def export(self):
self.prepare()
self.list.to_csv("~/Desktop/test.csv")
def prepare(self):
values = numpy.concatenate(
(
numpy.array(["Col1Value", "Col2Value", " Col3Value", "Col4Value"]),
numpy.repeat("", 24),
)
)
for x in range(8): #not the best way, but done due to other constraints
start = 3 + (x * 3) - 2
end = start + 3
values[start:end] = [
"123",
"some_random_value_that_gets_truncated",
"456",
]
self.list.loc[len(self.list)] = values
当调用export()
时,some_random_value_that_gets_truncated
被截断为some_rando
:
['Col1Value', '123', 'some_rando', '456', '123', 'some_rando', '456', '123', 'some_rando', '456', '123', 'some_rando', '456', '123', ...]
我尝试设置以下内容:pandas.set_option("display.max_colwidth", 10000)
,但这并没有改变任何东西...
为什么会发生这种情况,如何防止截断?
2条答案
按热度按时间3qpi33ja1#
因此,
numpy
将默认选择合适的固定长度的unicode格式。请注意dtype:
你可能不应该直接使用numpy,但一个快速解决方案是替换:
与:
注意
dtype=object
,它只使用指向pythonstr
对象的指针,因此对字符串的长度没有限制pdsfdshx2#
这里有一个代码的替代方案,它不依赖于numpy(从而避免了numpy的固定宽度unicode字符串类型):