having trouble converting dictionary elements (string) into int - python

Having a little trouble converting the two elements that are in a tuple inside of a dictionary into int values. the keys of the dictionary are country name and the tuple of info is (the area, the population). This is what i have so far :
def _demo_fileopenbox():
msg = "Pick A File!"
msg2 = "Select a country to learn more about!"
title = "Open files"
default="*.py"
f = fileopenbox(msg,title,default=default)
writeln("You chose to open file: %s" % f)
countries = {}
with open(f,'r') as handle:
reader = csv.reader(handle, delimiter = '\t')
for row in reader:
countries[row[0]] = (row[1].replace(',', ''), row[2].replace(',', ''))
for i in countries:
int((countries[i])[0])
int((countries[i])[1])
#while 1:
# reply = choicebox(msg=msg2, choices= list(countries.keys()) )
# writeln(reply + "-\tArea: " + (countries[reply])[0] + "\tPopulation: " + (countries[reply])[1] )
but i keep getting this error :
int((countries[i])[0])
ValueError: invalid literal for int() with base 10: ''
any ideas how to fix this or a better way to do this:

Related

Appending to dictionary in text file in Python inside the brackers {}

I am trying to append a message along with a name to an empty dictionary in a text file.
def tweeting():
f = open('bericht.txt', 'a')
message = input('Put in message: ')
name = input('Put in your name: ')
if name == '':
name = 'Anonymous'
tweet = name + '; ' + message
f.write(tweet)
f.close()
tweeting()
The result I am getting is this:
Tweets = {
}David; Hello everyone
The message goes after the brackets {}. Is there a way to put the message inside the brackets {} ?
Thanks for the help.
Try the following. Just take care of the quotes in name and message, if you want the text in your file to be as a dictionary. If they are not typed by user, they must be added to t before t is written in the file:
def tweeting():
with open('bericht.txt') as f:
t=f.read()
if ':' in t: #that means that t has other elements already
t=t[:-1]+','
else:
t=t[:-1]
message = input('Put in message: ')
name = input('Put in your name: ')
if name == '':
name = 'Anonymous'
t += name + '; ' + message + '}'
with open('bericht.txt', 'w') as f:
f.write(t)
you have not entered any brackets as such. Just add brackets as follows -
def tweeting():
f = open('bericht.txt', 'a')
message = input('Put in message: ')
name = input('Put in your name: ')
if name == '':
name = 'Anonymous'
tweet = '{' + name + '; ' + message + '}'
f.write(tweet)
f.close()
tweeting()
Add '\n' if you want it in different lines.

can't concat string to bytes

