2

So the file text, which I'm supposed to transfer to a dictionary for a phonebook, looks like this:

Name1 Name2 Numbers

Name3 Name4 Numbers2

and so on..

What I've tried:

def read():

    file1=open("file.txt","r")

    dict={}

    for line in file1:

        line=line.split()

        if not line:continue

        dict[line[0]]=line[1:]

    print(dict)

When I run it, it prints nothing.

Thank you!

SPYBUG96
  • 1,089
  • 5
  • 20
  • 38
Jaakkath
  • 65
  • 1
  • 8
  • For me your program works totally fine.... However, I would strongly recommend not to use `dict` as a variable name! – mrCarnivore Nov 28 '17 at 16:27
  • 1
    are you calling read() somewhere? also, don't call this thing read :) – sniperd Nov 28 '17 at 16:29
  • I have different variable names, just changed them for this and this is just a subprogram, so it is called later on. Okay, apparently I needed to open the file again when I'm doing something new with it in the same subprogram. Works now. – Jaakkath Nov 28 '17 at 16:35

4 Answers4

1

this is my way

def read_dict():
    file1 = open("file.txt", 'r')
    dict={}  

    # read lines of all
    lines = file1.readlines()

    # Process one line at a time.
    for line in lines:
        line = line.split()
        if not line: continue
        dict[line[0]] = line[1:]

    file1.close()
    print(dict)

read_dict()

or (use with) you don't have to close file

def read_dict():
    with open("file.txt", 'r') as file1:
        dict={}  
        # read lines of all
        lines = file1.readlines()
        # Process one line at a time.
        for line in lines:
            line = line.split()
            if not line: continue
            dict[line[0]] = line[1:]
        print(dict)
chandong83
  • 81
  • 8
0

Many comments to make here.

1 - You forgot to add ".read()" when you open your file.

2 - You re using reserved words for the python language. "dict" is something used by the language, so avoid using it directly. Instead name them more specifically. Avoid at all cost naming your variables with words already used by Python language.

3 - Your function does not return anything. At the end of each function, you need to specify "return" plus the object you want the function to return value.

def read_dict():
    file1 = open("file.txt","r").read()
    my_dict = {}
    for line in file1:
        line = line.split()
        if not line:
            continue
        my_dict[line[0]] = line[1:]
    return my_dict

print(read_dict())
Philippe Oger
  • 1,021
  • 9
  • 21
0

Make sure you call the function. I've changed a little of it around so it's not using words like 'read' or 'dict'. This works:

def main():
    thefile = open("file.txt","r")
    thedict={}
    for theline in thefile:
        thelist = theline.split(" ")
        if not thelist:
            continue
        thedict[thelist[0]]=thelist[1:]

    print(thedict)

main()

results in:

{'Name1': ['Name2', 'Numbers\n'], 'Name3': ['Name4', 'Numbers2']}
sniperd
  • 5,124
  • 6
  • 28
  • 44
0

You have included your implementation inside a function read(). You need to call the function somewhere.

def read():
  file1=open("file.txt","r")

  dict={}

  for line in file1:

    line=line.split()

    if not line:continue

    dict[line[0]]=line[1:]

  print(dict)

read()

Try this.

Ajay Maity
  • 740
  • 2
  • 8
  • 17