如何将文本文件中的列表按类别或标题按字母顺序排序?

2024-09-30 10:29:22 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的文本文件

#Listing showing sample book details 
#AUTHOR, TITLE, FORMAT, PUBLISHER, COST?, STOCK, GENRE
P.G. Wodehouse, Right Ho Jeeves, hb, Penguin, 10.99, 5, fiction
A. Pais, Subtle is the Lord, pb, OUP, 12.99, 2, biography
A. Calaprice, The Quotable Einstein, pb, PUP, 7.99, 6, science
M. Faraday, The Chemical History of a Candle, pb, Cherokee, 5.99, 1, science
C. Smith, Energy and Empire, hb, CUP, 60, 1, science
J. Herschel, Popular Lectures, hb, CUP, 25, 1, science
C.S. Lewis, The Screwtape Letters, pb, Fount, 6.99, 16, religion
J.R.R. Tolkein, The Hobbit, pb, Harper Collins, 7.99, 12, fiction
C.S. Lewis, The Four Loves, pb, Fount, 6.99, 7, religion
E. Heisenberg, Inner Exile, hb, Birkhauser, 24.95, 1, biography
G.G. Stokes, Natural Theology, hb, Black, 30, 1, religion

这是我用来读取文本文件并将其存储在列表中的代码 图书目录=[]

def readbook():
    
    infile = open('book_data_file.txt')

    for row in infile:
        start = 0 # used to start at the begginning of each line
        bookrecord = []

        if not(row.startswith('#')):
            for index in range(len(row)):
                if row[index] ==',' or index ==len(row)-1:
                    bookrecord.append(row[start:index])
                    start = index+1
                    
            book_list.append(bookrecord)
    infile.close()

有人能帮忙吗


Tags: theindexstartinfilerowscience文本文件pb
2条回答

给你:

按标题排序

def sortByTitle():
    data = ''
    title_list = []
    new_data = []
    
    with open('file.txt', 'r') as r:
        data = r.read()
    
    
    title_list = sorted(data.split(', ')[1::7])
    
    i=0
    while len(new_data)!=len(data.split(', '))//7:
        temp_data = data.split(', ')[7*i:7*(i+1)]
        new_data.append(temp_data)
        i+=1
 
    j=0
    while  j<len(title_list):
        
        for i in range(len(new_data)):
            if new_data[i][1] == title_list[j]:
                temp = new_data[j]
                new_data[j]=new_data[i]
                new_data[i] = temp 
                j+=1
            
    
    
    print('Title_list: ', title_list)
    print('Books: ',new_data)

按体裁分类

def sortByGenre():
    data = ''
    Genre_list = []
    new_data = []
    
    
    with open('file.txt') as r:
        data = r.read()
    
    
    Genre_list = sorted(data.split(', ')[6::7])
    i = 0
   
    while len(new_data)!=len(data.split(', '))//7:
        temp_data = data.split(', ')[7*i:7*(i+1)]
        new_data.append(temp_data)
        i+=1
    
    j=0
    while  j<len(Genre_list):
        for i in range(len(new_data)):
            if new_data[i][6] == Genre_list[j]:
                temp = new_data[j]
                new_data[j]=new_data[i]
                new_data[i] = temp 
                j+=1

    
    print('Genre: ', Genre_list)
    print('Books: ', new_data)

解决方案

import pandas as pd
df = pd.read_csv('book_data_file.txt' , sep=',' , header=0)
df.sort_values(by=["GENRE"])
df.sort_values(by=["TITLE"])

注释

您在文本文件中的列名周围留下了一些空格,请确保在导入之前删除这些空格并再次保存文本文件,否则列标题将有不必要的前导空格,如“流派”

解释

文本文件的格式类似于“csv”(逗号分隔的值文件),因此您可以使用Pandas“read_csv”将文本文件加载到Pandas数据框中。Pandas DataFrames允许您将数据格式化为表,这样您就可以按任何列对数据进行排序(以及无休止的其他操作)

这指定值用逗号分隔,因此现在需要用逗号分隔:

sep=','

这指定文本文件的第一行包含列标题名称:

header=0

然后,您可以使用以下命令按任意列进行排序:

df.sort_values(by=["COLUMN_NAME"])

相关问题 更多 >

    热门问题