简体   繁体   English

如何在python中的换行符之前从行尾删除多余的空间?

[英]How to remove extra space from end of the line before newline in python?

I'm quite new to python. 我是python的新手。 I have a program which reads an input file with different characters and then writes all unique characters from that file into an output file with a single space between each of them. 我有一个程序,该程序读取具有不同字符的输入文件,然后将该文件中的所有唯一字符写入输出文件,并且每个文件之间都有一个空格。 The problem is that after the last character there is one extra space (before the newline). 问题在于最后一个字符之后有一个多余的空间(在换行符之前)。 How can I remove it? 如何删除?

My code: 我的代码:

import sys
inputName = sys.argv[1]
outputName = sys.argv[2]

infile = open(inputName,"r",encoding="utf-8")
outfile = open(outputName,"w",encoding="utf-8")
result = []
for line in infile:

   for c in line:
       if c not in result:
           result.append(c)
           outfile.write(c.strip())

          if(c == ' '):
             pass
          else:
             outfile.write(' ')

outfile.write('\n')

With the line outfile.write(' ') , you write a space after each character (unless the character is a space). 使用outfile.write(' ') ,您可以在每个字符后写一个空格(除非该字符是一个空格)。 So you'll have to avoid writing the last space. 因此,您必须避免写最后一个空格。 Now, you can't tell whether any given character is the last one until you're done reading, so it's not like you can just put in an if statement to test that, but there are a few ways to get around that: 现在,在完成阅读之前,您无法确定是否有任何给定的字符是最后一个字符,因此,这不像您可以放入if语句来测试该字符,但是有几种解决方法:

  • Write the space before the character c instead of after it. 在字符c 之前而不是在字符c 之前写空格。 That way the space you have to skip is the one before the first character, and that you definitely can identify with an if statement and a boolean variable. 这样,你有空间跳过是第一个字符前一个,那你绝对可以识别if语句和布尔变量。 If you do this, make sure to check that you get the right result if the first or second c is itself a space. 如果这样做,请确保如果第一个或第二个c本身是空格,请检查是否获得正确的结果。
  • Alternatively, you can avoid writing anything until the very end. 或者,您可以避免在最后写任何东西。 Just save up all the characters you see - you already do this in the list result - and write them all in one go. 只需保存您看到的所有字符-您已经在列表result进行了此操作-一次性全部写入。 You can use 您可以使用

     ' '.join(strings) 

    to join together a list of strings (in this case, your characters) with spaces between them, and this will automatically omit a trailing space. 将字符串列表(在本例中为您的字符)之间用空格连接在一起,这将自动省略尾随空格。

Why are you adding that if block on the end? 为什么最后添加if块呢? Your program is adding the extra space on the end. 您的程序在末尾添加了额外的空间。

import sys
inputName = sys.argv[1]
outputName = sys.argv[2]

infile = open(inputName,"r",encoding="utf-8")
outfile = open(outputName,"w",encoding="utf-8")
result = []
for line in infile:
   charno = 0
   for c in line:
       if c not in result:
           result.append(c)
           outfile.write(c.strip())

          charno += 1
          if (c == ' '):
              pass
          elif charno => len(line):
              pass
          else:
              outfile.write(' ')

outfile.write('\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM