python 怎么去掉文本中每一行的首字符

作者&投稿:应哗 (若有异议请与网页底部的电邮联系)
如何用python把文件中每行字符前面的空格去掉~

file = 'test.txt'
fp = open('file')
while True:
if not line:
break
#处理前

line = fp.readline()

print line

#处理后

line = line.strip()
print line
fp.close()

1、新建一个php文件,命名为test.php,用于讲解php如何删除字符串第一个字符。

2、在test.php文件中,使用header()方法将页面的编码格式设置为utf-8,避免输出中文乱码。

3、在test.php文件中,将要删除的字符串存放在$str变量中。

4、在test.php文件中,使用substr()函数从字符串第二个字符开始截取至末尾字符串,同时将截取后的字符串替换原字符串$str。

5、在test.php文件中,使用echo输出截取后的结果。

6、在浏览器打开test.php文件,查看结果。

# 去掉首字符
txt = 'abcdefg'
print(txt[1:]) # bcdefg

# 去掉空格前字符
txt = '1 2345678'
print(txt.split()[1]) # 2345678

# 去掉每行首空格前字符
txt = '''
2 #define PORT 25341
3 #define BACKLOG 5
4 #define MEM_SIZE 1024
'''
arr = txt.strip().split('
') # 用换行符分割成列表
print(arr) # ['2 #define PORT 25341', '3 #define BACKLOG 5', '4 #define MEM_SIZE 1024']
newArr = [] # 创建新列表
for v in arr:
    varr   = v.split() # 用空格分割成列表
    newStr = v.replace(varr[0] + ' ', '') # 去掉第一个
    newArr.append(newStr) # 添加到新列表
print(newArr) # ['#define PORT 25341', '#define BACKLOG 5', '#define MEM_SIZE 1024']



你,确定这是...python?


建平县17769976370: 在python 里如何用replace删除文本里所有的空行 -
徐炕吡硫: 非要用replace吗,我都是用 x for x in L where L!='\n' 来去除空行的

建平县17769976370: Python中删除文档中的固定行 -
徐炕吡硫: import relist = [] matchPattern = re.compile(r'.+:\sdana') #简陋的reg用来匹配包含'dana'的那行 file = open('source.txt','r') #假设'source.txt'是你要处理的文档 while 1:line = file.readline() #从文件中读出一行if not line: #如果读出的是文件结尾,...

建平县17769976370: 如何用python实现去掉文本中的行序号?(行号) -
徐炕吡硫: #-*- coding: utf-8 -*-import res = '''1 #!/usr/bin/env python23 from cgi import FieldStorage4 from os import environ5 from cStringIO import StringIO 6 from urllib import quote, unquote7 from string import capwords, strip, split, join89 class AdvCGI(...

建平县17769976370: 用python代码实现txt文档的去除不符合条件的行(即去掉1.txt中和2.txt有一定条件的行) -
徐炕吡硫: import sys import os import string fp1= open('1.txt','r') fp2= open('2.txt','r') fp3= open('3.txt','a+')#读取文件首行,无需处理 str1=fp1.readline() fp2.readline() fp3.write(str1) for oneLine in fp1 : str1= oneLine.split() t=1 fp2.seek(0) for PLine in fp2 : str2= ...

建平县17769976370: python怎么删除txt文件的前12行 -
徐炕吡硫: import osDELNUM=12i=1openFileHandle=open('filename','r')writeFileHandle=open('Temp','w')while 1: line=openFileHandle.readline() if line: if i<=DELNUM: i=i+1 print "找到%d行:%s"%(DELNUM,line) continue else: i=i+1 ...

建平县17769976370: 如何用python删除文件尾行空行 -
徐炕吡硫: Python读取一个文本文件,删除文本文件的空行代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15def delblankline(infile, outfile): """ Delete blanklines of infile """ infp = open(infile, "r")o utfp = open(outfile, "w") lines = infp.readlines() for...

建平县17769976370: pandas python 怎么删除表格中的某一行 -
徐炕吡硫: data.drop(n)可以删除第i行import pandas as pddata=pd.DataFrame([[1,2,3],[4,5,6]])print data.drop(0)输出结果为 0 1 2 1 4 5 6

建平县17769976370: python关于删除特定行的问题 -
徐炕吡硫: ##No.1 import re for line in open("a.txt"): if not re.match("abcd", line): print line[:-1] #标准输出比较方便##No.2 omit = False for line in open("a.txt"): if line == "#start\n": omit = True elif line == "#end\n": omit = False else: if not omit: print line[:-1]

建平县17769976370: python怎么删除文本的最后一行或者指定行 -
徐炕吡硫: 直接readlines()转成列表.然后删掉list最后一行不就行啦

建平县17769976370: python如何去除文本中的换行 -
徐炕吡硫: f = codecs.open('d.txt','r')for line in f: s += line.strip().encode('utf-8')

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网