【问题标题】:How can I edit a block of text in memory using python before writing the text to a file?在将文本写入文件之前,如何使用 python 编辑内存中的文本块?
【发布时间】:2015-06-22 02:34:56
【问题描述】:

我有一个脚本可以从 SEC 的 EDGAR 数据库中下载文本块数据。数据提取准确。但是,文本包含多个连续空格 (x20) 和 CRLF (x0A xOD)。

我需要能够删除逗号和多余的 CRLF 和空格,然后将整个文本内容写入 CSV 文件以供以后分析。

我不是 python 程序员,但我使用 python 来完成这个任务,因为 XBRL 解析程序有一个 python 接口。

我需要为大约 6,000 次个人观察执行此任务,因此我不想手动尝试。

我进行了广泛的搜索,包括购买和阅读两本 python 教科书,但在尝试将文本写入 CSV 文件之前,我无法确定如何编辑文本。

这是在写入文件之前原始数据的代表性打印输出。请注意,应该有 5 个逗号分隔的字段,日期之后的所有内容都写入单个单元格。

DocumentType EntityName CIK PeriodEndDate PPE_Policy 10-K CONOLOG 公司 23503 2012 年 7 月 31 日

财产和设备


财产和设备按成本入账
                  less allowances for depreciation. Depreciation is computed by

                  the straight-line method over the estimated useful lives of

                  the assets which range between three (3) and thirty-nine(39)

                  years. Depreciation was $16,560 and $14,598 for the fiscal 

                  years ended July 31 2012 and 2011 respectively. Repairs and

                  maintenance expenditures which do not extend the useful lives

                  of the related assets are expensed as incurred. Gains and

                  losses on depreciable assets retired or sold are recognized

                  in the consolidated statement of operations in the year of

                  disposal</font></p>

【问题讨论】:

    标签: python csv scripting text-parsing


    【解决方案1】:

    我不确定您已经尝试过什么,但是如果您下载该文档并将其分配给一个变量,那么您可以对该文档执行字符串操作。例如(在pseduo-python中):

    doc = downloaded_xbrl
    edited_doc = doc.replace('\x20','') --removes x20, replaces with nothing
    csv.write(edited_doc)
    

    python 文档的链接:https://docs.python.org/2/library/string.html#string-formatting

    【讨论】:

      猜你喜欢
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多