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

2021/10/9 14:50:44

本文主要是介绍译(二十九)-Python按行读取文件并存进列表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

stackoverflow热门问题目录

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

目录 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)```


这篇关于译(二十九)-Python按行读取文件并存进列表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程