How do i parse .txt file contents with txt and ids to a dictionary in python?

converting text file to dictionary python
load text file into dictionary python
python create dictionary from text file
python read lines from file into dictionary
text to dictionary python
python dictionary
read text file as json python
python read list of dictionaries from file

Below is a text file content with no header or quotes for the text data. How do i parse these in a dictionary.Each record in my file is on a new line Below is my text file content:

B00308CJ12 Bulletproof Salesman (2008)
189138922X Classical Mechanics
B0000CEP9J Fiesta Black 464 7-1/4-inch Salad Plate
B000HRH6IA Baby Blue Aurora Blue Gem Butterfly Belly Ring
B000002ERY Predicciones Leo

Using the code below is giving me an error. I am assuming that's because my sentences are not in quotes. What do i do to rectify and put these values in a dict

 f = open('file_path', 'r')
    answer = {}
    for line in f:
        k, v = line.strip().split(' ')
        answer[k.strip()] = v.strip()

    f.close() 

ERROR:

k, v = line.strip().split(' ')

ValueError: too many values to unpack (expected 2)

Edit after changes to code

 f = open('file_path', encoding="utf8")
    answer = {}
    for line in f:
        k, v = line.strip().split(' ')[0],line.strip().split(' ')[1:]
        answer[k] = v

    f.close()

Error :

File "C:\ProgramData\Anaconda3\lib\codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 1266: invalid start byte

If you want {'B00308CJ12':'Bulletproof Salesman (2008)',...}

Try this:

di={}    
with open(fn) as f_in:
    for line in f_in:
        k,v=line.split(sep=None,maxsplit=1)
        di[k]=v.rstrip()

>>> di
{'B00308CJ12': 'Bulletproof Salesman (2008)', 
 '189138922X': 'Classical Mechanics', 
 'B0000CEP9J': 'Fiesta Black 464 7-1/4-inch Salad Plate', 
 'B000HRH6IA': 'Baby Blue Aurora Blue Gem Butterfly Belly Ring', 
 'B000002ERY': 'Predicciones Leo'}

Python: convert a text file into a dictionary, Python: convert a text file into a dictionary commands.txt processsor ADDISK (Operator command) ADD_REMOTE_ID Sets up user id for approach we're passing the full file contents as a single string with the fh.read()  Assuming a following text file (dict.txt) is present. 1 aaa 2 bbb 3 ccc. Following Python code reads the file using open() function. Each line as string is split at space character.

Replace

k, v = line.strip().split(' ')

with

k, v = line.strip().split(' ')[0],line.strip().split(' ')[1:]

Your sentences have spaces in them, so you are getting more values than you are expecting

6. Dictionaries, sets, files, and modules, Another way to create a dictionary is to provide a list of key-value pairs using Web pages are stored in text files, which are essentially files containing a string of text. The easiest and most powerful way to format a string in Python 3 is to use the which is a string, to a variable named contents , and finally print contents to  A Python program can read a text file using the built-in open() function. For example, below is a Python 3 program that opens lorem.txt for reading in text mode, reads the contents into a string variable named contents, closes the file, and then prints the data.

The line line.strip().split(' ') actually splits each line of your text file into a list with the space character ' ' as delimiter producing an output like this one for the first line ['B00308CJ12', 'Bulletproof', 'Salesman', '(2008)']. At this point if you want to create a dictionary, you need to decide what you want as key and value in your dictionary and get those values from the list using their corresponding indexes

Update about encoding

try reading the file with this line

f = open('file_path', encoding="utf8")

And to have 'Bulletproof Salesman (2008)' as value you can use the join function

k, v = line.strip().split(' ')[0], " ".join(line.strip().split(' ')[1:])

How to create a Python dictionary from text file?, Assuming a following text file (dict.txt) is present1 aaa2 bbb3 cccFollowing Python code reads the file using open() function. Each line as string is split at s The output shows contents of file in dictionary form {1: 'aaa', 2: 'bbb',  and so on (Its just an example). I want to put that into a dictionary. If so how do I do it. Tell us what your ideas are. Do you know how to read files? Here's another thing to think about and to go investigate how to do: how do you separate the key from the value in the lines in your file? Ok well I have a good understanding of python.

All answers put together the below code worked like a charm for the intended ie {'B00308CJ12': 'Bulletproof Salesman (2008)','189138922X': 'Classical Mechanics', ......} Thanks all!!

answer = {}
with open('filepath','rb') as f:
    for line in f:
        k, v = line.strip().split(sep=None,maxsplit=1)[0],line.strip().split(sep=None,maxsplit=1)[1:]
        answer[k] = v
f.close()

Using dictionaries to store data as key-value pairs, The dictionary stores objects as key-value pairs and can be used to represent access any member of the collection using a key – which can be a human-​readable string. will return a text file (in JSON format) that can be turned into a Python dictionary: Note that the actual contents are the same, whether they're inside a  Python provides inbuilt functions for creating, writing and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language,0s and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new

How to Parse Lines in a Text File and Index as Elasticsearch , To legally parse a line-by-line text file index for Elasticsearch documents with Python bulk, the user must have the rights to use to the content being inserting into a How to parse the line's string to look for a new dictionary entry of it with an _id key so Elasticsearch will know what document ID to assign:  When you’re working with Python, you don’t need to import a library in order to read and write files. It’s handled natively in the language, albeit in a unique manner. The first thing you’ll need to do is use Python’s built-in open function to get a file object. The open function opens a file. It’s simple.

Convert Text file to JSON in Python, To convert a text file into JSON, there is a json module in Python. dict, object. list, tuple, array. str, string. int, long, float, numbers. True, true Here the idea is to store the contents of the text as key-value pairs in the dictionary and then For each intermediate dictionary a unique id is created and that serves as the key. This tutorial will focus on the txt file format. Step 1 — Creating a Text File. Before we can begin working in Python, we need to make sure we have a file to work with. To do this, we’ll open up a text editor and create a new txt file, let’s call it days.txt. In the new file, enter a few lines of text.

Parsing Text Files in Python, The csv module helps you to elegantly process data stored within a CSV file. id​,name,age,height,weight 1,Alice,20,62,120.6 2,Freddie,21,74,190.6 3,Bob,17,​68,120.0. Your Python code must import the csv library. import csv When you iterate over a CSV file, each iteration of the loop produces a dictionary from strings to  file is a builtin (in Python 2.x only), consider a different variable name define your regular expression strings as raw strings you can probably replace .* wildcard with a more concrete \d+ for the "grade" regex: Grade = (\d+)

Comments
  • Are you intending to get {'B00308CJ12':'Bulletproof Salesman (2008)',...}? ie, each key on the left is then the unbroken string on the right?
  • Yes but my code is giving me something like this {'B00308CJ12':['Bulletproof', 'Salesman', '(2008)']}... That seems to be the issue
  • Yeah i did and its throwing a error after half of my file was parsed as a dict >>> File "C:\ProgramData\Anaconda3\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 4062: character maps to <undefined>
  • What are you using as key? Can you update your original question with the new version of your code. Otherwise it will be hard to help. But this looks to me like a character encoding issue
  • i am taking the id as key and the sentence as value. i have updated the original question with the changes made and the error
  • as I expected it is a character encoding error. try reading the file specifying an encoding method like utf-8. See my updated answer
  • That throws a succeeding error - updated question again
  • Note: 1) f.close() is not necessary when you use with to open the file. 2) You are performing a second split unnecessarily with k, v = line.strip().split(sep=None,maxsplit=1)[0],line.strip().split(sep=None,maxsplit=1)[1:] You can just use tuple unpacking with k,v=line.strip().split(maxsplit=1) and k and v will get the proper values automatically.
  • not doing the second split is giving me an error >>k,v=line.strip().split(maxsplit=1) ValueError: not enough values to unpack (expected 2, got 1)