LoginSignup
2
0

More than 5 years have passed since last update.

Python > fileIO > ファイルの一行目(空白含む)を取得する > open() as fd | title = fd.readline().rstrip('\n')

Last updated at Posted at 2018-01-14
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6

処理概要

mueller(一部)
theta s11 s12 s13 s14 s21 s22 s23 s24 s31 s32 s33 s34 s41 s42 s43 s44
0.00 1.4154797779E+02 0.0000000000E+00 0.0000000000E+00 2.0774468080E-11 0.0000000000E+00 1.4154797779E+02 1.5202490767E-10 0.0000000000E+00 0.0000000000E+00 -1.5202490767E-10 1.4154797779E+02 0.0000000000E+00 2.0774468080E-11 0.0000000000E+00 0.0000000000E+00 1.4154797779E+02
1.00 1.4140075324E+02 -5.8903789321E-03 1.0170761154E-11 2.4386238017E-11 -5.8903789321E-03 1.4140075324E+02 1.4176203701E-10 -3.5948816258E-12 1.0178382783E-11 -1.4176220755E-10 1.4140075276E+02 9.9513428708E-03 2.4385372022E-11 3.6038425530E-12 -9.9513428708E-03 1.4140075276E+02
2.00 1.4095992060E+02 -2.3466973506E-02 2.0327789144E-11 2.8007664918E-11 -2.3466973506E-02 1.4095992060E+02 1.3134304986E-10 -7.1547834934E-12 2.0357543715E-11 -1.3134441652E-10 1.4095991305E+02 3.9706966470E-02 2.8000740778E-11 7.1871195463E-12 -3.9706966470E-02 1.4095991305E+02
3.00 1.4022799573E+02 -5.2447620268E-02 3.0438949100E-11 3.1616176968E-11 -5.2447620268E-02 1.4022799573E+02 1.2079755856E-10 -1.0660883463E-11 3.0504182908E-11 -1.2080217069E-10 1.4022795770E+02 8.8972728682E-02 3.1592843704E-11 1.0725708703E-11 -8.8972728682E-02 1.4022795770E+02
4.00 1.3920914771E+02 -9.2366899825E-02 4.0475376063E-11 3.5192981027E-11 -9.2366899825E-02 1.3920914771E+02 1.1015915993E-10 -1.4090607479E-11 4.0588197313E-11 -1.1017007824E-10 1.3920902824E+02 1.5726195578E-01 3.5137810177E-11 1.4191725769E-11 -1.5726195578E-01 1.3920

上記の1行目の「theta s11 s12 s13 s14 s21 s22 s23 s24 s31 s32 s33 s34 s41 s42 s43 s44」を取得したい。

code v0.1

参考: How do I read a text file into a string variable in Python

リンクの処理から1行目を取る方法に変更した。

test_readTitle_180114.py
IN_FILE = 'mueller'
with open(IN_FILE, 'r') as fd:
    title = fd.read().split('\n')[0]

print(title)

run
$ python3 test_readTitle_180114.py 
theta s11 s12 s13 s14 s21 s22 s23 s24 s31 s32 s33 s34 s41 s42 s43 s44

code v0.2

@shiracamus さんのコメントにてパフォーマンスの良いreadline()使用例を教えていただきました。

情報感謝です。

test_readTitle_180114.py
IN_FILE = 'mueller'

with open(IN_FILE, 'r') as fd:
    title = fd.readline().rstrip('\n')
print(title)
print('---')

run
$ python3 test_readTitle_180114.py 
theta s11 s12 s13 s14 s21 s22 s23 s24 s31 s32 s33 s34 s41 s42 s43 s44
---

(Note: ---はデバッグ用の文字列出力です)

2
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0