2017-05-26 127 views
-3

我有一個Python的任務有什麼需要我做下面的一個文件,分割用逗號

*創建一個程序:*

  1. 按姓氏來檢索和顯示搜索此數據文件聯繫人
  2. 細節搜索這個數據文件的出生日期(DD/MM/YYYY)的日期在特定月份檢索和顯示的生日所有聯繫人
  3. 添加一個新的個人詳細資料文件

    file = open ("c:\ABOOK.txt", "r") 
    
    myfile = (file.readline()) 
    
    mywordlist = myfile.split() 
    
    
    mylength= len(mywordlist)# reads how long it is 
    print(mylength) 
    count = 0 # counts 
    afound = 0 # counts 
    s_name = input("Enter a surname to be found:") 
    textfile=(file.readlines()) 
    print(textfile[0]) 
    mwl = textfile[0].split(",") 
    mwl1 = textfile[1].split(",") 
    mwl2 = textfile[2].split(",") 
    mwl3 = textfile[3].split(",") 
    mwl4 = textfile[4].split(",") 
    while(count<len(textfile)): 
        print(textfile[count]) 
        count = count + 1 
    print(mwl,mwl1,mwl2,mwl3,mwl4) 
    print(textfile.split(",")) 
    

該文件被打破「」年代和這裏的記事本的提取物;它的六條線。

Jackson,Samantha,2 Heather Row,Basingstoke,RG21 3SD,01256 
135434,23/04/1973,[email protected] 
Vickers,Jonathan,18 Saville Gardens,Reading,RG3 5FH,01196 
678254,04/02/1965,[email protected] 
Morris,Sally,The Old Lodge, Hook,RG23 5RD,01256 
728443,19/02/1975,[email protected] 
Cobbly,Harry,345 The High Street,Guildford,GU2 4KJ,01458 
288763,30/03/1960,[email protected] 
Khan,Jasmine,36 Hever Avenue,Edenbridge,TN34 4FG,01569 
276524,28/02/1980,[email protected] 
Vickers,Harriet,45 Sage Gardens,Brighton,BN3 2FG,01675 
662554,04/04/1968,[email protected] 
+3

請格式化你的代碼,你確實有它,縮進是非常重要的Python,更重要的是你有什麼實際問題?具體是什麼不工作... 同樣對於您的提取,請格式化它如何顯示。 – shash678

+0

我在筆記本電腦上,作爲最近醒來我有點shakey和觸摸板是有點敏感,它的格式,以我如何擁有它,而我堅持讓它讓整個文本文件由「,」分裂然後我需要得到,所以我可以在文件中找到它,並將人員數據添加到文件中。我不需要完全完成只是需要一些指導,除非你想完全完成它繼續我不能阻止你這樣做 –

+2

所以你的代碼是不是第一次後縮進? – shash678

回答

1

這裏是你的程序的前兩個部分基本解決注意,您可以改善這個有很多,如沒有硬編碼的文件名,條帶化的新行字符等(的問題,如果你使用這個確切的代碼,這將意味着你不會得到100%),但我會離開,作爲一個任務,爲您:

import sys 

def find_record_by_surname(input_surname): 
    found = False 
    with open("ABOOK.txt", "r") as f: 
     for line in f: 
      surname = line.split(',')[0] 
      if surname == input_surname.title(): 
       found = True 
    return found 

def return_details_by_surname(input_surname): 
    details = [] 
    with open("ABOOK.txt", "r") as f: 
     for line in f: 
      details = line.split(',') 
      if details[0] == input_surname.title(): 
       break 
    return details 

def find_record_by_month_of_birth(input_month): 
    if len(input_month) != 2: 
     input_month = "0" + input_month 
    found = False 
    with open("ABOOK.txt", "r") as f: 
     for line in f: 
      month = line.split(',')[6].split('/')[1] 
      if input_month == month: 
       found = True 
       break 
    return found 

def return_details_by_month_of_birth(input_month): 
    if len(input_month) != 2: 
     input_month = "0" + input_month 
    details = "" 
    with open("ABOOK.txt", "r") as f: 
     for line in f: 
      if line.split(',')[6].split('/')[1] == input_month: 
       details = details + line 
    return details 

