尝试按字母顺序打印和排序文件,但也跳过文件中的第一行
Trying to print and sort file alphabetically, but also skip the first line in the file
我对编码很陌生,对于一项作业,我试图简单地显示文件中的项目,按字母顺序排序,没有名称、价格等。
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print('File cannot be opened:', fname)
exit()
print("")
data = fhand.readlines()
data.sort()
for i in range(len(data)):
print(data[i])
当我 运行 这段代码时,它按照我想要的字母顺序对文件进行排序,但文件中的第一行包含在此排序过程中。
Flash, 300, 1, Figurine, Dc
Hulk, 100, 1, Figurine, Marvel
Name, Price, Quantity, Product, Brand
Obi Wan Kenobi, 400.99, 1, Figurine, Star Wars
Spiderman, 30.50, 1, Comic, Marvel
Superman, 150, 1, Figurine, DC
Wolverine, 20, 2, Comic, Marvel
忽略文件内容的上下文lol,只是玩弄文件排序。
尝试读取所有行但存储除第一行以外的所有行:
fname = input("Enter the file name: ")
try:
fhand = open(fname)
except:
print("File cannot be opened:", fname)
exit()
print("")
# read all lines and store all lines except the first line:
data = fhand.readlines()[1:]
data.sort()
for i in range(len(data)):
print(data[i])
我对编码很陌生,对于一项作业,我试图简单地显示文件中的项目,按字母顺序排序,没有名称、价格等。
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print('File cannot be opened:', fname)
exit()
print("")
data = fhand.readlines()
data.sort()
for i in range(len(data)):
print(data[i])
当我 运行 这段代码时,它按照我想要的字母顺序对文件进行排序,但文件中的第一行包含在此排序过程中。
Flash, 300, 1, Figurine, Dc
Hulk, 100, 1, Figurine, Marvel
Name, Price, Quantity, Product, Brand
Obi Wan Kenobi, 400.99, 1, Figurine, Star Wars
Spiderman, 30.50, 1, Comic, Marvel
Superman, 150, 1, Figurine, DC
Wolverine, 20, 2, Comic, Marvel
忽略文件内容的上下文lol,只是玩弄文件排序。
尝试读取所有行但存储除第一行以外的所有行:
fname = input("Enter the file name: ")
try:
fhand = open(fname)
except:
print("File cannot be opened:", fname)
exit()
print("")
# read all lines and store all lines except the first line:
data = fhand.readlines()[1:]
data.sort()
for i in range(len(data)):
print(data[i])