我有一个这样的文本文件。你知道吗
1 firm A Manhattan (company name) 25,000
SK Ventures 25,000
AEA investors 10,000
2 firm B Tencent collaboration 16,000
id TechVentures 4,000
3 firm C xxx 625
(and so on)
我想做一个矩阵表格,把每一项都放到矩阵里。 例如,矩阵的第一行如下所示:
[[1,A公司,曼哈顿,25000],'','',SK Ventures,25000],'','',AEA investors,10000]]
或者
[[1,',',',',',',',',','公司A,',',',',','曼哈顿,SK Ventures,AEA Investors,',[25000,25000,10000]]
为此,我要解析文本文件每行的文本。例如,从第一行开始,我可以创建[1,公司A,曼哈顿,25000]。但是,我不知道该怎么做。每一个文本都从同一个位置开始,但在不同的位置结束。有什么好办法吗?你知道吗
谢谢你。你知道吗
如果我对你的理解正确(虽然我不完全确定我是否理解),这将产生我认为你需要的结果。你知道吗
程序输出:
我是基于你希望矩阵的第一行是:
[[1,Firm A,Manhattan,25,000],['',SK Ventures,25,000],['',AEA investors,10,000]]
然而,为了用更多的行来实现这一点,我们得到了一个嵌套3层的列表。这就是
print(matrix)
的输出。这可能有点难以使用,这就是为什么TessellatingHeckler的答案使用字典来存储数据,我认为这是一个更好的方式来访问你需要的。但是如果你想要的是“矩阵”列表,那么我上面写的代码就是这样做的。你知道吗根据您所给出的data*,如果行以数字或空格开头,则输入会发生变化,并且可以将数据分隔为
(数字)(空格)(字母加1空格)(空格)(字母加1空格)(空格)(数字+逗号)
或者
(空格)(字母加1空格)(空格)(数字+逗号)
这就是下面的两个正则表达式所要寻找的,它们用前导数字的索引构建了一个字典,每个索引都有一个公司名称和一个公司和值对列表。你知道吗
我真的不知道你的矩阵安排是什么。你知道吗
->
*这适用于您的示例,但可能无法很好地适用于您的实际数据。基督教青年会。你知道吗
如果你知道所有的开始位置:
相关问题 更多 >
编程相关推荐