I'm trying grab twitter user data by their screen name using python.
The entire script does is to loop over each of the Twitter accounts in the ids variable — and for each one it will grab its profile information and add that to a row of the output file.
but I'm getting an error.
This is my code
// LIST OF TWITTER USER IDS
ids = "4816,9715012,13023422, 13393052, 14226882, 14235041, 14292458, 14335586, 14730894,\
15029174, 15474846, 15634728, 15689319, 15782399, 15946841, 16116519, 16148677, 16223542,\
16315120, 16566133, 16686673, 16801671, 41900627, 42645839, 42731742, 44157002, 44988185,\
48073289, 48827616, 49702654, 50310311, 50361094,"
// THE VARIABLE USERS IS A JSON FILE WITH DATA ON THE 32 TWITTER USERS LISTED ABOVE
users = t.lookup_user(user_id = ids)
//NAME OUR OUTPUT FILE - %i WILL BE REPLACED BY CURRENT MONTH, DAY, AND YEAR
outfn = "twitter_user_data_%i.%i.%i.txt" % (now.month, now.day, now.year)
// NAMES FOR HEADER ROW IN OUTPUT FILE
fields = "id screen_name name created_at url followers_count friends_count statuses_count \
favourites_count listed_count \
contributors_enabled description protected location lang expanded_url".split()
// INITIALIZE OUTPUT FILE AND WRITE HEADER ROW
outfp = open(outfn, "w")
//outfp.write(string.join(fields, "\t") + "\n") # header
outfp.write("\t".join(fields) + "\n") # header
// THIS BLOCK WILL LOOP OVER EACH OF THESE IDS, CREATE VARIABLES, AND OUTPUT TO FILE
for entry in users:
// CREATE EMPTY DICTIONARY
r = {}
for f in fields:
r[f] = ""
// ASSIGN VALUE OF 'ID' FIELD IN JSON TO 'ID' FIELD IN OUR DICTIONARY
r['id'] = entry['id']
// SAME WITH 'SCREEN_NAME' HERE, AND FOR REST OF THE VARIABLES
r['screen_name'] = entry['screen_name']
r['name'] = entry['name']
r['created_at'] = entry['created_at']
r['url'] = entry['url']
r['followers_count'] = entry['followers_count']
r['friends_count'] = entry['friends_count']
r['statuses_count'] = entry['statuses_count']
r['favourites_count'] = entry['favourites_count']
r['listed_count'] = entry['listed_count']
r['contributors_enabled'] = entry['contributors_enabled']
r['description'] = entry['description']
r['protected'] = entry['protected']
r['location'] = entry['location']
r['lang'] = entry['lang']
// NOT EVERY ID WILL HAVE A 'URL' KEY, SO CHECK FOR ITS EXISTENCE WITH IF CLAUSE
if 'url' in entry['entities']:
r['expanded_url'] = entry['entities']['url']['urls'][0]['expanded_url']
else:
r['expanded_url'] = ''
print(r)
// CREATE EMPTY LIST
lst = []
// ADD DATA FOR EACH VARIABLE
for f in fields:
lst.append(str(r[f]).replace("\/", "/"))
// WRITE ROW WITH DATA IN LIST
//outfp.write(string.join(lst, "\t").encode("utf-8") + "\n")
outfp.write("\t".join(lst).encode('utf-8') + '\n')
outfp.close()
The error message
TypeError Traceback (most recent call last)
<ipython-input-54-441137b1bb4d> in <module>()
37 #WRITE ROW WITH DATA IN LIST
38 #outfp.write(string.join(lst, "\t").encode("utf-8") + "\n")
---> 39 outfp.write("\t".join(lst).encode('utf-8') + '\n')
40
41 outfp.close()
TypeError: can't concat str to bytes
Any idea on how to fix this? The version of Python is 3.6.5
Your help will be greatly appreciated. Thanks.
Edite:
This screenshot of part of my file after I opened the output file in the binary mode
outfp.write("\t".join(lst).encode('utf-8') + '\n')
After you do .encode() on a string you get an instance of bytes. You can't add another string (like \n) to bytes. That's what the error is telling you.
So you need to add the \n before you encode the string. Like below:
outfp.write(("\t".join(lst) + '\n').encode('utf-8'))

How to fix errors IndexError: list index out of range

I would like load data which are 10 categories of document, each cateory contains text files, but I keep getting the following error:
IndexError: list index out of range
THis is code :
def load_data(folder):
data = []
files = [join(folder, x) for x in os.listdir(folder)]
for file in files:
topic = file.split("/")[9] # this is where the error occurs
label = topic.replace(" ", "_")
name = "__label__" + label
with open(file, "rb") as f:
content = f.read()
content = content.decode('utf-16')
content = " ".join(i for i in content.split())
data.append(name + " " + content)
return data
Easy way to debug this would be to add print statements and check what the objects hold. For e.g. in this case, you can add 2 print statements at the beginning of the for loop. This would help you to figure out why you are getting IndexError
def load_data(folder):
data = []
files = [join(folder, x) for x in os.listdir(folder)]
for file in files:
print(file)
print(file.split("/"))
topic = file.split("/")[9] # this is where the error occurs
label = topic.replace(" ", "_")
name = "__label__" + label
with open(file, "rb") as f:
content = f.read()
content = content.decode('utf-16')
content = " ".join(i for i in content.split())
data.append(name + " " + content)
return data

Searching Python Dictionary by key value is returning multiple, consecutive results, not just 1

