簡體   English   中英

如何從python中的最后一個單詞到第一個單詞讀取txt文件上的單詞(或字符)?

[英]How to read word(or character) on txt file from the last word to the first word in python?

示例:我有一個文件:
filename1 =“我是學生”(在filename1中有我是學生)

f = open(filename1)
string = f.read()
spl = re.split('\s|(?<!\d)[,.](?!\d)',string)
f.close

print spl將會顯示:我是學生,但我需要結果,因為[學生是我]請您回答我...預先感謝。

您可以使用火星的笑臉反轉列表:

spl = spl[::-1]

或者,如果您只需要迭代器:

spl = reversed(spl)
str=['im a student','im a teacher']
list=[]
for x in str:
   new=""
   for y in [x for x in reversed(x.split())]:
       new+=y
       new+=' '
   list.append(new)

print list // ['student a im ', 'teacher a im ']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM