Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有csv文件的python字典_Python_Csv_Dictionary - Fatal编程技术网

带有csv文件的python字典

带有csv文件的python字典,python,csv,dictionary,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')

我对所有这些编码都是新手,但即使事情不顺利,我也有很多动力

我正在尝试使用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)

这个文件有什么内容吗?看起来您正试图在一个空的行迭代器上循环。试试像

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

查看您得到的信息。

请检查以下代码。 还有一些其他的错误,我也试图修复。做了两个相同的更改

对于读取csv,我没有使用csv阅读器模块

如果要使用该函数,请替换search()函数中的代码

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)
输出:

C:\Users\dinesh_pundkar\Desktop>python b.py
1. Print phone book
2. add phone book
3. look for number using first name only
4. look by last name
5. exit
()
type menu selection number - 1
phone book:
Phone book file not created. First create it to read it
1. Print phone book
2. add phone book
3. look for number using first name only
4. look by last name
5. exit
()
type menu selection number - 2
enter first name: "Dinesh"
enter last name: "Pundkar"
enter phone number: "12345"
1. Print phone book
2. add phone book
3. look for number using first name only
4. look by last name
5. exit
()
type menu selection number - 1
phone book:
first_name,last_name,phone_number

Dinesh,Pundkar,12345

1. Print phone book
2. add phone book
3. look for number using first name only
4. look by last name
5. exit
()
type menu selection number - 3
look up number
first name:"Dinesh"
First Name is  Dinesh
Last Name is  Pundkar
the number is 12345
1. Print phone book
2. add phone book
3. look for number using first name only
4. look by last name
5. exit
()
type menu selection number - 4
search by last name
please enter last name: "Pundkar"
First Name is  Dinesh
Last Name is  Pundkar
the number is 12345
1. Print phone book
2. add phone book
3. look for number using first name only
4. look by last name
5. exit
()
type menu selection number - 5

C:\Users\dinesh_pundkar\Desktop>

你能编辑你的问题并添加你得到的错误吗?在python中,这些消息包含大量信息,只需稍加训练,您就可以通过阅读它们轻松地更正大部分错误。下面是python的文档示例:谢谢您的快速回答。。为csv_文件中的行添加代码时出错:io。不支持操作:不可读