if __name__ == "__main__": 
    print("Menu Choices") 
    print("============") 
    print("1: Search contacts by surname") 
    print("2: Search contacts by month of birth") 
    print("3: Add a new contact to ABOOK.txt") 
    print("4: Exit") 
    choice = input("Enter your choice: ") 
    while choice != "4": 
     if choice == "1": 
      input_surname = input("Enter a surname you would like the records for from ABOOK.txt? ") 
      if find_record_by_surname(input_surname): 
       print("The record with the entered surname was found in ABOOK.txt") 
       print("The details for that contact are:") 
       print(return_details_by_surname(input_surname)) 
      else: 
       print("The record with the entered surname was not found in ABOOK.txt") 
     if choice == "2": 
      input_month = input("Enter the month of birth (1-12) you would like the records for from ABOOK.txt? ") 
      if find_record_by_month_of_birth(input_month): 
       print("The details for the contact(s) with the entered month of birth are: ") 
       print(return_details_by_month_of_birth(input_month)) 
      else: 
       print("No record with the entered month of birth was found in ABOOK.txt") 
     if choice == "3": 
      # TO-DO code for adding a new contact 
      print() 
     print("Menu Choices") 
     print("============") 
     print("1: Search contacts by surname") 
     print("2: Search contacts by month of birth") 
     print("3: Add a new contact to ABOOK.txt") 
     print("4: Exit") 
     choice = input("Enter your choice: ") 
    print("Goodbye!")  
    sys.exit(0) 

ABOOK。使用txt文件:

Jackson,Samantha,2 Heather Row,Basingstoke,RG21 3SD,01256135434,23/04/1973,[email protected] 
Vickers,Jonathan,18 Saville Gardens,Reading,RG3 5FH,01196678254,04/02/1965,[email protected] 
Morris,Sally,The Old Lodge, Hook,RG23 5RD,01256728443,19/02/1975,[email protected] 
Cobbly,Harry,345 The High Street,Guildford,GU2 4KJ,01458288763,30/03/1960,[email protected] 
Khan,Jasmine,36 Hever Avenue,Edenbridge,TN34 4FG,01569276524,28/02/1980,[email protected] 
Vickers,Harriet,45 Sage Gardens,Brighton,BN3 2FG,01675662554,04/04/1968,[email protected] 
+1

嘿,非常感謝你,它的工作原理!!,接下來幫助我做的事情是試着找出如何改變這件作品,讓它搜索出生日期,然後如何添加someo ne到文件!救命的人你是先生 –

+0

對於日期,一個簡單的方法是使用一個嵌套的'if's你首先存儲日期:'date = line.split(',')[6]'然後再次使用split來查找如果月份與月份輸入的用戶匹配,則輸出:'month = date.split('/')[1]'。 – shash678

+0

我所做的是使用你的代碼,並修改它給我的日期,我似乎無法得到這個工作我一直等待24小時,直到我再次問我是否可以解決我的自我, –

0

爲了將一個字符串分解只使用

detail_arr = string.split(',') 

detail_arr現在將與您的不同行的數組。

以下是在代碼的示例:

>> str = "Jackson,Samantha,2 Heather Row,Basingstoke,RG21 3SD,01256135434,23/04/1973,[email protected]" 
>> str.split(',') 
['Jackson', 'Samantha', '2 Heather Row', 'Basingstoke', 'RG21 3SD', '01256135434', '23/04/1973', '[email protected]'] 

每>>在控制檯中執行的線。

爲了增加細節將它們添加到您的編曲,然後做:

str = ''.join(map(lambda x: x + ',', details_arr))[:-1] 

海峽現在將所有的數組用逗號分隔的,你可以把它寫入文件。

在你的代碼,你不希望在第3行,而是由逗號爲完成拆分的空間數據。

而且你有一段時間沒有退出條款所以你會停留在一個無限循環。

您的代碼應該如下所示:

file = open ("c:\ABOOK.txt", "r") 

data = file.read() 

data_rows = data.split(',') //every 8th row it will start a new entry (7,15,23...) 

現在,姓搜索將在行0,7,15搜索... 的生日搜索將是行6,14,22。 你可以把所有的行都列出來。您可以創建一個計數器,看看在看什麼接觸您選擇:

c = 0 //counter of entry 
for i in xrange(len(data_rows)/8): 
    if data_rows[i+row_number] == val: // row_number depends on what youre trying to match 
     print ''.join(data_rows[c:c+8]) 
    c += 1 

現在你可以告訴項的值相匹配。

+0

這是如何在代碼行中執行的,我的老師並沒有真正地教給我很多,他只是給了對我而言,這些對我來說都是陌生的,而且作爲我的老師嚴格,他也會批准我,如果它不符合他的標準 –

+0

@ChristopherAllum Better? – IsaacDj

+0

謝謝:)這有助於我理解 –