Reading and writing dictionary

the literacy dictionary: the vocabulary of reading and writing
dictionary skills

So I thought about writing a program that takes a URL address and shortens it. I want the program to be able to write every new URL and store it in a file and generate a new short one and if the user tries to shorten the same address it'll pull out of the dictionary the result the user got last time he put this address. I set the KEY to the full URL and the VALUE to the short version. I tried to do it but when I try to read it gives me this error:

(key, val) = line.split() ValueError: too many values to unpack (expected 2)

import random
import string
import json

#Default items
dict_url = {
    'instagramcom/rois..../':  "https:/roi.sl/2Ly8PzD",
    'instagramcom/....../':  "https://roi.sl/2YqJa12"
}
#Updated_dict will be the dict i'll be using to input and output data
updated_dict = {}
updated_dict.update(dict_url)
#writing the default items from dict_url so updated_dict will have something 
with open('UrlDB.txt', 'w') as file_write:
    file_write.write(str(updated_dict))
updated_dict.update(dict_url)

#trying to read the file
with open('UrlDB.txt', 'r') as file_read:
    for line in file_read:
        (key, val) = line.split()
        updated_dict[key] = val



while True:
    new_url = ""
    exs_url = ""
    base_form = "https:/roi.sl/"
    user_input = input("Please Enter the URL: ")
    if user_input in dict_url:
        exs_url = dict_url.get(user_input)
        print(exs_url)

        break
    else:
        rnd_comb = (random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=6))
        rnd_comb_str = ''.join(rnd_comb)
        new_url = base_form + rnd_comb_str
        new_item = {
            user_input: (3, new_url)
        }
        file_write.write(str(new_item))
        dict_url.update(new_item)
        print(new_url)
        break

(key, val) = line.split() ValueError: too many values to unpack (expected 2)

line.split() splits a string on characters ignoring whitespace, and line.split(n) splits a line on string n. When assigning to this tuple, it expects two elements, but is being passed more than two after the split operation.

Additionally, the data is not being saved correctly. file_write.write(str(updated_dict)) will write it as a dictionary. If I have a dictionary with a key of a and value of b, it gets written as {'a': 'b'}. When read, the code splits it into characters ['{', 'a', ':', ' ', "'', ...] which is incorrect and is no longer a dictionary; it is an array holding the characters of a dictionary which doesn't make sense.

I would recommend using the JSON serializer before writing (to ensure that all values are encoded correctly), using json.dumps(data) then using the deserializer when reading.

The Literacy Dictionary: The Vocabulary of Reading and Writing, Amazon.com: The Literacy Dictionary: The Vocabulary of Reading and Writing ( 9780872071384): Theodore Lester Harris, Theodore Lester Harris, Richard E. Define reading. reading synonyms, reading pronunciation, reading translation, English dictionary definition of reading. 1. A borough of south-central England west of London.

Your line has too many parts. U need to use list for that, or filter input data

Text: Dictionaries, Should you stop reading to look words up? It depends. Looking up words slows you down, and you may be able to make reasonable sense of their context� Writing definition, the act of a person or thing that writes. See more.

One of your lines in the file has more than parameters, for instance, "key value extra"

I would recommend wrapping your block in try/except block and if there is an invalid line you will print it out.

For instance.

with open('UrlDB.txt', 'r') as file_read:
    try:
        for line in file_read:
            key, val = line.split()
            updated_dict[key] = val
    except ValueError:
        print("Invalid line value:\n", line)
        raise

TextHelp Read and Write Dictionary, The dictionary tool can be used anywhere that you can select or write text. It's a great tool to quickly look up words on the web, in emails or documents you have � Reading, Writing Dictionary. Learn how to use a dictionary! In this language arts worksheet, your child learns why dictionaries are helpful and how to use one.

Using a Dictionary - Reading and Study Strategies, Writers' Center Use the tabs to find guides to help you read academic texts in a way that helps you remember and use the information. Understanding when to look up a word, how an English dictionary is organized, and� writing definition: 1. a person's style of writing with a pen on paper that can be recognized as their own: 2…. Learn more.

Guest Post: Reading the Dictionary, The thought of reading a dictionary or encyclopedia strikes many as the height of absurdity, an emblem of futility. Business writer George S. Day, a professor at� Reading and Writing Aside from reviewing tips for reading and writing exams, another way to sharpen your skills in such situations is to constantly read and write. Become comfortable with the conventions of standard English, and the way that individuals phrase their words.

Reading and Writing Dictionaries, You don't need to manually read in a file to access the contents of this file as a dictionary: you can just call Bundle.main.infoDictionary . Introduction Just like with all other types of files, you can use the Pandas library to read and write Excel files using Python as well. In this short tutorial, we are going to discuss how to read and write Excel files via DataFrames. In addition to simple reading and writing, we will also learn how to write multiple DataFrames into an Excel file, how to read specific rows and columns from a

Comments
  • The error message seems clear enough. In (key, val) = line.split() -- apparently line splits into more than 2 pieces, at least sometimes.