模仿 NCHAR(X) 的 Pandas/Python dtype?

问题描述

我想读取一个 Excel 并传递像 CHAR(X) 这样的 DTYPES 来告诉用空格填充其余部分的列,以创建一个 txt 固定宽度(其中每列都有不同的宽度)。

我现在正在这样做。

dictp7 = {
    "Tipo Registro": np.dtype("str"),"Tipo Registro 2": np.dtype("str")}

df = pd.read_excel("lote.xlsx",dtype=dictp7)

df["Tipo Registro"] = df["Tipo Registro"].astype("string").str.rjust(11," ")
df["Tipo Registro 2"] = df["Tipo Registro 2"].astype("string").str.rjust(160," ")
open("lote.txt","w").write(df.to_string(header=False))

(这是简短版本) 我对每一列都这样做,它有效。

但也许有更好的解决方案,直接从指定的 dtypes 中获取

知道这行不通,但类似于:

dictp7 = {
    "Tipo Registro": str.rjust(11)}

会很好

还有这个:

dictp7 = {
    "Tipo Registro": str.rjust(11).fillna(' ' * 11)}

会很棒。

为什么要这样做?
好吧,因为该文件必须以特定的固定宽度格式(并且每列的宽度不同)呈现,才能使该过程正常工作。 不,我不能改变这个过程的工作方式。我只能遵循指导方针。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)