字典python与csv文件

时间:2016-08-29 14:10:19

标签: python csv dictionary

我对所有这些编码都很陌生,但即使事情不起作用,我也有很多动力。

我正在尝试使用CSV文件处理字典。我打开并编辑一个新文件,但我想在尝试从csv文件中读取时我遗漏了一些东西。 我在第39-41行遇到错误我可能做错了但是它是什么? 这是代码:

import csv
import os
import os.path

phonebook = {}
def print_menu():
    print('1. Print phone book')
    print('2. add phone book')
    print('3. look for number using first name only')
    print('4. look by last name')
    print('5. exit')
    print()

menu_selection = 0

while menu_selection != 5:

    if os.path.isfile('my_phonebook.csv') == True:  
        csv_file = open('my_phonebook.csv', 'a', newline = '')
        writer = csv.writer(csv_file)
    else:
        csv_file = open('my_phonebook.csv', 'w', newline = '')
        writer = csv.writer(csv_file)
        headers = ['first_name','last_name','phone_number']
        writer.writerow(headers)
    print_menu()
    menu_selection = int(input('type menu selection number'))

    if menu_selection == 2:  #add list in phone book
        first_name = input("enter first name: ")
        last_name = input("enter last name: ")
        phone_number = input("enter phone number: ")
        writer.writerow([first_name,last_name,phone_number])
        csv_file.close()
    elif menu_selection == 1: #print phone book
        print("phone book:")
        listGen = csv.reader(csv_file, delimiter=' ', quotechar='|') #error starts here and in the next two rows...
        for row in csv_file:
            print(row)
    elif menu_selection == 3: #look for number using first name only
        print('look up number')
        first_name = input('first name:')
        if first_name in phonebook:
            print('the number is', phonebook[phone_number])
        else:
            print('name not found')
    elif menu_selection == 4: #print all details of last name entered
        print('search by last name')
        last_name = input('please enter last name: ')
        for row in csv_file:
            print(row)

2 个答案:

答案 0 :(得分:1)

文件是否包含任何内容?看起来你正试图遍历一个空的行迭代器。尝试类似......

for row in csv_file:
    print(row)
else:
    print('no phone numbers')

看看你得到了什么。

答案 1 :(得分:0)

请检查以下代码。 还有一些其他错误,我试图解决。同样的几个变化。

为了阅读csv,我没有使用csv阅读器模块。

如果您想使用它,请替换search()函数中的代码。

config.withValue("multi",myParsedStringList)

输出:

import csv
import os
import os.path

phonebook = {}
def print_menu():
    print('1. Print phone book')
    print('2. add phone book')
    print('3. look for number using first name only')
    print('4. look by last name')
    print('5. exit')
    print()

def search(name):
    phonebook = open('my_phonebook.csv','r')
    name_found = True
    for line in phonebook:
        if name in line:
            fname,lname,num = line.split(',')
            print "First Name is ",fname.strip()
            print "Last Name is ",lname.strip()
            print 'the number is', num.strip()

            name_found = True
    if  not name_found:
        print('name not found')
    return    
menu_selection = 0

while menu_selection != 5:


    print_menu()
    menu_selection = int(input('type menu selection number - '))

    if menu_selection == 2:  #add list in phone book
        if os.path.isfile('my_phonebook.csv') == True:  
            csv_file = open('my_phonebook.csv', 'a')
            writer = csv.writer(csv_file)
        else:
            csv_file = open('my_phonebook.csv', 'w')
            writer = csv.writer(csv_file)
            headers = ['first_name','last_name','phone_number']
            writer.writerow(headers)
        first_name = input("enter first name: ")
        last_name = input("enter last name: ")
        phone_number = input("enter phone number: ")
        writer.writerow([first_name,last_name,phone_number])
        csv_file.close()

    elif menu_selection == 1: #print phone book
        print("phone book:")
        if os.path.isfile('my_phonebook.csv') == True:  
            csv_file=open('my_phonebook.csv','r')

            for row in csv_file:
                print(row)
            csv_file.close()
        else:
            print "Phone book file not created. First create it to read it"

    elif menu_selection == 3: #look for number using first name only
        print('look up number')
        first_name = input('first name:')
        search(first_name)


    elif menu_selection == 4: #print all details of last name entered
        print('search by last name')
        last_name = input('please enter last name: ')
        search(last_name)