Python 反转函数中的每一行,但输出打印为一行
我试图在一个多行文件中反转每个单词,并在同一行上打印这些单词。因此,基本上,如果输入的文本是: 我喜欢跑步 跑步很有趣 结束 我希望输出为: 我爱你,努尔 gninnuR si.nuf ehT.dne 但它正在被输出为 我爱你,努尔 gninnuR si.nuf ehT.dne 到目前为止,我的代码是这样的:Python 反转函数中的每一行,但输出打印为一行,python,python-3.x,Python,Python 3.x,我试图在一个多行文件中反转每个单词,并在同一行上打印这些单词。因此,基本上,如果输入的文本是: 我喜欢跑步 跑步很有趣 结束 我希望输出为: 我爱你,努尔 gninnuR si.nuf ehT.dne 但它正在被输出为 我爱你,努尔 gninnuR si.nuf ehT.dne 到目前为止,我的代码是这样的: f = open(file, "r") #the input goes here data = f.read() for line in data: re
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)
我如何解决这一问题以逐行打印?非常感谢您的帮助。您应该先将行拆分为
。拆分(“\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
您应该首先按
.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
您需要在
\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
您需要在
\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
在新行上拆分后再次加入。如果不使用
循环,选择单个循环:
s = """I like to run.
Running is fun.
The end."""
rev = "\n".join(' '.join(sb[::-1] for sb in sub.split(' ')) for sub in s.split('\n'))
现在rev
看起来像您的预期输出:
I ekil ot .nur
gninnuR si .nuf
ehT .dne
在新行上拆分后再次加入。如果不使用循环,选择单个循环:
s = """I like to run.
Running is fun.
The end."""
rev = "\n".join(' '.join(sb[::-1] for sb in sub.split(' ')) for sub in s.split('\n'))
现在rev
看起来像您的预期输出:
I ekil ot .nur
gninnuR si .nuf
ehT .dne
该计划存在一些问题:
# 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
该计划存在一些问题:
# 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
您的代码有3个问题
read方法逐字读取文件,结果是每行都打印字母。为了解决这个问题,您需要逐行读取每个文件。您可以使用“readlines”方法来实现这一点
这与第一个问题有关,连接方法是不必要的。事实上,您使用它的唯一原因可能是因为第一个问题
您的反向变量是for循环的局部变量,因此在for循环外部打印它不是一个好主意。您应该在for循环内部打印它
这里是您的代码稍微调整
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)
您的代码有3个问题
read方法逐字读取文件,结果是每行都打印字母。为了解决这个问题,您需要逐行读取每个文件。您可以使用“readlines”方法来实现这一点
这与第一个问题有关,连接方法是不必要的。事实上,您使用它的唯一原因可能是因为第一个问题
您的反向变量是for循环的局部变量,因此在for循环外部打印它不是一个好主意。您应该在for循环内部打印它
这里是您的代码稍微调整
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)
您可以使用内置函数reversed()返回反向列表。
并在每行末尾加入“\n”
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)
如果希望在函数中返回反向的\u ans
,此解决方案非常有用。或者,如果您想在另一个文件中插入该字符串。您可以使用内置函数reversed()返回反向列表。
并在每行末尾加入“\n”
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)
如果希望在函数中返回反向的\u ans
,此解决方案非常有用。或者如果您想在另一个文件中插入该字符串