程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Data analysis with Python -- files and operating systems

編輯:Python

List of articles

  • 1 Basic knowledge

1 Basic knowledge

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.',
'']

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved