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

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

stackoverflow热门问题目录

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

如何按行读取文件并存进列表?
  • 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)```
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值