IO
)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)
# 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
# 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
# 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
# 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
# 迭代器读取
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
# 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()
fo = open('data/test.txt', 'wt')
fo.write('something')
fo.close()
!cat data/test.txt
something
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
"The Value is %012.8f" % 23.253427
'The Value is 023.25342700'
"RA is %12.8f, Dec is %12.8f" % (332.202274, -2.056767)
'RA is 332.20227400, Dec is -2.05676700'
coord = {'RA': 332.202274, 'Dec': -2.056767}
"RA is %(RA)12.8f, Dec is %(Dec)12.8f" % coord
'RA is 332.20227400, Dec is -2.05676700'
[[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
浮点,默认精度6F
nan -> NAN, inf-> INFg
G
"RA is {0:12.8f}, Dec is {1:12.8f}".format(332.202274, -2.056767)
'RA is 332.20227400, Dec is -2.05676700'
"RA is {RA:12.8f}, Dec is {Dec:12.8f}".format(RA=332.202274, Dec=-2.056767)
'RA is 332.20227400, Dec is -2.05676700'
coord = {'RA': 332.202274, 'Dec': -2.056767}
"RA is {0[RA]:12.8f}, Dec is {0[Dec]:12.8f}".format(coord)
'RA is 332.20227400, Dec is -2.05676700'
"RA is {}, Dec is {}".format(332.202274, -2.056767)
'RA is 332.202274, Dec is -2.056767'
# 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 ✫ ------- ----- ------ ----------------------------------------