[英]How to read word(or character) on txt file from the last word to the first word in python?
Example: I have a file as : 示例:我有一个文件:
filename1 = "I am a student" (inside filename1 have I am a student ) filename1 =“我是学生”(在filename1中有我是学生)
f = open(filename1)
string = f.read()
spl = re.split('\s|(?<!\d)[,.](?!\d)',string)
f.close
print spl will show: I am a student , but I need the result as [student a am I] could you answer me please... Thanks in advance. print spl将会显示:我是学生,但我需要结果,因为[学生是我]请您回答我...预先感谢。
You can reverse the list with a martian smiley : 您可以使用火星的笑脸反转列表:
spl = spl[::-1]
Or, if you just need an iterator: 或者,如果您只需要迭代器:
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.