译(二十九)-Python按行读取文件并存进列表

文章首发及后续更新:https://mwhls.top/3093.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。

*热门问题目录

如有翻译问题欢迎评论指出,谢谢。

目录 1. 如何按行读取文件并存进列表? 2. How to read a file line-by-line into a list?

如何按行读取文件并存进列表?

  • Julie Raswick asked:

    • Python如何读取一个文件的每一行,并将其存进列表里?
    • 按行读取,然后追加到列表末尾。
  • Answers:

    • SilentGhost – vote: 2450

    • 下面的代码可以读取文件至内存,并且移除每行的空白字符(换行符与空格):

    • with open(filename) as file:
      lines = file.readlines()`
      lines = [line.rstrip() for line in lines]

  • 如果是读取大文件,最后按行读取并处理:

  • with open(filename) as file:
      for line in file:    
          print(line.rstrip())
  • Python3.8可以用带海象运算符的while循环(译者注::=长得像海象,所以这么叫,文档里说的):

  • with open(filename) as file:
     while (line := file.readline().rstrip()):    
         print(line)```
  • 根据文件的处理需求与编码方式来设置不同的读取方式与编码方式:

  • with open(filename, 'r', encoding='UTF-8') as file:
     while (line := file.readline().rstrip()):  
         print(line)```
  • Felix Kling – vote: 1108

  • 输入与输出

  • with open('filename') as f:
     lines = f.readlines()```
  • 或者去除换行符:

  • with open('filename') as f:
      lines = [line.rstrip() for line in f]```
  • robert – vote: 667

  • 虽然这不简洁,但的确如要求一般:

  • with open(file.txt) as file_in:
     lines = []  
     for line in file_in:    
         lines.append(line)```

  • How to read a file line-by-line into a list?

    • Julie Raswick asked:

      • How do I read every line of a file in Python and store each line as an element in a list?
        Python如何读取一个文件的每一行,并将其存进列表里?
      • I want to read the file line by line and append each line to the end of the list.
        按行读取,然后追加到列表末尾。
    • Answers:

      • SilentGhost – vote: 2450

      • This code will read the entire file into memory and remove all whitespace characters (newlines and spaces) from the end of each line:
        下面的代码可以读取文件至内存,并且移除每行的空白字符(换行符与空格):

      • with open(filename) as file:
          lines = file.readlines()`    
          lines = [line.rstrip() for line in lines]
      • If you\’re working with a large file, then you should instead read and process it line-by-line:
        如果是读取大文件,最后按行读取并处理:

      • with open(filename) as file:
          for line in file:    
              print(line.rstrip())
      • In Python 3.8 and up you can use a while loop with the walrus operator like so:
        Python3.8可以用带海象运算符的while循环(译者注::=长得像海象,所以这么叫,文档里说的):

      • with open(filename) as file:
         while (line := file.readline().rstrip()):    
             print(line)```
      • Depending on what you plan to do with your file and how it was encoded, you may also want to manually set the access mode and character encoding:
        根据文件的处理需求与编码方式来设置不同的读取方式与编码方式:

      • with open(filename, 'r', encoding='UTF-8') as file:
         while (line := file.readline().rstrip()):  
             print(line)```
      • Felix Kling – vote: 1108

      • See Input and Ouput:
        输入与输出

      • with open('filename') as f:
         lines = f.readlines()```
      • or with stripping the newline character:
        或者去除换行符:

      • with open('filename') as f:
          lines = [line.rstrip() for line in f]```
      • robert – vote: 667

      • This is more explicit than necessary, but does what you want.
        虽然这不简洁,但的确如要求一般:

      • with open(file.txt) as file_in:
         lines = []  
         for line in file_in:    
             lines.append(line)```
上一篇:LSD直线检测MATLAB


下一篇:03 Spark RDD编程基础