使用Python将列表转换为CSV文件中的单行的列表,并带有适当的列标题

2024-05-09 08:29:32 发布

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

我正在使用Python和beauthoulsoup为我的教授从城市财务报告中提取财务报表信息。我已经得到了下面列表的输出。我需要能够将值写入CSV文件的一行中,这样就可以在SAS中分析它们。我将运行我的程序超过750个财务报表,所以我需要的代码是相当健全的。理想情况下,我希望输出如下所示:

输出示例

(第1行)“政府资产流动资产现金及现金等价物”

“057年6月30日”,4052年6月

“资产”最好出现在每个列标题中, “当前资产”将变为“非流动资产”,当python不受限制地循环通过它时,每个行标题都会改变“政府”将出现在第一列中所有值的标题栏中,第二栏中的“商业类型”和第三栏中的“总计”。在

例如,http://www.osc.ct.gov/2002cafr/financial/basic/netassets.asp

我认为“枚举”会起作用,因为它似乎只对列表有用,而不是列表列表。我知道我可以在我的代码中为每个可能的场景指定它,比如if text==“ASSETS”,但这看起来不是很python。我认为必须有一种方法告诉程序在新的部分出现之前继续使用其中一个列标题。示例
‘现金和现金等价物’应该一直使用,直到项目达到‘可回收’为止,然后‘应收账款’应该取代它。”“政府”应一直使用到第一列值的底部,然后应使用“商业类型”。在

如果你能在这件事上给我任何帮助,我将不胜感激。在

列表

[[u'Statement of Net Assets']
[u'June 30 2002', '', '', '', '']
[u'-Expressed in Thousands', '', '', '', '']
[u'Primary Government']
[u'Governmental', u'Business-Type', '', u'Component']
[u'Activities', u'Activities', u'Total', u'Units']
[u'Assets']
[u'Current Assets:', u' ', u' ', u' ', u' ']
[u'Cash and Cash Equivalents', u'$405057', u'$486600', u'$891657', u'$536609']
[u'Deposits with U.S. Treasury', u'0', u'675562', u'675562', u'0']
[u'Investments', u'181405', u'250670', u'432075', u'120078']
[u'Receivables -Net of Allowances', u'1841932', u'450954', u'2292886', u'165888']
[u'Due From Component Units', u'0', u'99611', u'99611', u'0']
[u'Due From Primary Government', u'0', u'0', u'0', u'20346']
[u'Inventories', u'61130', u'10814', u'71944', u'3543']
[u'Restricted Assets', u'0', u'9420', u'9420', u'451057']
[u'Internal Balances', u'-145078', u'145078', u'0', u'0']
[u'Other Current Assets', u'13821', u'8910', u'22731', u'12353']
[u'Total Current Assets', u'2358267', u'2137619', u'4495886', u'1309874']
[u'Noncurrent Assets:', u' ', u' ', u' ', u' ']
[u'Cash and Cash Equivalents', u'0', u'63073', u'63073', u'0']
[u'Restricted Assets', u'590374', u'695704', u'1286078', u'425372']
[u'Investments', u'0', u'448063', u'448063', u'234383']
[u'Loans -Net of Allowances', u'406272', u'505043', u'911315', u'3068708']
[u'Capital Assets -Net of Accumulated Depreciation', u'9125804', u'2306065',         
u'11431869', u'252286']
[u'Other Noncurrent Assets', u'14388', u'81532', u'95920', u'59925']
[u'Total Noncurrent Assets', u'10136838', u'4099480', u'14236318', u'4040674']
[u'Total Assets', u'12495105', u'6237099', u'18732204', u'5350548']
[u'Liabilities', u' ', u' ', u' ', u' ']
[u'Current Liabilities:', u' ', u' ', u' ', u' ']
[u'Accounts Payable and Accrued Liabilities', u'710270', u'194520', u'904790', 
u'52986']
[u'Due To Component Units', u'20346', u'0', u'20346', u'0']
[u'Due To Primary Government', u'0', u'0', u'0', u'99611']
[u'Escrow Deposits', u'0', u'0', u'0', u'26347']
[u'Current Portion of Long-Term Obligations', u'976958', u'168936', u'1145894', 
u'118451']
[u'Amount Held for Institutions', u'0', u'0', u'0', u'279817']
[u'Deferred Revenue', u'39159', u'59335', u'98494', u'680']
[u'Medicaid Liability', u'577150', u'0', u'577150', u'0']
[u'Liability for Escheated Property', u'51178', u'0', u'51178', u'0']
[u'Other Current Liabilities', u'160333', u'65563', u'225896', u'18271']
[u'Total Current Liabilities', u'2535394', u'488354', u'3023748', u'596163']
[u'Noncurrent Liabilities:', u' ', u' ', u' ', u' ']
[u'Non-Current Portion of Long-Term Obligations', u'14576670', u'1948712', u'16525382', 
u'3667265']
[u'Total Noncurrent Liabilities', u'14576670', u'1948712', u'16525382', u'3667265']
[u'Total Liabilities', u'17112064', u'2437066', u'19549130', u'4263428']
[u'Net Assets', u' ', u' ', u' ', u' ']
[u'Invested in Capital Assets Net of Related Debt', u'2348364', u'1847526', u'4195890', 
u'44126']
[u'Restricted For:', u' ', u' ', u' ', u' ']
[u'Transportation', u'169228', u'0', u'169228', u'0']
[u'Debt Service', u'553530', u'103933', u'657463', u'20229']
[u'Capital Projects', u'0', u'144982', u'144982', u'0']
[u'Unemployment Compensation', u'0', u'798703', u'798703', u'0']
[u'Clean Water Projects', u'0', u'402281', u'402281', u'0']
[u'Bond Indenture Requirements', u'0', u'22425', u'22425', u'609058']
[u'Other Purposes', u'419135', u'196465', u'615600', u'27817']
[u'Funds Held as Permanent Investments:', u' ', u' ', u' ', u' ']
[u'Expendable', u'5924', u'0', u'5924', u'0']
[u'Nonexpendable', u'83598', u'177343', u'260941', u'0']
[u'Unrestricted -Deficit', u'-8196738', u'106375', u'-8090363', u'385890']
[u'Total Net Assets -Deficit', u'$-4616959', u'$3800033', u'$-816926', u'$1087120']]

Tags: of列表netcashcurrent资产totaldue
1条回答
网友
1楼 · 发布于 2024-05-09 08:29:32

您好,您可以访问以下信息:

import csv
description = table_timeline_inner['user']['description']
writer = csv.writer(open('stocks.csv', 'a', buffering=0))
            writer.writerows([(description, hashtags])

对于表中的表,使用[][] 你可以用csv.写入-“a”使它不会重写任何内容

相关问题 更多 >