簡體   English   中英

在python中使用csv工具對txt文件進行排序的最佳方法

[英]Best way to sort txt file using csv tools in python

我有以下代碼,並試圖以最簡單的方法對文件內容進行排序。

import csv
import operator

#==========Search by ID number. Return Just the Name Fields for the Student
with open("studentinfo.txt","r") as f:
  studentfileReader=csv.reader(f)
  id=input("Enter Id:")
  for row in studentfileReader:
    for field in row:
      if field==id:
        currentindex=row.index(id)
        print(row[currentindex+1]+" "+row[currentindex+2])

#=========Sort by Last Name
with open("studentinfo.txt","r") as f:
  studentfileReader=csv.reader(f)
  sortedlist=sorted(f,key=operator.itemgetter(0),reverse=True)
  print(sortedlist)

我知道各種可能的解決方案,但不能完全使它們正常運行,並且出於教學/學習的目的,我會對最簡單有效的解決方案感興趣, 並給出了明確的解釋。

研究包括: ****導入運算符**** sortedlist = sorted(閱讀器,鍵= operator.itemgetter(3),reverse = True)

或使用lambda sortedlist = sorted(閱讀器,鍵= lambda行:row [3],reverse = True)

對於ANSWER,如果有人可以發布完整的解決方案,並按姓氏和ID號進行排序,以說明兩個不同的示例,我將不勝感激。 答案的擴展將是在此特定示例中顯示如何按多個值排序:

完整代碼清單:

https://repl.it/Jau3/3

文件內容

002,Ash,Smith,Test1:20,Test2:20,Test3:100003
004,Grace,Asha,Test1:33,Test2:54,Test3:23
005,Cat,Zelch,Test1:66,Test2:22,Test3:11
001,Joe,Bloggs,Test1:99,Test2:100,Test3:1
003,Jonathan,Peter,Test1:99,Test2:33,Test3:44

您可以使用lambda函數按csv閱讀器返回的列表的任何鍵進行排序,例如,按姓氏(第三列)進行排序:

with open("studentinfo.txt", "r") as f:
    reader = csv.reader(f)
    sorted_list = list(reader)  # turn the reader iterator into a list
    sorted_list.sort(key=lambda x: x[2])  # use the third column as a sorting key
    print("\n".join(str(row) for row in sorted_list))  # prettier print

或按ID(第一列):

with open("studentinfo.txt", "r") as f:
    reader = csv.reader(f)
    sorted_list = list(reader)  # turn the reader iterator into a list
    sorted_list.sort(key=lambda x: x[0])  # the first column as a sorting key, can be omitted
    print("\n".join(str(row) for row in sorted_list))  # prettier print

或通過兩個鍵:

with open("studentinfo.txt", "r") as f:
    reader = csv.reader(f)
    sorted_list = list(reader)  # turn the reader iterator into a list
    sorted_list.sort(key=lambda x: (x[3], x[4]))  # use fourth and fifth column
    print("\n".join(str(row) for row in sorted_list))  # prettier print

您可以在list.sort()調用中添加reverse=True進行降序排序。

附件 -如果您確實不想使用lambda(為什么?),則可以定義一個項獲取函數(或僅使用為此目的而存在的operator.itemgetter )並將其傳遞給list.sort()調用,例如:

def get_third_column(x):
    return x[2]

with open("studentinfo.txt", "r") as f:
    reader = csv.reader(f)
    sorted_list = list(reader)  # turn the reader iterator into a list
    sorted_list.sort(key=get_third_column)  # use the third column as a sorting key
    print("\n".join(str(row) for row in sorted_list))  # prettier print

一種緊湊,簡單的解決方案,可讀取->排序->寫入:

import csv
import operator

with open("input.csv") as fh:
    reader = csv.reader(fh)
    rows = sorted(reader, key=operator.itemgetter(0), reverse=True)

with open("output.csv", "w") as fh:
    csv.writer(fh).writerows(rows)

要在控制台上打印而不是寫入文件,可以使用sys.stdout作為文件句柄:

import sys

with sys.stdout as fh:
    csv.writer(fh).writerows(rows)

operator.itemgetter(0)確定要排序的字段。 第0個字段是ID。 要按姓氏排序,請使用operator.itemgetter(2) ,因為姓氏是第三列。

要按多個字段排序,您將需要使用lambda,例如按姓氏和名字排序:

    rows = sorted(reader, key=lambda x: (x[2], x[1]), reverse=True)

您還可以改進排序之前要求用戶輸入ID的代碼:

  • 當您知道id字段是第一個字段時,就不需要遍歷每個字段
  • id隱藏了Python中的內置函數,因此不建議將其用作變量

您可以這樣寫:

with open("studentinfo.txt") as fh:
    reader = csv.reader(fh)
    student_id = input("Enter Id:")
    for row in reader:
        if row[0] == student_id:
            print(row[1] + " " + row[2])

完成操作后,使用import運算符和一個可能的解決方案:注意-理想情況下,您需要一個標頭來區分要排序的內容(假設用戶想明確指定此內容)

import csv
import operator
ifile =open('myfile.csv', 'rb')
infile = csv.reader(ifile)
# Note that if you have a header, this is the header line
infields = infile.next()
startindex = infields.index('Desired Header')
# Here you are creating the sorted list
sortedlist = sorted(infile, key=operator.itemgetter(startindex), reverse=True)
ifile.close
# open the output file - it can be the same as the input file
ofile = open('myoutput.csv, 'wb')
outfile.writerow(infields)
for row in sortedlist:
  outfile.writerow(row)
ofile.close()

暫無
暫無

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

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