简体   繁体   English

如何将 QTable 保存到文件(.txt、.csv 等)

[英]How to save a QTable to a file (.txt, .csv, etc.)

I am trying to save a QTable object which originates from a Isophote list of an elliptical isophote fit, to really any filetype (.txt, .csv, etc.) that allows me to simply just load it back into the script without having to clean it or any of that sorts.我正在尝试将 QTable object 保存为任何文件类型(.txt、.csv 等),它源自椭圆等光度拟合的等光度列表,让我只需将其加载回脚本而无需清理它或任何一种。

  • The QTable is created as follows: QTable 创建如下:
example_isolist = ellipse_example.fit_image(sclip=3., nclip=3) #performing elliptical isophote fit and creating the Isophote list
example_isolist_tab = example_isolist.to_table() #converting to QTable
  • My attempt:我的尝试:
import json
with open("example_isolist_tab.txt", 'w') as f:
    json.dump(example_isolist_tab, f, indent=2)

Using json here does not work.在这里使用 json 不起作用。 The following error occurrs:出现以下错误:

TypeError: Object of type QTable is not JSON serializable

Anybody here got experience with photutils data handling or saving an isophote fit to a file?这里的任何人都有过 photutils 数据处理或将等光度适合保存到文件的经验吗? It should be a very simple thing really, just a way so I don't have to rerun my entire script each time I want to work with the results from the isophote fit.这应该是一件非常简单的事情,只是一种方式,这样我就不必每次想要使用等光度拟合的结果时都重新运行整个脚本。 My whole data set contains 26 images which would mean roughly 2.5h computation time without saving in between.我的整个数据集包含 26 张图像,这意味着大约需要 2.5 小时的计算时间,而不会在两者之间进行保存。

Thanks in advance!提前致谢!

Astropy already has numerous built-in formats for saving Tables with Table.write . Astropy 已经有许多用于保存表格的内置格式Table.write

For QTable in particular, if you want a round-trippable text-based format the ECSV format is highly recommended, as it also outputs metadata concerning each column's types and units.特别是对于QTable ,如果您想要一个可往返的基于文本的格式,强烈建议使用ECSV格式,因为它还输出有关每列类型和单位的元数据。 If you name your file with a .ecsv extension, this format is used automatically.如果您使用.ecsv扩展名命名文件,则会自动使用此格式。 For example:例如:

>>> from astropy.table import QTable
>>> import astropy.units as u
>>> import numpy as np

>>> a = np.array([1, 4, 5], dtype=np.int32)
>>> b = [2.0, 5.0, 8.5]
>>> c = ['x', 'y', 'z']
>>> d = [10, 20, 30] * u.m / u.s

>>> t = QTable([a, b, c, d],
...            names=('a', 'b', 'c', 'd'),
...            meta={'name': 'first table'})

>>> t.write('table.ecsv')
>>>  print(open('table.ecsv').read())
# %ECSV 0.9
# ---
# datatype:
# - {name: a, datatype: int32}
# - {name: b, datatype: float64}
# - {name: c, datatype: string}
# - {name: d, unit: m / s, datatype: float64}
# meta:
#   __serialized_columns__:
#     d:
#       __class__: astropy.units.quantity.Quantity
#       unit: !astropy.units.Unit {unit: m / s}
#       value: !astropy.table.SerializedColumn {name: d}
#   name: first table
# schema: astropy-2.0
a b c d
1 2.0 x 10.0
4 5.0 y 20.0
5 8.5 z 30.0

>>> QTable.read('table.ecsv')
<QTable length=3>
  a      b     c      d   
                    m / s 
int32 float64 str1 float64
----- ------- ---- -------
    1     2.0    x    10.0
    4     5.0    y    20.0
    5     8.5    z    30.0

Use the astropy ECSV writer to write a QTable losslessly:使用 astropy ECSV编写器无损地编写QTable

>>> example_isolist_tab.write('example_isolist_tab.ecsv')
>>> t = QTable.read('example_isolist_tab.ecsv')  # returns the same table

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将a.txt格式文件导入Python,具有规则的列结构(但不可靠的分隔符如制表符、逗号等) - How to import a .txt format file into Python, with a regular column structure (but not reliable separators such as tab, comma, etc.) 在QTable中打开CSV文件时程序关闭 - program close when open csv file in qtable 如何使用 pyspark 将 csv 文件转换或保存为 txt 文件 - how to convert or save a csv file into a txt file using pyspark 如何将具有列的txt文件保存到csv文件中? - How to save a txt file that has columns to a csv file? Gensim:如何将LDA模型生成的主题保存为可读格式(csv,txt等)? - Gensim: How to save LDA model's produced topics to a readable format (csv,txt,etc)? 打开.txt 文件并将 output 保存在 csv 文件中 - open .txt file and save output in csv file 如何将 output 保存为图像文件多次,名称中包含递增的数字序列(name01、name02 等)? - How to save the output as image file mutliple times with incremental number sequence in the name (name01,name02,etc.)? 用于计算平均年龄,最高薪水等的Python代码 从.txt文件 - Python code for calculating average age ,max salary,etc. from .txt file 如何在databricks(scala,python等)中创建.tsv文件 - How to create .tsv file in databricks(scala, python and etc.) 如何将 requirements.txt 发送给没有 PyLint 等开发包的用户? - How to ship requirements.txt to users without development-packages such as PyLint etc.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM