私はそれを知っています既に回答された:)上記を要約すると:
# It is a good idea to store the filename into a variable.
# The variable can later become a function argument when the
# code is converted to a function body.
filename = 'data.txt'
# Using the newer with construct to close the file automatically.
with open(filename) as f:
data = f.readlines()
# Or using the older approach and closing the filea explicitly.
# Here the data is re-read again, do not use both ;)
f = open(filename)
data = f.readlines()
f.close()
# The data is of the list type. The Python list type is actually
# a dynamic array. The lines contain also the \n; hence the .rstrip()
for n, line in enumerate(data, 1):
print '{:2}.'.format(n), line.rstrip()
print '-----------------'
# You can later iterate through the list for other purpose, for
# example to read them via the csv.reader.
import csv
reader = csv.reader(data)
for row in reader:
print row
それは私のコンソールに出力します:
1. 12,12,12
2. 12,31,12
3. 1,5,3
-----------------
['12', '12', '12']
['12', '31', '12']
['1', '5', '3']
は私達にあなたのコードを表示し、これまで – Jordonias
'pydocのfile.readlines' – bjarneh
@bjarneh readlines()効率的なメモリではありません。 –