Issue with reading a text file to a dictionary in python - python

Hey everyone just have an issue with a text file and putting it into a dictionary.
So my code first starts off by gathering data from a website and writes it to a text file. From there I reopen the file and make it into a dictionary to transfer the data from the text to the dictionary. In the while loop, I am getting the error of
key,value = line.split()
ValueError: too many values to unpack (expected 2)
Which I'm not sure why if I'm using the wrong method to write the text file data to the new place in the program of "countryName"
Then once that compiles I want to be able to ask the user to input a country name and it will give the income capita of that country as shown in the print line.
def main():
import requests
webFile = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2004.txt"
data = requests.get(webFile) #connects to the file and gest a response object
with open("capital.txt",'wb') as f:
f.write(data.content) #write the data out to a file – wb used since thecontent from the response object is returned as abinary object.
f.close()
infile = open('capital.txt', 'r')
line = infile.readline()
countryName = {}
while line != "":
key,value = line.split()
countryName[key] = value
line = infile.readline()
infile.close()
userInput = input("Enter a country name: ")
for i in countryName:
while(userInput != 'stop'):
print("The per capita income in",countryName[key], "is",countryName[value])
userInput = input("Enter a country name: ")
main()

each line also has a number in the beginning of it, and some country names have spaces in them, causing split to return longer lists. If you use regex to add in semicolons as delimiters, and trim leading and trailing whitespace, the splitting works properly. This code would go inside the first while loop
line = re.sub(r"(\$)", r";\1", line) # add semicolon before $ sign
line = re.sub(r'^([0-9]+)',r'\1;', line) # add semicolon after first group of numbers
num, key, value = re.split(r';', line) # split with semicolons as delimiters
countryName[key.strip()] = value.strip() # assign key and values after stripping whitespace

Split returns list, not dictionary.
a = 'a b c'
list = a.split() #-> ['a','b','c']
Are you trying to do something like:
import requests
webFile = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2004.txt"
data = requests.get(webFile).text #connects to the file and gest a response object
print(data)
while(1):
name = input('Enter a country name: ')
for a in data.splitlines():
if name.lower() in a.lower():
print(a.split()[-1])

Related

Read data from txt file in dictionary format

how do I convert a data read from a txt file in dictionary format.
Lines from txt file:
A1, 5, Apples
B1, 3, Oranges
Desired output:
{'A1':[5.0,'Apples'], 'B1':[3.0,'Oranges']}
only managed to code these:
fr = open('products.txt','r')
for line in fr.readlines():
code, name, price = line.split(',')
print(line)
fr.close()
You can use comprehension:
result = {line.strip().split(',')[0]: [line.strip().split(',')[1], line.strip().split(',')[2]] for line in open('products.txt','r')}
Or without comprehension:
result = {}
with open('products.txt','r') as fr:
for line in fr:
parts = line.strip().split(',')
result[parts[0]] = [parts[1], parts[2]]
print(result)
my_dict = {}
fr = open('products.txt','r')
for line in fr.readlines():
code, name, price = line.split(',')
my_dict[code] = [name,price]
print(line)
fr.close()
You can create an empty dictionary and update the dictionary for each lines, also strip off the value for price to get rid of residue space after splitting on comma:
data = {}
fr = open('products.txt','r')
for line in fr.readlines():
code, price, name = line.split(',') # price is the middle item, name is last
print(line)
data.update({code:[float(price.strip()), name.strip()]})
fr.close() #Keep this outside the loop
OUTPUT:
{'A1': [5.0, 'Apples'], 'B1': [3.0, 'Oranges']}
You're close, but you've got a few errors and a few missing things...
out_d = {} # init an empty dict
fr = open('products.txt','r')
for line in fr.readlines():
code, price, name = line.split(', ') # not code, name, price
print(line)
out_d[code] = [float(price), name] # adds a new dict entry
fr.close() # gets dedented
print(out_d)
A few things...
(1) according to your input file, you want to split on ', ', not just ','
(2) it looks like the order of your input file is code/price/name not code/name/price
(3) you need to cast the price from string to float
(4) your fr.close() is indented too far. It needs to be dedented.
Also, it's fine to do an open() and close() when you're first starting out. But there is a better way to go, called a context manager, which will automatically do the close() for you. Basically, it would look like this:
out_d = {} # init an empty dict
with open('products.txt','r') as fr: # this creates a context
for line in fr.readlines():
code, price, name = line.split(', ') # not code, name, price
print(line)
out_d[code] = [float(price), name]
# fr.close() -- this line now goes away
print(out_d)
Above, the fr.close() line goes away, because the with open calls close when the with block finishes.

