python如何读取以分号分割的记事本数据?
更新时间:2018-11-12 04:50:26
1319次访问
date;Convertible Arbitrage;CTA Global;Distressed Securities;Emerging Markets;Equity Market Neutral;Event Driven;Fixed Income Arbitrage;Global macro;Long/Short Equity;Merger Arbitrage;Relative Value;Short Selling;Funds Of Funds
31/01/1997;1.19%;3.93%;1.78%;7.91%;1.89%;2.13%;1.91%;5.73%;2.81%;1.50%;1.80%;-1.66%;3.17%
28/02/1997;1.23%;2.98%;1.22%;5.25%;1.01%;0.84%;1.22%;1.75%;-0.06%;0.34%;1.18%;4.26%;1.06%
31/03/1997;0.78%;-0.21%;-0.12%;-1.20%;0.16%;-0.23%;1.09%;-1.19%;-0.84%;0.60%;0.10%;7.78%;-0.77%
30/04/1997;0.86%;-1.70%;0.30%;1.19%;1.19%;-0.05%;1.30%;1.72%;0.84%;-0.01%;1.22%;-1.29%;0.09%
第一行14个变量名,代表14列。。。下面是对应的14列数值。。。。这个如何用python读取数据并转换成一个表格式数据(DataFrame)
其实比较简单啦:
我把你给的文件放在 data.txt里面了,文件存储编码为utf-8
代码如图:
运行结果:
代码:
=====================code=====================
from pandas import DataFrame
import io
lineList = io.open("data.txt", encoding="utf-8").readlines()
# print(lineList)
df = DataFrame(columns=lineList[0].split(";"))
for i in range(1, len(lineList)):
df.loc[i] = lineList[i].split(";")
print(df)
=====================code=====================
有不懂得请追问, 满意请采纳,谢谢