python中按字母排序_在Python中按字母顺序排序文本文件的内容

我想在文件内部按字母顺序排序。我当前执行此操作的代码不起作用,文件保持不变。这个程序本身就是一个基本的调查问卷,用来实验读写文件。在import time

import sys

name = input("What is your first name?").upper()

age = input("How old are you?")

while not age.isdigit():

print("Please Only Enter Numbers.")

age = input("How old are you?")

if int(age) <16:

infofile = "DatabaseMinor.txt"

elif int(age) >15 and int(age) <22:

infofile = "DatabaseYoungAdult.txt"

elif int(age) >21 and int(age) <65:

infofile = "DatabaseAdult.txt"

else:

infofile = "DatabaseSenior.txt"

gender = input("Are you [M]ale or [F]emale?").upper()

while gender not in {'M', 'F'}:

print("Please Only Enter M Or F.")

gender = str(input("Are you [M]ale or [F]emale?")).upper()

location = input("What country are you from? (UK)").upper()

while location not in {'ENGLAND', 'SCOTLAND', 'WALES', 'NORTHERN IRELAND'}:

print("Please Only Enter A Valid Country Within The UK.")

location = input("What country are you from?").upper()

#Compilation of inputs into a single line format

userinfo = name + " " + str(age) + " " + gender + " " + location + " " + (time.strftime("%d/%m/%Y")) + " " + (time.strftime("%H:%M:%S")) + '\n'

#Opening and writing value of the userinfo variable to the appropriate text file

file = open(infofile, 'a')

file.write(userinfo)

file.close()

file = open(infofile)

lines = file.readlines()

lines.sort()

file.close()

提前谢谢。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值