将数据另存为* .dat文件? [英] Save data as a *.dat file?

查看:59
本文介绍了将数据另存为* .dat文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写程序,该程序应导入 *.dat 文件,从某些列中减去特定值,然后以 *.dat 格式保存文件一个不同的目录.

I am writing a program in Python which should import *.dat files, subtract a specific value from certain columns and subsequently save the file in *.dat format in a different directory.

我当前的策略是将数据文件加载到 numpy 数组中,执行计算然后保存.我坚持节省部分.我不知道如何以 *.dat 格式在python中保存文件.谁能帮我?还是有另一种方法不需要导入 *.dat 文件作为 numpy 数组?非常感谢!

My current tactic is to load the datafiles in a numpy array, perform the calculation and then save it. I am stuck with the saving part. I do not know how to save a file in python in the *.dat format. Can anyone help me? Or is there an alternative way without needing to import the *.dat file as a numpy array? Many thanks!

推荐答案

假设文件看起来像

file = open(filename, "r")

您需要做的就是打开另一个以"w"作为第二个参数的文件

all you need to do is open another file with "w" as the second parameter

file = open(new_file-path,"w")
file.write(data)
file.close()

如果您的数据不是字符串,则将其设置为字符串或使用

if your data is not a string, either make it a string, or use

file = open(filename, "rb")
file = open(filename, "wb")

读取和写入时,因为这些读取和写入原始字节

when reading and writing, since these read and write raw bytes

这篇关于将数据另存为* .dat文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