How to Read Numeric Data From Text file and save it to a variable using python

I am working on a project that gets data from a text file and that value needs to be stored in a variable. but the following code does not work properly. sometimes it works while other times it returns
ValueError: invalid literal for int() with base 10: ''
the following is the code used:
def main():
# Txt read
global id
input = open('data.txt', 'r')
lines = input.readlines()
i = 0
for line in lines:
i += 1
id = int(line)
main()
print id
Data would be in single int followed by new line in text file.
Any Help would be appreciated.
Few things first
Don't use input as a variable, since it's a built-in function in python. It is not considered a good practice.
Also, id also happens to be a built-in function, so avoid that as well
Also, I would suggest to read the whole file as string and the split based on \n. This will help you to strip the extra newlines at end (and start if you wish)
You can use something like this:
def main():
# Txt read
input1 = open('text.txt', 'r').read().strip()
l = input1.split("\n")
#convert to int
ll = [int(s) for s in l]
print(ll)
main()
In your code you will get only last value in file for getting all values use list and store them in list and there is no need of i and increment of i if want to calculate total number of values use len(id)
Try Below code
def main():
# Txt read
global id
id=[]
input = open('data.txt', 'r')
lines = input.readlines()
for line in lines:
if line.strip(): #Checking Non-Empty Line
id.append(int(line.strip()))
main()
print id
print "Total Valuse: "+str(len(id))
The newline character "\n" is casing the error.
def main():
# Txt read
global id
data = open('data.txt', 'r').read()
data = data+'0'
data = data.replace('\n','+0+')
id = eval(data)
main()
print(id)

How to fetch these below values in python

I have a file in the below format
.aaa b/b
.ddd e/e
.fff h/h
.lop m/n
I'm trying to read this file. My desired output is if I find ".aaa" I should get b/b, if I find ".ddd" I should get e/e and so on.
I know how to fetch 1st column and 2nd column but I don't know how to compare them and fetch the value. This is what I've written.
file = open('some_file.txt')
for line in file:
fields = line.strip().split()
print (fields[0]) #This will give 1st column
print (fields[1]) # This will give 2nd column
This is not the right way of doing things. What approach follow?
Any time you want to do lookups, a dictionary is going to be your friend.
You could write a function to load the data into a dictionary:
def load_data(filename):
result = dict()
with open(filename, 'r') as f:
for line in f:
k,v = line.strip().split() # will fail if not exactly 2 fields
result[k] = v
return result
And then use it to perform your lookups like this:
data = load_data('foo.txt')
print data['.aaa']
It sounds like what you may want is to build a dictionary mapping column 1 to column 2. You could try:
file = open('some_file.txt')
field_dict = {}
for line in file:
fields = line.strip().split()
field_dict[fields[0]] = fields[1]
Then in your other code, when you see '.ddd' you can simply get the reference from the dictionary (e.g. field_dict['.ddd'] should return 'e/e')
Just do splitting on each line according to the spaces and check whether the first item matches the word you gave. If so then do printing the second item from the list.
word = input("Enter the word to search : ")
with open(file) as f:
for line in f:
m = line.strip().split()
if m[0] == word:
print m[1]

Printing to a file via Python

