sub在python中的意义_在Python中读取SUB以外的行

本文探讨了在Python 2.7.2中遇到的问题,即遇到ASCII码为1A的SUB字符时,文件读取会提前结束。解决方法是在Windows系统上以二进制模式打开文件,但会失去行尾换行信息。作者提供了如何绕过此问题并处理警告的方法。
摘要由CSDN通过智能技术生成

Newbie question. In Python 2.7.2., I have a problem reading text files which accidentally seem to contain some control characters. Specifically, the loop

for line in f

will cease without any warning or error as soon as it comes across a line containing the SUB character (ascii hex code 1a). When using f.readlines() the result is the same. Essentially, as far as Python is concerned, the file is finished as soon as the first SUB character is encountered, and the last value assigned line is the line up to that character.

Is there a way to read beyond such a character and/or to issue a warning when encountering one?

解决方案

On Windows systems 0x1a is the End-of-File character. You'll need to open the file in binary mode in order to get past it:

f = open(filename, 'rb')

The downside is you will lose the line-oriented nature and have to split the lines yourself:

lines = f.read().split('\r\n') # assuming Windows line endings

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值