I am relatively new to Python and I am having trouble with the following.
I am trying to write a program that would read a CSV file of personal information and then display an individuals information based on entering an ID number.
It is almost working perfectly, except that when I search by id number, it is returning all of the results (rows) preceding the desired result, in addition to the result I want.
I am reading a CSV file into a dictionary. I am then naming the fields dynamically from the file based on the names from the CSV (theoretically the CSV file can contain 2 columns of data or 100, as long as there is one field named "id").
csvfile.txt looks like:
id,name,age
1,jay,35
2,jen,36
3,sam,38
4,mike,26
What I want is when I search for id "1", it returns:
"
id: 1
name: Jay
age: 35
"
...and it does.... but if I search for id "3", I am getting:
"
id: 1
name: Jay
age: 35
id: 2
name: Jen
age: 36
id: 3
name: Sam
age: 38
"
I can't figure out why it is not just returning the one row I am asking for... here is the core of the code:
def runprogram():
import csv
file = open(csvfile.txt, "r") #open my test file
reader = csv.DictReader(file, skipinitialspace=True, dialect='excel', delimiter=',')
totalfields = (len(reader.fieldnames)) #count all the fields in the files for the purpose of for looping.
result={} #new dictionary named result
resultfields = ""
i=1
for row in reader:
for i in range(1,totalfields):
resultfields = resultfields + reader.fieldnames[i] + ": " + row[reader.fieldnames[i]] + "\n"
i+1
result[row['id']] = resultfields #storing each line from the CSV into the results dictionary under the key "id"
#this was just some code so I could exit my program by typing "exit" in the input box...
idvalue=""
while idvalue != "exit":
#display the box in which to enter a id number
if idvalue =="":
message = "Enter id Number"
else:
message = result[idvalue]
#using easyGUI for my input and display boxes.
idvalue = eg.enterbox(msg=message,title='Print Results', default='', strip=True)
if idvalue:
identered = "1"#this was used for testing.
printresults(results[idvalue]) #call printresults function
else:
programmenu()
if idvalue =="exit":
exit()
def printresults(results):
output = "Information Requested:\n" + results
print(output)
Any help would be greatly appreciated!
You need to re-initialize resultfields for each row you process.
#!/usr/bin/python
import csv
def printresults(results):
print ("Information Requested:\n" + results)
file = open("/tmp/csvfile.txt", "r")
reader = csv.DictReader(file, skipinitialspace=True, dialect='excel', delimiter=',')
totalfields = (len(reader.fieldnames))
result={}
for row in reader:
resultfields = ""
for i in range(1,totalfields):
resultfields = resultfields + reader.fieldnames[i] + ": " + row[reader.fieldnames[i]] + "\n"
result[row['id']] = resultfields
idvalues = ["exit", "4"]
while 1:
idvalue = idvalues.pop() #eg.enterbox(msg=message,title='Print Results', default='', strip=True)
if idvalue == "":
message = "Enter id Number"
elif idvalue =="exit":
print "done"
exit()
else:
message = result[idvalue]
print(message)
Output now looks like:
name: mike
age: 26
done

change input into int while its being placed into dictionary

Is there any way to while reading into this dictionary to convert the values of each key value to int? Originally they are strings but i would prefer them to be ints. This is what i tried but i am getting errors! Each key looks like {'USA': ('123,123', '312,321,321')} But i want those numbers to be ints
**def _demo_fileopenbox():
msg = "Pick A File!"
msg2 = "Select a country to learn more about!"
title = "Open files"
default="*.py"
f = fileopenbox(msg,title,default=default)
writeln("You chose to open file: %s" % f)
countries = {}
with open(f,'r') as handle:
reader = csv.reader(handle, delimiter = '\t')
for row in reader:
countries[row[0]] = ((int(row[1])),(int(row[2])))
while 1:
reply = choicebox(msg=msg2, choices= list(countries.keys()) )
writeln(reply + ";\tArea: " + (countries[reply])[0] + "\tPopulation: " + (countries[reply])[1] )
**
thanks!
Try removing the commas from the strings before converting them to ints:
countries[row[0]] = (int(row[1].replace(',', '')), int(row[2].replace(',', '')))
Your problem is that your numbers contain commas. Change the code to this:
for row in reader:
countries[row[0]] = tuple(int(a.replace(",","")) for a in row[1:])

Categories

Resources