の配列の要素に行毎にファイルを読み込む:だからRubyで、私は次の操作を行うことができますPythonの
testsite_array = Array.new
y=0
File.open('topsites.txt').each do |line|
testsite_array[y] = line
y=y+1
end
どのように1はPythonでそれを行うのでしょうか?
の配列の要素に行毎にファイルを読み込む:だからRubyで、私は次の操作を行うことができますPythonの
testsite_array = Array.new
y=0
File.open('topsites.txt').each do |line|
testsite_array[y] = line
y=y+1
end
どのように1はPythonでそれを行うのでしょうか?
testsite_array = []
with open('topsites.txt') as my_file:
for line in my_file:
testsite_array.append(line)
を。 f.readlines()
を使用して代わりに
、より簡単な方法であって、Pythonで
with open('topsites.txt') as my_file:
testsite_array = my_file.readlines()
だけでファイルを開き、readlines()
機能を使用:Pythonはあなたが直接ファイルを反復処理することができますので、これは可能である
with open('topsites.txt') as file:
array = file.readlines()
あなたはファイルオブジェクトのreadlines
メソッドを使用することができます。
with open('topsites.txt') as f:
testsite_array=f.readlines()
または単にlist
を使用し、これはreadlines
を使用するのと同じですが、唯一の違いは、私たちがreadlines
に、オプションのサイズ引数を渡すことができるということです。file.readlines
上
with open('topsites.txt') as f:
testsite_array=list(f)
ヘルプ:
In [46]: file.readlines?
Type: method_descriptor
String Form:<method 'readlines' of 'file' objects>
Namespace: Python builtin
Docstring:
readlines([size]) -> list of strings, each a line from the file.
Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.