删除文本文件python中的列

时间:2018-09-12 00:12:21

标签: python parsing text

我有以下文本文件,并希望从文件中完全删除(打开,高,低)。我想留下:日期,时间,关闭,向上,向下

这是示例文本文件:

"Date","Time","Open","High","Low","Close","Up","Down"
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,109,0
08/09/2018,17:00:00,2852.25,2852.25,2852.25,2852.25,0,1
08/09/2018,17:00:00,2852.25,2852.25,2852.25,2852.25,0,2
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,1,0
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,1,0
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,1,0
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,2,0
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,1,0
08/09/2018,17:00:00,2852.25,2852.25,2852.25,2852.25,0,2
08/09/2018,17:00:00,2852.25,2852.25,2852.25,2852.25,0,5
08/09/2018,17:00:00,2852.25,2852.25,2852.25,2852.25,0,1
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,3,0
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,1,0
08/09/2018,17:00:00,2852.50,2852.50,2852.50,2852.50,3,0
08/09/2018,17:00:00,2852.75,2852.75,2852.75,2852.75,3,0
08/09/2018,17:00:00,2852.25,2852.25,2852.25,2852.25,0,1
08/09/2018,17:00:00,2852.75,2852.75,2852.75,2852.75,7,0
08/09/2018,17:00:00,2852.75,2852.75,2852.75,2852.75,4,0
08/09/2018,17:00:00,2852.75,2852.75,2852.75,2852.75,1,0
08/09/2018,17:00:00,2852.75,2852.75,2852.75,2852.75,12,0
08/09/2018,17:00:00,2852.75,2852.75,2852.75,2852.75,2,0
08/09/2018,17:00:00,2852.75,2852.75,2852.75,2852.75,2,0
08/09/2018,17:00:00,2852.75,2852.75,2852.75,2852.75,1,0

2 个答案:

答案 0 :(得分:0)

尽管这也许不是最好的方法,但我建议使用pandas库Pandas将这样的文本文件读入数据框。 (如果要对此数据执行实际的数据分析操作,则像Pandas之类的东西最适合您,而不是简单的文件io读取/拆分。)创建数据框后(请参阅Pandas的文档),请使用pandas放置命令Pandas drop

对于您的情况,应该是这样的:

#import pandas as pd
#Following creation of Pandas dataframe df
df = df.drop(["Open", "High", "Low"], axis = 1)

请确保在进行更改后重新分配数据框。我将把它作为练习供您参考文档并实际导入数据框。翻看一下熊猫,你应该弄清楚。

答案 1 :(得分:0)

在读取诸如此类的文件时,通常使用像Pandas这样的库。

import pandas as pd
df = pd.read_csv("sample_text_file.txt")
df_new = df[['Date', 'Time', 'Close', 'Up', 'Down']]
df_new.to_csv('sample_text_file_new.txt', header=True, index=None)