反转函数中的每一行,但输出打印为一行

时间:2016-10-31 05:27:44

标签: python python-3.x

我试图反转多行文件中的每个单词并在同一行上打印单词。所以基本上,如果输入的文字是:

  

我喜欢跑步。

     

跑步很有趣。

     

结束。

我希望输出读取:

  

我ekil ot .nur

     

gninnuR si .nuf

     

ehT .dne

但它正在输出

  

我ekil ot .nur   gninnuR si .nuf   ehT .dne

到目前为止我的代码是这样的:

f = open(file, "r")    #the input goes here
data = f.read()
for line in data:
    reverse = ' '.join(line[::-1] for line in data.split())

print(reverse)

如何解决此问题逐行打印?任何帮助表示感谢,谢谢。

6 个答案:

答案 0 :(得分:2)

您需要在\n上拆分以对每条线路进行操作。您的代码当前在空格上执行拆分(默认情况下),包括空格和换行符,这就是您将所有单词放在一行上的原因。

只需使用readlines()方法,该方法已经为您提供了文件中的行列表,如下所示:

with open('file', 'r') as f:
     data=f.readlines()
     for line in data:
             print(' '.join(word[::-1] for word in line.split()))

这是输出:

I ekil ot .nur
gninnuR si .nuf
ehT .dne

答案 1 :(得分:1)

您应首先按.split("\n")拆分行,然后反转行中的每个单词,然后再将它们连接起来。

data = f.read()
for line in data.split("\n"):
    print (' '.join([l[::-1] for l in line.split()]))

输出:

I ekil ot .nur 
gninnuR si .nuf 
ehT .dne

答案 2 :(得分:1)

分割后再次加入新行。如果您没有使用rev循环并选择单个:

I ekil ot .nur

gninnuR si .nuf

ehT .dne

现在Mockito.spy看起来像您预期的输出:

baseClass object = new baseClass(new FetchData());
baseClass spy = Mockito.spy(object);

Mockito.when(spy.parse()).thenCallRealMethod();
Mockito.when(spy.transform()).thenReturn("Something");

答案 3 :(得分:0)

该计划存在一些问题:

# opens the file but never closes it explicitly.
f = open(file, "r")    #the input goes here

# reads the whole file as one long string.
data = f.read()

# iterates one character at a time through the string (about 40 loops)
for line in data:
    # Computes the same thing about 40 times on the entire file in "data".
    reverse = ' '.join(line[::-1] for line in data.split())

# prints only the last computation.
print(reverse)

以下是修复:

# closes the file when with block exits
with open(file) as data:
    # data is a file iterator.  This reads one *line* at a time.
    for line in data:
        # split on whitespace getting words, reverse word, and join again.
        print(' '.join(word[::-1] for word in line.split()))

输出:

I ekil ot .nur
gninnuR si .nuf
ehT .dne

答案 4 :(得分:0)

您的代码存在3个问题。

  1. read方法逐字逐句读取文件,导致每行打印字母。为了解决这个问题,您需要按行读取每个文件。您可以使用" readlines"方法

  2. 这与第一个问题无关,连接方法是不必要的。事实上,你可能使用它的唯一原因是因为第一个问题

  3. 你的反向变量是for循环的局部变量,因此在for循环之外打印它不是一个好主意你应该在for循环中打印

  4. 这是你的代码略微调整

    import os
    
    f = open(file, "r")
    data = f.readlines() # changed "read()" to "readlines()"
    for line in data:
        reverse = line.strip()[::-1] # removed "join" method
    
        print(reverse) # called "print" function within the for loop
    

    这是另一种方法。

    import os
    
    
    with open('input.txt') as fl:
        for line in fl:
            reverse_line = line.strip()[::-1]
            print(reverse_line)
    

答案 5 :(得分:0)

您可以使用返回反向列表的内置函数 reversed()。 并在每行末尾加入reversed_ans = str() with open('file', 'r') as f: data = f.readlines() for line in data: reversed_ans += " ".join(["".join(reversed(x)) for x in line.split()]) + "\n" print(reversed_ans)

return reversed_ans

如果您想在函数中使用div,此解决方案非常有用。或者,如果您想将该字符串插入另一个文件中。