Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我正在编写一个python脚本,它可以去掉行尾的空白,但它只在第一行工作_Python_For Loop_Text Files_Whitespace - Fatal编程技术网

I';我正在编写一个python脚本,它可以去掉行尾的空白,但它只在第一行工作

I';我正在编写一个python脚本,它可以去掉行尾的空白,但它只在第一行工作,python,for-loop,text-files,whitespace,Python,For Loop,Text Files,Whitespace,正如我在标题中所说,我的脚本似乎只在第一行起作用。 这是我的剧本: #!/usr/bin/python import sys def main(): a = sys.argv[1] f = open(a,'r') lines = f.readlines() w = 0 for line in lines: spot = 0 cp = line for char in reversed(cp): x = -1 if cha

正如我在标题中所说,我的脚本似乎只在第一行起作用。
这是我的剧本:

#!/usr/bin/python

import sys



def main():
  a = sys.argv[1]
  f = open(a,'r')
  lines = f.readlines()
  w = 0
  for line in lines:
    spot = 0
    cp = line
    for char in reversed(cp):
      x = -1
      if char == ' ':
        del line[x]
        w += 0
      if char != '\n' or char != ' ':
        lines[spot] = line
        spot += 1
        break
      x += 1
  f.close()
  f = open(a,'w')
  f.writelines(lines)
  print("White Space deleted: "+str(w))


if __name__ == "__main__":
  main()

对于循环,我没有太多的经验。

以下脚本与您的程序做同样的事情,更简洁:

import fileinput

deleted = 0
for line in fileinput.input(inplace=True):
    stripped = line.rstrip()
    deleted += len(line) - len(stripped) + 1  # don't count the newline
    print(stripped)

print("Whitespace deleted: {}".format(deleted))
此处删除行尾的所有空白(换行符、空格和制表符)

如果您命名了多个文件,则会为您处理
sys.argv
,逐个打开文件


使用
print()
将换行符添加回剥离行的末尾。

以下脚本执行与程序相同的操作,更加简洁:

import fileinput

deleted = 0
for line in fileinput.input(inplace=True):
    stripped = line.rstrip()
    deleted += len(line) - len(stripped) + 1  # don't count the newline
    print(stripped)

print("Whitespace deleted: {}".format(deleted))
此处删除行尾的所有空白(换行符、空格和制表符)

如果您命名了多个文件,则会为您处理
sys.argv
,逐个打开文件


使用
print()
将换行符添加回剥离行的末尾。

以下脚本执行与程序相同的操作,更加简洁:

import fileinput

deleted = 0
for line in fileinput.input(inplace=True):
    stripped = line.rstrip()
    deleted += len(line) - len(stripped) + 1  # don't count the newline
    print(stripped)

print("Whitespace deleted: {}".format(deleted))
此处删除行尾的所有空白(换行符、空格和制表符)

如果您命名了多个文件,则会为您处理
sys.argv
,逐个打开文件


使用
print()
将换行符添加回剥离行的末尾。

以下脚本执行与程序相同的操作,更加简洁:

import fileinput

deleted = 0
for line in fileinput.input(inplace=True):
    stripped = line.rstrip()
    deleted += len(line) - len(stripped) + 1  # don't count the newline
    print(stripped)

print("Whitespace deleted: {}".format(deleted))
此处删除行尾的所有空白(换行符、空格和制表符)

如果您命名了多个文件,则会为您处理
sys.argv
,逐个打开文件

使用
print()
会将换行符重新添加到剥离行的末尾。

rstrip()
可能就是您想要使用的方法

>>> 'Here is my string       '.rstrip()
'Here is my string'
在stings上向后迭代的一种更紧凑的方法是

>>> for c in 'Thing'[::-1]:
        print(c)


g
n
i
h
T
[::-1]
是切片表示法。切片符号可以表示为
[开始:停止:步骤]
。在我的示例中,步骤的
-1
表示它将从后面一步一个索引<代码>[x:y:z]将从索引
x
开始,在
y-1
停止,并通过
z
位置向前推进每一步。

rstrip()
可能就是您想要实现这一点的方法

>>> 'Here is my string       '.rstrip()
'Here is my string'
在stings上向后迭代的一种更紧凑的方法是

>>> for c in 'Thing'[::-1]:
        print(c)


g
n
i
h
T
[::-1]
是切片表示法。切片符号可以表示为
[开始:停止:步骤]
。在我的示例中,步骤的
-1
表示它将从后面一步一个索引<代码>[x:y:z]将从索引
x
开始,在
y-1
停止,并通过
z
位置向前推进每一步。

rstrip()
可能就是您想要实现这一点的方法

>>> 'Here is my string       '.rstrip()
'Here is my string'
在stings上向后迭代的一种更紧凑的方法是

>>> for c in 'Thing'[::-1]:
        print(c)


g
n
i
h
T
[::-1]
是切片表示法。切片符号可以表示为
[开始:停止:步骤]
。在我的示例中,步骤的
-1
表示它将从后面一步一个索引<代码>[x:y:z]将从索引
x
开始,在
y-1
停止,并通过
z
位置向前推进每一步。

rstrip()
可能就是您想要实现这一点的方法

>>> 'Here is my string       '.rstrip()
'Here is my string'
在stings上向后迭代的一种更紧凑的方法是

>>> for c in 'Thing'[::-1]:
        print(c)


g
n
i
h
T

[::-1]
是切片表示法。切片符号可以表示为
[开始:停止:步骤]
。在我的示例中,步骤的
-1
表示它将从后面一步一个索引<代码>[x:y:z]将从索引
x
开始,在
y-1
停止,并通过
z
位置向前推进每一步。

只需使用
rstrip

f = open(a,'r')
lines = f.readlines()
f.close()
f = open(a,'w')
for line in lines:
    f.write(line.rstrip()+'\n')
f.close()

只需使用
rstrip

f = open(a,'r')
lines = f.readlines()
f.close()
f = open(a,'w')
for line in lines:
    f.write(line.rstrip()+'\n')
f.close()

只需使用
rstrip

f = open(a,'r')
lines = f.readlines()
f.close()
f = open(a,'w')
for line in lines:
    f.write(line.rstrip()+'\n')
f.close()

只需使用
rstrip

f = open(a,'r')
lines = f.readlines()
f.close()
f = open(a,'w')
for line in lines:
    f.write(line.rstrip()+'\n')
f.close()


string.rstrip()?它会删除行尾的空格。
w+=0
可能不是您想要的。。哦,是的,但这只是计算删除了多少空格@MartijnPieters@Awalrod:因此您每次都计数到0。
string.rstrip()
将从字符串的末尾删除空白。您知道Python有一个
str.rstrip()
方法,正确的?它会删除行尾的空格。
w+=0
可能不是您想要的。。哦,是的,但这只是计算删除了多少空格@MartijnPieters@Awalrod:因此您每次都计数到0。
string.rstrip()
将从字符串的末尾删除空白。您知道Python有一个
str.rstrip()
方法,正确的?它会删除行尾的空格。
w+=0
可能不是您想要的。。哦,是的,但这只是计算删除了多少空格@MartijnPieters@Awalrod:因此您每次都计数到0。
string.rstrip()
将从字符串的末尾删除空白。您知道Python有一个
str.rstrip()
方法,正确的?它会删除行尾的空格。
w+=0
可能不是您想要的。。哦,是的,但这只是计算删除了多少空格@MartijnPieters@Awalrod:因此每次都计数为0。如果其他人想知道:“可选的就地筛选:如果关键字参数inplace=1传递给fileinput.input()或者,对于FileInput构造函数,文件被移动到备份文件,标准输出被定向到输入文件(如果与备份文件同名的文件已经存在,它将以静默方式被替换)。这样就可以编写一个过滤器,在适当的位置重写其输入文件。“这是可行的,但为什么不打印(line.rstrip())
输出到终端?另外,
inplace=True
是什么意思。@Awalrod:
fileinput.input(inplace=True)
调用将
sys.stdout
重定向到输出文件。打印