在将文本写入文件之前,如何使用python编辑内存中的文本块?

2024-04-25 11:31:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

我需要为大约6000个单独的观察做这个任务,所以我不想手动尝试。你知道吗

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

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

DocumentType EntityName CIK PeriodEndDate PPE\U策略 10-K CONOLOG CORP 23503 7/31/2012物业和设备


财产和设备按成本计价
                  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>

Tags: and文件ofcsvthe数据文本内容
1条回答
网友
1楼 · 发布于 2024-04-25 11:31:48

我不确定您已经尝试了什么,但是如果您下载文档并将其分配给一个变量,则可以对该文档执行字符串操作。例如(在psedoo 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

相关问题 更多 >