我是Python的新手,我已经发现这里的帖子非常有用,但现在我被卡住了。我已经从电子邮件中解析了交易数据并将其保存到一个字符串对象中,如下所示:
= E2 = 84 = 96 \ tOrderID \ tInstrument / ISIN \ tDirection \ tQuantity \ t = \ nPrice \ tAmount \ tDeal time \ tSlippage \ t确认时间\ t = \ n结算时间\ tCommission \ t收取费用\ t其他\ n> ; 1 \ tPO59332737 \ tOil-20Sep17 \ tBuy \ t100 \ t46.100000 \ t = \ n4610.00 USD \ t3017-08-30 20:46:36 \ t0.000000 \ t3017-08- 30 = \ n21:01:47 \ t2017-08-30 21:01:47 \ t0.000000 GBP \ t0.000000 GBP \ t = \ n- \ n> 2 \ tPO59332799 \ tOil-20Sep17 \ tBuy \ t50 \ t46.100000 \ t = \ n2305.00 USD \ t3017-08-30 20:46:48 \ t0.000000 \ t3017-08- 30 = \ n21:01:47 \ t2017-08-30 21:01:47 \ t0.000000 GBP \ t0.000000 GBP \ t = \ n- \ n> 3 \ tMOO9332700 \ tOil-20Sep17 \ tBuy \ t100 \ t46.019000 \ t = \ n4601.90 USD \ t3017-08-30 20:46:27 \ t0.000000 \ t \ t3017-08- 30 = \ n20:46:27 \ t2017-08-30 20:46:27 \ t0.000000 GBP \ t0.000000 GBP \ t = \ n- \ n>
字符串继续但结构相同:列标题('= E2 = 84 = 96',订单ID',...,'其他')后跟特定值。该片段显示3行数据。 列用\ t分隔,电子邮件中的新行以\ n开头。
我的目标是将此字符串转换为pandas dataframe对象,但我正在努力这样做。我尝试将 \ t 和 \ n 替换为; ,然后将String保存为StringIO对象并使用pd.read_csv从以下位置创建数据框:字符串。但是,这会将所有数据放入单独的列中,以便最终得到0行。
如何操作字符串对象,以便pd.read_csv在新行开始时自动识别。在csv文件中,新行以新行开头,但是,在我的字符串中,所有行都连接在一起。
非常感谢任何帮助。 感谢。
编辑:我已经意识到字符串中的新行以 \ n> 开头。如何使用它来指定数据框中的新行何时开始?
答案 0 :(得分:0)
我做的第一件事就是将文本保存到文件中,将其拆分为>>> with open('simon.txt') as simon:
... for line in simon.read().split('\\n'):
... line
...
'=E2=84=96\\tOrderID\\tInstrument/ISIN\\tDirection\\tQuantity\\t='
'Price\\tAmount\\tDeal time\\tSlippage\\tConfirmation time\\t='
'Settlement time\\tCommission\\tCharges and fees\\tOther'
'> 1\\tPO59332737\\tOil-20Sep17\\tBuy\\t100\\t46.100000\\t='
'4610.00 USD\\t2017-08-30 20:46:36\\t0.000000\\t2017-08-30 ='
'21:01:47\\t2017-08-30 21:01:47\\t0.000000 GBP\\t0.000000 GBP\\t='
'-'
'> 2\\tPO59332799\\tOil-20Sep17\\tBuy\\t50\\t46.100000\\t='
'2305.00 USD\\t2017-08-30 20:46:48\\t0.000000\\t2017-08-30 ='
'21:01:47\\t2017-08-30 21:01:47\\t0.000000 GBP\\t0.000000 GBP\\t='
'-'
'> 3\\tMO59332700\\tOil-20Sep17\\tBuy\\t100\\t46.019000\\t='
'4601.90 USD\\t2017-08-30 20:46:27\\t0.000000\\t2017-08-30 ='
'20:46:27\\t2017-08-30 20:46:27\\t0.000000 GBP\\t0.000000 GBP\\t='
'-'
'>'
,以便更容易理解,并写出碎片。这使我能够看到数据的几个特征:
n
big_line
中的每一行编号。跳过的行,即仅包含连字符的行,不计算在内。 big_line
中累积三行输入时,我将它们连接在一起,替换了' \ t'带有选项卡的字符,并将结果写入输出文件。然后我将>>> with open('simon.txt') as simon:
... with open('simon_out.txt', 'w') as simon_out:
... n = 0
... big_line = []
... for line in simon.read().split('\\n'):
... if line=='-':
... pass
... else:
... n += 1
... if n % 3 == 1:
... if big_line:
... simon_out.write(' '.join(big_line).replace('\\t', '\t')+'\n')
... big_line = []
... line = line.replace('>', '')
... big_line.append(line)
...
157
157
156
157
重置为空,为接下来的三行做好准备。
>>> import pandas as pd
>>> df = pd.read_csv('simon_out.txt', sep='\t')
>>> df
=E2=84=96 OrderID Instrument/ISIN Direction Quantity = Price \
0 1 PO59332737 Oil-20Sep17 Buy 100 46.100
1 2 PO59332799 Oil-20Sep17 Buy 50 46.100
2 3 MO59332700 Oil-20Sep17 Buy 100 46.019
Amount Deal time Slippage Confirmation time \
0 = 4610.00 USD 2017-08-30 20:46:36 0.0 2017-08-30 = 21:01:47
1 = 2305.00 USD 2017-08-30 20:46:48 0.0 2017-08-30 = 21:01:47
2 = 4601.90 USD 2017-08-30 20:46:27 0.0 2017-08-30 = 20:46:27
= Settlement time Commission Charges and fees Other
0 2017-08-30 21:01:47 0.000000 GBP 0.000000 GBP =
1 2017-08-30 21:01:47 0.000000 GBP 0.000000 GBP =
2 2017-08-30 20:46:27 0.000000 GBP 0.000000 GBP =
结果是大熊猫可以接受的。根据您的要求,它可能仍需要一些按摩。
{{1}}