永发信息网

使用Python按字节分割字符串

答案:2  悬赏:20  手机版
解决时间 2021-12-25 10:05
处理过程大致是:从文件中按行读取数据,对每一行数据按给定的字节(field length)插入分隔符再以UTF-8保存到新的文件。我试过分别以二进制和文本模式打开源文件再进行处理,但都比较繁琐并且失败,原因是多样化的。各位是否有更灵活简单的方法。感激不尽。
最佳答案
按行读取之后按原文件编码类型解码,插入完后按UTF-8解码写入文件
以源文件为gbk为例,假设每5字符插入|
python2
with open('target','w') as f:
    for line open('source').readlines():
        line=line.decode('gbk')
        line='|'.join([line[i:min(i+5,len(line))] for i in range(0,len(line),5)])
        f.write(line.encode('utf-8'))python3
with open('target','w',encoding='utf-8') as f:
    for line open('source',encoding='gbk').readlines():
        line=line
        line='|'.join([line[i:min(i+5,len(line))] for i in range(0,len(line),5)])
        f.write(line)
全部回答
唔,你也没写具体问题…… 给你个python的字符串处理汇总吧。 str='python string function' 生成字符串变量str='python string function' 字符串长度获取:len(str) 例:print '%s length=%d' % (str,len(str)) 一、字母处理 全部大写:str.upper() 全部小写:str.lower() 大小写互换:str.swapcase() 首字母大写,其余小写:str.capitalize() 首字母大写:str.title() print '%s lower=%s' % (str,str.lower()) print '%s upper=%s' % (str,str.upper()) print '%s swapcase=%s' % (str,str.swapcase()) print '%s capitalize=%s' % (str,str.capitalize()) print '%s title=%s' % (str,str.title())  二、格式化相关 获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width) 获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width) 获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width) 获取固定长度,右对齐,左边不足用0补齐 print '%s ljust=%s' % (str,str.ljust(20)) print '%s rjust=%s' % (str,str.rjust(20)) print '%s center=%s' % (str,str.center(20)) print '%s zfill=%s' % (str,str.zfill(20)) 三、字符串搜索相关 搜索指定字符串,没有返回-1:str.find('t') 指定起始位置搜索:str.find('t',start) 指定起始及结束位置搜索:str.find('t',start,end) 从右边开始查找:str.rfind('t') 搜索到多少个指定字符串:str.count('t') 上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1 print '%s find nono=%d' % (str,str.find('nono')) print '%s find t=%d' % (str,str.find('t')) print '%s find t from %d=%d' % (str,1,str.find('t',1)) print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2)) #print '%s index nono ' % (str,str.index('nono',1,2)) print '%s rfind t=%d' % (str,str.rfind('t')) print '%s count t=%d' % (str,str.count('t')) 四、字符串替换相关 替换old为new:str.replace('old','new') 替换指定次数的old为new:str.replace('old','new',maxreplacetimes) print '%s replace t to *=%s' % (str,str.replace('t', '*')) print '%s replace t to *=%s' % (str,str.replace('t', '*',1)) 五、字符串去空格及去指定字符 去两边空格:str.strip() 去左空格:str.lstrip() 去右空格:str.rstrip() 去两边字符串:str.strip('d'),相应的也有lstrip,rstrip str=' python string function ' print '%s strip=%s' % (str,str.strip()) str='python string function' print '%s strip=%s' % (str,str.strip('d')) 按指定字符分割字符串为数组:str.split(' ') 六、默认按空格分隔 str='a b c de' print '%s strip=%s' % (str,str.split()) str='a-b-c-de' print '%s strip=%s' % (str,str.split('-')) 七、字符串判断相关 是否以start开头:str.startswith('start') 是否以end结尾:str.endswith('end') 是否全为字母或数字:str.isalnum() 是否全字母:str.isalpha() 是否全数字:str.isdigit() 是否全小写:str.islower() 是否全大写:str.isupper() str='python string function' print '%s startwith t=%s' % (str,str.startswith('t')) print '%s endwith d=%s' % (str,str.endswith('d')) print '%s isalnum=%s' % (str,str.isalnum()) str='pythonstringfunction' print '%s isalnum=%s' % (str,str.isalnum()) print '%s isalpha=%s' % (str,str.isalpha()) print '%s isupper=%s' % (str,str.isupper()) print '%s islower=%s' % (str,str.islower()) print '%s isdigit=%s' % (str,str.isdigit()) str='3423' print '%s isdigit=%s' % (str,str.isdigit())
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
女朋友说头疼 怎么去安慰
信用卡随时都可以激活吗?
山地车怎么提起车头
以铜为镜可以整衣冠,以古为镜可以知兴衰,!一
600平方小型酒店空调方案
陶和瓷制作的茶具有什么不同
如图是植物细胞和动物细胞的结构示意图,据图
夏季,我国北方和南方气温相差不大的最主要原
上海到贵阳火车票卧铺
音速多少公里每小时
单选题两圆共有3条公切线,则这两圆的位置关
胡耘豪《老九门番外之四屠黄葵》什么时候播出
黑腹内什么意思
Our school basketball team is going to som
新工程公司简介怎么写
推荐资讯
世缘家纺(昆山同丰路店)地址在什么地方,想过
单选题能够体现共同富裕的经济成份是A.公有制
什么时候做腊肉
用五笔打字法怎么打这、、、、 就“杰”字下
柳树营村委会办公位置在哪啊?好找么?
请律师去探监了解案情费用是多少
was known to them all that William had bro
单选题对诗句所描写的季节判断有误的一项是A.
不履行承诺的话叫什么言 10分
惠东县公安局高潭派出所办公位置在哪啊?好找
夏天怎么做腊肉
宿州到承德有多少公里
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?