python读取dat数据_从python中的.dat文件读取和做计算

这篇博客介绍如何在Python中读取.dat文件,特别是处理包含多列数据的大型文件。作者面临的问题是如何按需选取列进行计算,例如将第2、3、4列除以第1列。解决方案包括先将.dat文件转换为CSV,然后使用pandas库读取并计算所需列。代码示例中展示了如何实现这个过程。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

I need to read a .dat file in python which has 12 columns in total and millions of lines of rows. I need to divide column 2,3 and 4 with column 1 for my calculation. So before I load that .dat file, do I need to delete all the other unwanted columns? If not, how do I selectively declare the column and ask python to do the math?

an example of the .dat file would be

data.dat

I am new to python , so a little instruction to open , read and calculation would be appreciated.

I have added the code I am using as a starter from your suggestion:

from sys import argv

import pandas as pd

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename

print txt.read()

def your_func(row):

return row['x-momentum'] / row['mass']

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值