【发布时间】:2019-03-08 00:24:06
【问题描述】:
我知道以前有人问过类似的问题。但我仍然无法找出为我的程序处理数据的最佳方法
我有一个大文本文件(50,000 到 5,000,000 行文本)。我需要处理这个文件的每一行并将其写入数据框,以便我可以对它们进行一些数据分析。
数据框有 9 列,大部分是浮点数和一些字符串,没有。行数〜没有。输入文件中的行数
目前,我正在使用“with open..”逐行读取此文件,然后使用正则表达式提取所需数据并将其作为一行写入数据框中。由于这是通过 For 循环,因此需要永远完成。
最好的方法是什么?任何指针或示例程序?我什至应该使用数据框吗?
这是我的代码。
def gcodetodf(self):
with open(self.inputfilepath, 'r') as ifile:
lflag = False
for item in ifile:
layermatch = self.layerpattern.match(item)
self.tlist = item.split(' ')
self.clist = re.split(r"(\w+)", item)
if layermatch and (str(self.tlist[2][:-1]) == 'end' or int(self.tlist[2][:-1]) == (self.endlayer + 1)):
break
if (layermatch and int(self.tlist[2][:-1]) == self.startlayer) or lflag is True:
lflag = True
# clist = re.split(r"(\w+)", item)
map_gcpat = {bool(self.gonepattern.match(item)): self.gc_g1xyef,
bool(self.gepattern.match(item)): self.gc_g1xye,
bool(self.gtrpattern.match(item)): self.gc_g1xyf,
bool(self.resetextpattern.match(item)): self.gc_g92e0,
bool(self.ftpattern.match(item)): self.gc_ftype,
bool(self.toolcompattern.match(item)): self.gc_toolcmt,
bool(self.layerpattern.match(item)): self.gc_laycmt,
bool(self.zpattern.match(item)): self.gc_g1z}
map_gcpat.get(True, self.contd)()
# print(self.newdataframe)
写入数据帧的示例函数如下所示:
def gc_g1xye(self):
self.newdataframe = self.newdataframe.append(
{'Xc': float(self.tlist[1][1:]), 'Yc': float(self.tlist[2][1:]), 'Zc': self.gc_z,
'E': float(self.tlist[3][1:]),
'F': None, 'FT': self.ft_var, 'EW': self.tc_ew, 'LH': self.tc_lh, 'Layer': self.cmt_layer},
ignore_index=True)
示例输入文件:
........
G1 X159.8 Y140.2 E16.84505
G1 X159.8 Y159.8 E17.56214
M204 S5000
M205 X30 Y30
G0 F2400 X159.6 Y159.8
G0 X159.33 Y159.33
G0 X159.01 Y159.01
M204 S500
M205 X20 Y20
;TYPE:SKIN
G1 F1200 X140.99 Y159.01 E18.22142
G1 X140.99 Y140.99 E18.8807
G1 X159.01 Y140.99 E19.53999
G1 X159.01 Y159.01 E20.19927
M204 S5000
M205 X30 Y30
G0 F2400 X150.21 Y150.21
M204 S500
M205 X20 Y20
G1 F1200 X149.79 Y150.21 E20.21464
G1 X149.79 Y149.79 E20.23
G1 X150.21 Y149.79 E20.24537
G1 X150.21 Y150.21 E20.26073
M204 S5000
M205 X30 Y30
G0 F2400 X150.61 Y150.61
M204 S500
M205 X20 Y20
G1 F1200 X149.39 Y150.61 E20.30537
G1 X149.39 Y149.39 E20.35
G1 X150.61 Y149.39 E20.39464
..........
【问题讨论】:
-
你能举个例子,数据是什么样的,你需要什么处理?先将其读入数据帧然后再进行处理可能更容易。
-
@Lidae 我已经用文件的示例块更新了帖子。我需要对其进行数学运算。例如。从 col1 和 col2 中取值来计算圆的面积;将所有区域加在一起并绘制图形。
-
好吧,我有点希望你能用 read_csv 之类的东西来做解析。看到这让我觉得解析必须是手动的。速度方面的瓶颈可能是 self.newdataframe = self.newdataframe.append 这一行。尝试先附加到一个列表,然后使用该列表一次创建所有数据框,否则它会做很多不必要的复制。
标签: python-3.x pandas dataframe data-analysis large-data