Hopefully this is an easy fix. I'm trying to edit one field of a file we use for import, however when I run the following code it leaves the file blank and 0kb. Could anyone advise what I'm doing wrong?
import re #import regex so we can use the commands
name = raw_input("Enter filename:") #prompt for file name, press enter to just open test.nhi
if len(name) < 1 : name = "test.nhi"
count = 0
fhand = open(name, 'w+')
for line in fhand:
words = line.split(',') #obtain individual words by using split
words[34] = re.sub(r'\D', "", words[34]) #remove non-numeric chars from string using regex
if len(words[34]) < 1 : continue # If the 34th field is blank go to the next line
elif len(words[34]) == 2 : "{0:0>3}".format([words[34]]) #Add leading zeroes depending on the length of the field
elif len(words[34]) == 3 : "{0:0>2}".format([words[34]])
elif len(words[34]) == 4 : "{0:0>1}".format([words[34]])
fhand.write(words) #write the line
fhand.close() # Close the file after the loop ends
I have taken below text in 'a.txt' as input and modified your code. Please check if it's work for you.
#Intial Content of a.txt
This,program,is,Java,program
This,program,is,12Python,programs
Modified code as follow:
import re
#Reading from file and updating values
fhand = open('a.txt', 'r')
tmp_list=[]
for line in fhand:
#Split line using ','
words = line.split(',')
#Remove non-numeric chars from 34th string using regex
words[3] = re.sub(r'\D', "", words[3])
#Update the 3rd string
# If the 3rd field is blank go to the next line
if len(words[3]) < 1 :
#Removed continue it from here we need to reconstruct the original line and write it to file
print "Field empty.Continue..."
elif len(words[3]) >= 1 and len(words[3]) < 5 :
#format won't add leading zeros. zfill(5) will add required number of leading zeros depending on the length of word[3].
words[3]=words[3].zfill(5)
#After updating 3rd value in words list, again creating a line out of it.
tmp_str = ",".join(words)
tmp_list.append(tmp_str)
fhand.close()
#Writing to same file
whand = open("a.txt",'w')
for val in tmp_list:
whand.write(val)
whand.close()
File content after running code
This,program,is,,program
This,program,is,00012,programs
The file mode 'w+' Truncates your file to 0 bytes, so you'll only be able to read lines that you've written.
Look at Confused by python file mode "w+" for more information.
An idea would be to read the whole file first, close it, and re-open it to write files in it.
Not sure which OS you're on but I think reading and writing to the same file has undefined behaviour.
I guess internally the file object holds the position (try fhand.tell() to see where it is). You could probably adjust it back and forth as you went using fhand.seek(last_read_position) but really that's asking for trouble.
Also, I'm not sure how the script would ever end as it would end up reading the stuff it had just written (in a sort of infinite loop).
Best bet is to read the entire file first:
with open(name, 'r') as f:
lines = f.read().splitlines()
with open(name, 'w') as f:
for l in lines:
# ....
f.write(something)
For 'Printing to a file via Python' you can use:
ifile = open("test.txt","r")
print("Some text...", file = ifile)

How to read file line by line in file?

I'm trying to learn python and I'm doing a problem out of a book but I'm stuck on one question. It asks me to read a file and each line contains an 'a' or a 's' and basically I have a total which is 500. If the line contains an 'a' it would add the amount next to it for example it would say "a 20" and it would add 20 to my total and for s it would subtract that amount. In the end I'm supposed to return the total after it made all the changes. So far I got
def NumFile(file:
infile = open(file,'r')
content = infile.readlines()
infile.close()
add = ('a','A')
subtract = ('s','S')
after that I'm completely lost at how to start this
You need to iterate over the lines of the file. Here is a skeleton implementation:
# ...
with open(filename) as f:
for line in f:
tok = line.split()
op = tok[0]
qty = int(tok[1])
# ...
# ...
This places every operation and quantity into op and qty respectively.
I leave it to you to fill in the blanks (# ...).
A variation might be
f = open('myfile.txt','r')
lines = f.readlines()
for i in lines:
i = i.strip() # removes new line characters
i = i.split() # splits a string by spaces and stores as a list
key = i[0] # an 'a' or an 's'
val = int( i[1] ) # an integer, which you can now add to some other variable
Try adding print statements to see whats going on. The cool thing about python is you can stack multiple commands in a single line. Here is an equivalent code
for i in open('myfile.txt','r').readlines():
i = i.strip().split()
key = i[0]
val = int (i[1])

Categories

Resources