Read data files on disk into Python data structure , Most of them use pandas.read_csv
Advanced tools like that .
To open a file for reading and writing , You can use the built-in open function , And a relative or absolute file path .
In [207]: path = 'examples/segismundo.txt'
In [208]: f = open(path)
By default , The file is in read-only mode (’r’)
The open . then , We can deal with this file handle just as we do with lists f 了 , For example, iterating over rows :
for line in f:
pass
Because the lines taken from the file have line terminators (EOL), So get rid of EOL The method is as follows :
In [209]: lines = [x.rstrip() for x in open(path)]
In [210]: lines
Out[210]:
['Sueña el rico en su riqueza,',
'que más cuidados le ofrece;',
'',
'sueña el pobre que padece',
'su miseria y su pobreza;',
'',
'sueña el que a medrar empieza,',
'sueña el que afana y pretende,',
'sueña el que agravia y ofende,',
'',
'y en el mundo, en conclusión,',
'todos sueñan lo que son,',
'aunque ninguno lo entiende.',
'']