Python for Astronomy

Python 基础知识 :: 编程范式

何勃亮
中国科学院国家天文台 中国虚拟天文台 (China-VO)

编程范式

  • 文件操作 (IO
  • 字符串

文件操作

读文件

  • read()
  • readline()
  • readlines()

open

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

'r'    open for reading (default)
'w'    open for writing, truncating the file first
'x'    open for exclusive creation, failing if the file already exists
'a'    open for writing, appending to the end of the file if it exists
'b'    binary mode
't'    text mode (default)
'+'    open a disk file for updating (reading and writing)
'U'    universal newlines mode (deprecated)
In [10]:
# read()

f = open('data/sample.txt', 'rt')
dat = f.read()
f.close()
print(dat)
obsid|designation|obsdate|lmjd|planid|spid|fiberid|ra|dec|snru|snrg|snrr|snri|snrz|objtype|class|subclass|magtype|mag1|mag2|mag3|mag4|mag5|mag6|mag7|tsource|fibertype|tfrom|t_info|rv|z|z_err
101001|J220848.54-020324.3|2011-10-24|55859|F5902|1|1|332.2022740000|-2.0567670000|2.23|10.69|17.99|23.07|13.93|Star|STAR|K1|ugriz|18.78|17.12|16.42|16.15|15.97|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-23.06902964||0.00000297
101002|J220953.17-020506.0|2011-10-24|55859|F5902|1|2|332.4715760000|-2.0850150000|2.00|5.52|14.19|20.30|14.05|Star|STAR|M0|ugriz|20.91|18.10|16.66|16.05|15.67|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||27.10000040||0.00017775
101008|J220928.49-015720.7|2011-10-24|55859|F5902|1|8|332.3687450000|-1.9557710000|1.84|9.94|25.25|32.32|18.29|Star|STAR|G5|ugriz|18.25|16.64|15.97|15.77|15.64|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||25.03866609||0.00000287
101009|J220849.59-015207.1|2011-10-24|55859|F5902|1|9|332.2066650000|-1.8686530000|1.86|9.13|18.80|25.28|14.18|Star|STAR|G0|ugriz|18.64|17.19|16.63|16.37|16.25|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-22.16965227||0.00000537
101016|J220923.69-020809.9|2011-10-24|55859|F5902|1|16|332.3487250000|-2.1360960000|2.17|28.22|52.30|72.89|46.52|Star|STAR|K5|ugriz|18.64|16.21|15.23|14.85|14.62|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-6.63140917||0.00000130
101017|J220946.66-015526.5|2011-10-24|55859|F5902|1|17|332.4444170000|-1.9240460000|2.60|16.56|29.63|38.19|22.15|Star|STAR|G0|ugriz|17.97|16.53|16.00|15.78|15.65|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-2.46129608||0.00000233
101020|J220853.37-015915.4|2011-10-24|55859|F5902|1|20|332.2223790000|-1.9876260000|2.65|17.26|26.29|36.30|20.29|Star|STAR|F5|ugriz|17.01|15.98|15.51|15.35|15.27|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||10.84948906||0.00000751
101021|J220924.33-014833.5|2011-10-24|55859|F5902|1|21|332.3513810000|-1.8093330000|6.05|34.57|53.87|62.42|37.85|Star|STAR|F5|ugriz|16.75|15.61|15.16|14.98|14.92|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-17.91859521||0.00000354
101023|J221001.52-020100.8|2011-10-24|55859|F5902|1|23|332.5063740000|-2.0169000000|2.35|12.14|22.38|27.72|16.25|Star|STAR|F9|ugriz|18.46|16.97|16.39|16.18|16.12|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||52.65854525||0.00000227

In [11]:
# read(chunksize)

f = open('data/sample.txt')
dat = f.read(100)
f.close()
print(dat)
obsid|designation|obsdate|lmjd|planid|spid|fiberid|ra|dec|snru|snrg|snrr|snri|snrz|objtype|class|sub
In [12]:
# readline

f = open('data/sample.txt', 'rt')
while True:
    line = f.readline()
    if not line:
        break
    print(line)
f.close()
obsid|designation|obsdate|lmjd|planid|spid|fiberid|ra|dec|snru|snrg|snrr|snri|snrz|objtype|class|subclass|magtype|mag1|mag2|mag3|mag4|mag5|mag6|mag7|tsource|fibertype|tfrom|t_info|rv|z|z_err

101001|J220848.54-020324.3|2011-10-24|55859|F5902|1|1|332.2022740000|-2.0567670000|2.23|10.69|17.99|23.07|13.93|Star|STAR|K1|ugriz|18.78|17.12|16.42|16.15|15.97|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-23.06902964||0.00000297

101002|J220953.17-020506.0|2011-10-24|55859|F5902|1|2|332.4715760000|-2.0850150000|2.00|5.52|14.19|20.30|14.05|Star|STAR|M0|ugriz|20.91|18.10|16.66|16.05|15.67|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||27.10000040||0.00017775

101008|J220928.49-015720.7|2011-10-24|55859|F5902|1|8|332.3687450000|-1.9557710000|1.84|9.94|25.25|32.32|18.29|Star|STAR|G5|ugriz|18.25|16.64|15.97|15.77|15.64|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||25.03866609||0.00000287

101009|J220849.59-015207.1|2011-10-24|55859|F5902|1|9|332.2066650000|-1.8686530000|1.86|9.13|18.80|25.28|14.18|Star|STAR|G0|ugriz|18.64|17.19|16.63|16.37|16.25|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-22.16965227||0.00000537

101016|J220923.69-020809.9|2011-10-24|55859|F5902|1|16|332.3487250000|-2.1360960000|2.17|28.22|52.30|72.89|46.52|Star|STAR|K5|ugriz|18.64|16.21|15.23|14.85|14.62|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-6.63140917||0.00000130

101017|J220946.66-015526.5|2011-10-24|55859|F5902|1|17|332.4444170000|-1.9240460000|2.60|16.56|29.63|38.19|22.15|Star|STAR|G0|ugriz|17.97|16.53|16.00|15.78|15.65|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-2.46129608||0.00000233

101020|J220853.37-015915.4|2011-10-24|55859|F5902|1|20|332.2223790000|-1.9876260000|2.65|17.26|26.29|36.30|20.29|Star|STAR|F5|ugriz|17.01|15.98|15.51|15.35|15.27|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||10.84948906||0.00000751

101021|J220924.33-014833.5|2011-10-24|55859|F5902|1|21|332.3513810000|-1.8093330000|6.05|34.57|53.87|62.42|37.85|Star|STAR|F5|ugriz|16.75|15.61|15.16|14.98|14.92|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-17.91859521||0.00000354

101023|J221001.52-020100.8|2011-10-24|55859|F5902|1|23|332.5063740000|-2.0169000000|2.35|12.14|22.38|27.72|16.25|Star|STAR|F9|ugriz|18.46|16.97|16.39|16.18|16.12|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||52.65854525||0.00000227

In [13]:
# readline
# 去掉行尾 '\n'

f = open('data/sample.txt', 'rt')
while True:
    line = f.readline()
    if not line:
        break
    print(line.rstrip())
f.close()
obsid|designation|obsdate|lmjd|planid|spid|fiberid|ra|dec|snru|snrg|snrr|snri|snrz|objtype|class|subclass|magtype|mag1|mag2|mag3|mag4|mag5|mag6|mag7|tsource|fibertype|tfrom|t_info|rv|z|z_err
101001|J220848.54-020324.3|2011-10-24|55859|F5902|1|1|332.2022740000|-2.0567670000|2.23|10.69|17.99|23.07|13.93|Star|STAR|K1|ugriz|18.78|17.12|16.42|16.15|15.97|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-23.06902964||0.00000297
101002|J220953.17-020506.0|2011-10-24|55859|F5902|1|2|332.4715760000|-2.0850150000|2.00|5.52|14.19|20.30|14.05|Star|STAR|M0|ugriz|20.91|18.10|16.66|16.05|15.67|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||27.10000040||0.00017775
101008|J220928.49-015720.7|2011-10-24|55859|F5902|1|8|332.3687450000|-1.9557710000|1.84|9.94|25.25|32.32|18.29|Star|STAR|G5|ugriz|18.25|16.64|15.97|15.77|15.64|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||25.03866609||0.00000287
101009|J220849.59-015207.1|2011-10-24|55859|F5902|1|9|332.2066650000|-1.8686530000|1.86|9.13|18.80|25.28|14.18|Star|STAR|G0|ugriz|18.64|17.19|16.63|16.37|16.25|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-22.16965227||0.00000537
101016|J220923.69-020809.9|2011-10-24|55859|F5902|1|16|332.3487250000|-2.1360960000|2.17|28.22|52.30|72.89|46.52|Star|STAR|K5|ugriz|18.64|16.21|15.23|14.85|14.62|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-6.63140917||0.00000130
101017|J220946.66-015526.5|2011-10-24|55859|F5902|1|17|332.4444170000|-1.9240460000|2.60|16.56|29.63|38.19|22.15|Star|STAR|G0|ugriz|17.97|16.53|16.00|15.78|15.65|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-2.46129608||0.00000233
101020|J220853.37-015915.4|2011-10-24|55859|F5902|1|20|332.2223790000|-1.9876260000|2.65|17.26|26.29|36.30|20.29|Star|STAR|F5|ugriz|17.01|15.98|15.51|15.35|15.27|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||10.84948906||0.00000751
101021|J220924.33-014833.5|2011-10-24|55859|F5902|1|21|332.3513810000|-1.8093330000|6.05|34.57|53.87|62.42|37.85|Star|STAR|F5|ugriz|16.75|15.61|15.16|14.98|14.92|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-17.91859521||0.00000354
101023|J221001.52-020100.8|2011-10-24|55859|F5902|1|23|332.5063740000|-2.0169000000|2.35|12.14|22.38|27.72|16.25|Star|STAR|F9|ugriz|18.46|16.97|16.39|16.18|16.12|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||52.65854525||0.00000227
In [14]:
# 迭代器读取

f = open('data/sample.txt', 'rt')
for line in f:
    print(line.rstrip())
f.close()
obsid|designation|obsdate|lmjd|planid|spid|fiberid|ra|dec|snru|snrg|snrr|snri|snrz|objtype|class|subclass|magtype|mag1|mag2|mag3|mag4|mag5|mag6|mag7|tsource|fibertype|tfrom|t_info|rv|z|z_err
101001|J220848.54-020324.3|2011-10-24|55859|F5902|1|1|332.2022740000|-2.0567670000|2.23|10.69|17.99|23.07|13.93|Star|STAR|K1|ugriz|18.78|17.12|16.42|16.15|15.97|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-23.06902964||0.00000297
101002|J220953.17-020506.0|2011-10-24|55859|F5902|1|2|332.4715760000|-2.0850150000|2.00|5.52|14.19|20.30|14.05|Star|STAR|M0|ugriz|20.91|18.10|16.66|16.05|15.67|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||27.10000040||0.00017775
101008|J220928.49-015720.7|2011-10-24|55859|F5902|1|8|332.3687450000|-1.9557710000|1.84|9.94|25.25|32.32|18.29|Star|STAR|G5|ugriz|18.25|16.64|15.97|15.77|15.64|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||25.03866609||0.00000287
101009|J220849.59-015207.1|2011-10-24|55859|F5902|1|9|332.2066650000|-1.8686530000|1.86|9.13|18.80|25.28|14.18|Star|STAR|G0|ugriz|18.64|17.19|16.63|16.37|16.25|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-22.16965227||0.00000537
101016|J220923.69-020809.9|2011-10-24|55859|F5902|1|16|332.3487250000|-2.1360960000|2.17|28.22|52.30|72.89|46.52|Star|STAR|K5|ugriz|18.64|16.21|15.23|14.85|14.62|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-6.63140917||0.00000130
101017|J220946.66-015526.5|2011-10-24|55859|F5902|1|17|332.4444170000|-1.9240460000|2.60|16.56|29.63|38.19|22.15|Star|STAR|G0|ugriz|17.97|16.53|16.00|15.78|15.65|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-2.46129608||0.00000233
101020|J220853.37-015915.4|2011-10-24|55859|F5902|1|20|332.2223790000|-1.9876260000|2.65|17.26|26.29|36.30|20.29|Star|STAR|F5|ugriz|17.01|15.98|15.51|15.35|15.27|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||10.84948906||0.00000751
101021|J220924.33-014833.5|2011-10-24|55859|F5902|1|21|332.3513810000|-1.8093330000|6.05|34.57|53.87|62.42|37.85|Star|STAR|F5|ugriz|16.75|15.61|15.16|14.98|14.92|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-17.91859521||0.00000354
101023|J221001.52-020100.8|2011-10-24|55859|F5902|1|23|332.5063740000|-2.0169000000|2.35|12.14|22.38|27.72|16.25|Star|STAR|F9|ugriz|18.46|16.97|16.39|16.18|16.12|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||52.65854525||0.00000227
In [15]:
# readlines 读取所有行

f = open('data/sample.txt', 'rt')
lines = f.readlines()

for line in lines:
    print(line.rstrip())
f.close()
obsid|designation|obsdate|lmjd|planid|spid|fiberid|ra|dec|snru|snrg|snrr|snri|snrz|objtype|class|subclass|magtype|mag1|mag2|mag3|mag4|mag5|mag6|mag7|tsource|fibertype|tfrom|t_info|rv|z|z_err
101001|J220848.54-020324.3|2011-10-24|55859|F5902|1|1|332.2022740000|-2.0567670000|2.23|10.69|17.99|23.07|13.93|Star|STAR|K1|ugriz|18.78|17.12|16.42|16.15|15.97|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-23.06902964||0.00000297
101002|J220953.17-020506.0|2011-10-24|55859|F5902|1|2|332.4715760000|-2.0850150000|2.00|5.52|14.19|20.30|14.05|Star|STAR|M0|ugriz|20.91|18.10|16.66|16.05|15.67|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||27.10000040||0.00017775
101008|J220928.49-015720.7|2011-10-24|55859|F5902|1|8|332.3687450000|-1.9557710000|1.84|9.94|25.25|32.32|18.29|Star|STAR|G5|ugriz|18.25|16.64|15.97|15.77|15.64|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||25.03866609||0.00000287
101009|J220849.59-015207.1|2011-10-24|55859|F5902|1|9|332.2066650000|-1.8686530000|1.86|9.13|18.80|25.28|14.18|Star|STAR|G0|ugriz|18.64|17.19|16.63|16.37|16.25|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-22.16965227||0.00000537
101016|J220923.69-020809.9|2011-10-24|55859|F5902|1|16|332.3487250000|-2.1360960000|2.17|28.22|52.30|72.89|46.52|Star|STAR|K5|ugriz|18.64|16.21|15.23|14.85|14.62|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-6.63140917||0.00000130
101017|J220946.66-015526.5|2011-10-24|55859|F5902|1|17|332.4444170000|-1.9240460000|2.60|16.56|29.63|38.19|22.15|Star|STAR|G0|ugriz|17.97|16.53|16.00|15.78|15.65|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-2.46129608||0.00000233
101020|J220853.37-015915.4|2011-10-24|55859|F5902|1|20|332.2223790000|-1.9876260000|2.65|17.26|26.29|36.30|20.29|Star|STAR|F5|ugriz|17.01|15.98|15.51|15.35|15.27|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||10.84948906||0.00000751
101021|J220924.33-014833.5|2011-10-24|55859|F5902|1|21|332.3513810000|-1.8093330000|6.05|34.57|53.87|62.42|37.85|Star|STAR|F5|ugriz|16.75|15.61|15.16|14.98|14.92|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-17.91859521||0.00000354
101023|J221001.52-020100.8|2011-10-24|55859|F5902|1|23|332.5063740000|-2.0169000000|2.35|12.14|22.38|27.72|16.25|Star|STAR|F9|ugriz|18.46|16.97|16.39|16.18|16.12|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||52.65854525||0.00000227

写文件

write()

In [16]:
fo = open('data/test.txt', 'wt')
fo.write('something')
fo.close()
In [17]:
!cat data/test.txt
something

with 语句

with语句可以自动进行文件的关闭,简化编程。

with ... as ...:
    statement
In [18]:
with open('data/sample.txt', 'rt') as fi:
    for line in fi:
        print(line.rstrip())
obsid|designation|obsdate|lmjd|planid|spid|fiberid|ra|dec|snru|snrg|snrr|snri|snrz|objtype|class|subclass|magtype|mag1|mag2|mag3|mag4|mag5|mag6|mag7|tsource|fibertype|tfrom|t_info|rv|z|z_err
101001|J220848.54-020324.3|2011-10-24|55859|F5902|1|1|332.2022740000|-2.0567670000|2.23|10.69|17.99|23.07|13.93|Star|STAR|K1|ugriz|18.78|17.12|16.42|16.15|15.97|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-23.06902964||0.00000297
101002|J220953.17-020506.0|2011-10-24|55859|F5902|1|2|332.4715760000|-2.0850150000|2.00|5.52|14.19|20.30|14.05|Star|STAR|M0|ugriz|20.91|18.10|16.66|16.05|15.67|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||27.10000040||0.00017775
101008|J220928.49-015720.7|2011-10-24|55859|F5902|1|8|332.3687450000|-1.9557710000|1.84|9.94|25.25|32.32|18.29|Star|STAR|G5|ugriz|18.25|16.64|15.97|15.77|15.64|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||25.03866609||0.00000287
101009|J220849.59-015207.1|2011-10-24|55859|F5902|1|9|332.2066650000|-1.8686530000|1.86|9.13|18.80|25.28|14.18|Star|STAR|G0|ugriz|18.64|17.19|16.63|16.37|16.25|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-22.16965227||0.00000537
101016|J220923.69-020809.9|2011-10-24|55859|F5902|1|16|332.3487250000|-2.1360960000|2.17|28.22|52.30|72.89|46.52|Star|STAR|K5|ugriz|18.64|16.21|15.23|14.85|14.62|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-6.63140917||0.00000130
101017|J220946.66-015526.5|2011-10-24|55859|F5902|1|17|332.4444170000|-1.9240460000|2.60|16.56|29.63|38.19|22.15|Star|STAR|G0|ugriz|17.97|16.53|16.00|15.78|15.65|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-2.46129608||0.00000233
101020|J220853.37-015915.4|2011-10-24|55859|F5902|1|20|332.2223790000|-1.9876260000|2.65|17.26|26.29|36.30|20.29|Star|STAR|F5|ugriz|17.01|15.98|15.51|15.35|15.27|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||10.84948906||0.00000751
101021|J220924.33-014833.5|2011-10-24|55859|F5902|1|21|332.3513810000|-1.8093330000|6.05|34.57|53.87|62.42|37.85|Star|STAR|F5|ugriz|16.75|15.61|15.16|14.98|14.92|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||-17.91859521||0.00000354
101023|J221001.52-020100.8|2011-10-24|55859|F5902|1|23|332.5063740000|-2.0169000000|2.35|12.14|22.38|27.72|16.25|Star|STAR|F9|ugriz|18.46|16.97|16.39|16.18|16.12|99.00|99.00|JF_LEGAS_S|Obj|SDSS_S||52.65854525||0.00000227

结构化文件的读写

  • csv
  • fits

后面详细介绍

字符串

format

  • %
  • str.format()

尽量使用.format而不是%

传统的 %

In [19]:
"The Value is %012.8f" % 23.253427
Out[19]:
'The Value is 023.25342700'
In [20]:
"RA is %12.8f, Dec is %12.8f" % (332.202274, -2.056767)
Out[20]:
'RA is 332.20227400, Dec is  -2.05676700'
In [22]:
coord = {'RA': 332.202274, 'Dec': -2.056767}
"RA is %(RA)12.8f, Dec is %(Dec)12.8f" % coord
Out[22]:
'RA is 332.20227400, Dec is  -2.05676700'

.format

[[fill]align][sign][#][0][width][,][.precision][type]
  • fill 填充符, 除{}以外的其他字符
  • align 对齐方式
  • sign 符号
  • width 宽度
  • precision 精度
  • type 转换类型

align 对齐方式

  • <
  • > 右,默认
  • = 仅对数值类型有效
  • ^ 居中

sign 符号

  • + 正数前加+,负数前加-
  • - 正数前不加+,负数前加-,默认
  • 空格 正数前加空格,负数前加-

type 转换类型

  • b 二进制
  • c 字符
  • d 十进制,默认
  • o 9进制
  • x 16进制小写
  • X 16进制大写

  • e 默认精度是6

  • E 大写
  • f 浮点,默认精度6
  • F nan -> NAN, inf-> INF
  • g
  • G
In [23]:
"RA is {0:12.8f}, Dec is {1:12.8f}".format(332.202274, -2.056767)
Out[23]:
'RA is 332.20227400, Dec is  -2.05676700'
In [27]:
"RA is {RA:12.8f}, Dec is {Dec:12.8f}".format(RA=332.202274, Dec=-2.056767)
Out[27]:
'RA is 332.20227400, Dec is  -2.05676700'
In [28]:
coord = {'RA': 332.202274, 'Dec': -2.056767}
"RA is {0[RA]:12.8f}, Dec is {0[Dec]:12.8f}".format(coord)
Out[28]:
'RA is 332.20227400, Dec is  -2.05676700'
In [30]:
"RA is {}, Dec is {}".format(332.202274, -2.056767)
Out[30]:
'RA is 332.202274, Dec is -2.056767'
In [41]:
# Sample

print("decimal   hex   char   {0:^40}".format("name"))
print("-------  ----- ------ {0}".format("-"*40))

for v in range(10018, 10028):
    print("{0:7}  {0:^5X}  {0:^3c}".format(v))

print("-------  ----- ------ {0}".format("-"*40))
decimal   hex   char                     name                  
-------  ----- ------ ----------------------------------------
  10018  2722    ✢ 
  10019  2723    ✣ 
  10020  2724    ✤ 
  10021  2725    ✥ 
  10022  2726    ✦ 
  10023  2727    ✧ 
  10024  2728    ✨ 
  10025  2729    ✩ 
  10026  272A    ✪ 
  10027  272B    ✫ 
-------  ----- ------ ----------------------------------------
In [ ]: