【问题标题】:Is there a possibility to write NaN values to a txt file as blank?是否有可能将 NaN 值作为空白写入 txt 文件?
【发布时间】:2023-03-16 12:38:01
【问题描述】:

我有以下带有列的df:

    DueDate
0   <cbc:DueDate>2020-10-18</cbc:DueDate>
1   <cbc:DueDate>2020-01-08</cbc:DueDate>
2   NaN
3   NaN

     Streetname
0    <cbc:StreetName>Xerox GmbH</cbc:StreetName>            
1    <cbc:StreetName>Rompslomp.nl B.V.</cbc:StreetName>     
2    <cbc:StreetName>STAS picture</cbc:StreetName>          
3    <cbc:StreetName>Rex International B.V.</cbc:StreetName>

     PostalAdress
0    </cac:PostalAddress>
1    </cac:PostalAddress>
2    </cac:PostalAddress>
3    </cac:PostalAddress>
Name: PostalAdressClose, dtype: object

当我尝试使用以下代码将其写入文本文件时:

# xml document to be expanding with per row details
fac_doc_template = """<?xml version="1.0"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd">
  <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
  <cbc:CustomizationID>urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0:extended:urn:www.simplerinvoicing.org:si:si-ubl:ver1.1.x</cbc:CustomizationID>
  <cbc:ProfileID>urn:www.cenbii.eu:profile:bii04:ver2.0</cbc:ProfileID>
  {fac_details}"""

# per row details
# todo: expand for all of the column values you want
fac_details_xml_template = """{Streetname}
  {DueDate}
  """

然后我遍历列以使用以下代码将每个列写入单独的文件:

def series_to_fac_details_xml(s):
    return fac_details_xml_template.format(**s)

for index, row in df3.iterrows():
    details = series_to_fac_details_xml(row)
    with open(fr"C:\Users\Max12\Desktop\xml\pdfminer\UiPath\output\{index}.xml", "w") as f:
        f.write(fac_doc_template.format(fac_details=details))

我有一个问题.. 我希望在值为 NaN 时跳过 NaN,但是当我使用以下方法将 NaN 转换为空字符串时:

df3 = df3.replace(np.nan, '', regex=True)

我在输出文件中得到白线。当出现 NaN 时,所需的输出是下一列写入文件的立即继续(无空格)。你能帮帮我吗?

【问题讨论】:

    标签: python regex pandas nan


    【解决方案1】:

    假设你有这个 DataFrame:

    import numpy as np
    import pandas as pd
    df = pd.DataFrame({'DueDate':   ['2020-01-01','2020-01-02',np.nan], 
                       'Streetname':['Main Street 1', 'Main Street 2', 'Main Street 3']
                      })
    
    df
    >>>
          DueDate     Streetname
    0  2020-01-01  Main Street 1
    1  2020-01-02  Main Street 2
    2         NaN  Main Street 3
    

    您可以像替换 NaN 那样替换 df = df.replace(np.nan,'', regex=True)

    之后我建议你执行apply 函数并创建一个新系列来组成你的阵型。

    z = df.apply(lambda x: x['Streetname'] + ' ' + x['DueDate'], axis=1)
    

    稍后您可以致电z.to_string(index=False) 并将其写入您的文件。如果您不喜欢换行符,您可以使用 z.to_string(index=False).replace('\n','') 替换它们。我认为这会稍微清理您的代码,因为您不必遍历所有行。

    我真的希望这对您有所帮助,并回答您的问题。

    【讨论】:

      猜你喜欢
      • 2021-03-27
      • 2020-10-12
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多