我想通过反转text_in.txt文件中的单词来生成text_out.txt文件,如下所示:
text_in.txt具有两段,如下所示:
Hello world, I am Here.
I am eighteen years old.
text_out.txt应该是这样的:
Here. am I world, Hello
old. years eighteen am I
我的代码是:
filein = open ('text_in.txt', 'r+')
fileout = open ('text_out.txt', 'w')
data = filein.read()
array1 = data.split(' ')
array1.reverse()
out1 = " " .join(array1)
fileout.write(out1)
我的结果是old. years eighteen am I \nHere. am I world, Hello
有人可以帮我解决吗?非常感谢 !
参考方案
你需要:
k = """Hello world, I am Here.
I am eighteen years old."""
x = k.split("\n")
x = [" ".join(i.split()[::-1]) for i in x]
k = "\n".join(x)
print(k)
输出:
Here. am I world, Hello
old. years eighteen am I
Python sqlite3数据库已锁定 - python我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
Python-Excel导出 - python我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…
Python:传递记录器是个好主意吗? - python我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…
Python pytz时区函数返回的时区为9分钟 - python由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…