テキスト ファイルがあり、2 行目の 1 番目、2 番目、3 番目、4 番目、5 番目、6 番目、7 番目、8 番目の文字を読みたい場合、何を書きますか? 私はPythonを初めて使用し、v3.3を使用しています
サンプル テキスト ファイル
Hello, my name
is Bob. How are
you?
文字 H、E、L、L、(,)、( )、M、および Y だけをどのように読み取ることができますか?
それは難しいことではありません:
with open('filename.txt', 'r') as handle:
first_line = handle.readline()
print(first_line[0], first_line[1], ...)
まず、Python チュートリアルを一通り読むことから始めてください。
一般的に言えば:
これの python 仕様は本当に簡単です。何を思い付くことができるかを確認することをお勧めします。そうすれば、よりよく学ぶことができます。
# `f` is your file
skip = 1 # in this case we want to skip one line to read characters from the second one
for i in range(skip): # this loop will skip a number of lines
f.realine()
line = f.readline() # read the line we were looking for
chars = list(line[:8]) # getting first eight characters as a list