The goal here is to write a function called displayPerson that takes in an integer called id as its first parameter, and a dictionary as its second parameter, called personData.
The purpose of the function is to print the name and birthday of a given user identified by the input id. If there is no entry with the given id, then print “No user found with that id” instead.
The format should be “Person # id is name with a birthday of
date”, where id is the id # inputted, and name is the name of the person (from the file) and date is the birthday of the user (formatted as YYYY-MM-DD.
This is what I have so far
import argparse
import urllib.request
import datetime
import logging
#url https://s3.amazonaws.com/cuny-is211-spring2015/birthdays100.csv
#url = input('Insert URL here: ')
url = "https://s3.amazonaws.com/cuny-is211-spring2015/birthdays100.csv"
def downloadData(url):
response = urllib.request.urlopen(url)
data = response.read().decode('utf-8')
#print(data)
return data
def processData(file_content):
dictionary = {}
#print(file_content)
# [
# "1,Charles Paige,06/01/1963",
# "2,Andrew Bell,29/03/1972",
# ...
# "99,Alan Wilson,03/04/1960",
# "100,Austin Burgess,04/06/1979"
# ]
count = 0
data_items = file_content.splitlines()
logging.basicConfig(filename='error.log', filemode='w', level=logging.ERROR)
for line in data_items[1:]:
data_pieces = line
data_pieces = data_pieces.split(',')
# ["1", "Charles Paige", "06/01/1963"]
count = count + 1
#print(data_pieces)
# dictionary[data_pieces[0]] = (data_pieces[1]), datetime.datetime.strptime((data_pieces[2]), '%d/%m/%Y')
try:
dictionary[data_pieces[0]] = (data_pieces[1]), datetime.datetime.strptime((data_pieces[2]), '%d/%m/%Y')
except ValueError:
logging.error("Error processing line #: " + str(count) + " for ID #: " + str(data_pieces[0]))
return dictionary
def displayPerson(id, personData):
#print(personData)
#return
try:
id = input("ID:")
print("Person #" + id + "is" + dictionary[data_pieces[1]] + "with a birthday of" + datetime.datetime.strptime((data_pieces[2]), '%Y-%m-%d'))
except:
print("No user ID found")
def main():
downloadData(url)
file_content = downloadData(url)
values = processData(file_content)
#print(values)
displayPerson(id, values)
When I input an ID number, it raises the except every time. I'm not sure how to format the code to correspond the ID number with the values from the dictionary I created in processData.
Your code all seems to work OK except for the print line in your displayPerson function. Replace that line with this, and I think you'll get the behavior you're looking for:
print("Person #" + id + " is " + personData[id][0] + " with a birthday of " + personData[id][1].strftime('%d/%m/%Y'))
When I enter a value of "1", I get the following output:
Person #1 is Charles Paige with a birthday of 06/01/1963
I was working on a cryptocurrency and everything else works but this, you can have a mine once, but then you can't after one time because the balance is the same. It should be simple. I need help with the balance and saving it and being able to mine more than once,
You will probly be able to fix this instantly
import os
import time
from anthonys_balance import lbalance
balance = 1
user = input("Username:")
f = open(user + "'s file", "w")
username = user
ubalance = balance
userbalance = f'{balance}{username}'
log = f'{user} has a balance of {lbalance} Coinon'
y=f'{user}s_balance.py'
if os.path.exists(y):
if os.path.isfile(y):
print("file is present")
else:
f = open(user + "s_balance.py", "x")
f.write (f'lbalance = {ubalance}')
f.close()
f = open(user + "'s file", "w")
f.write(str(log))
f.close()
userfile = user + "s_balance.txt"
os.system("clear")
def select():
from anthonys_balance import lbalance
print(f'{user} has a balance of {lbalance} Coinon')
print("Mine = 1")
print("Send = 2")
options = input("Selection:")
if options == "1":
def mine():
x = 1
if x == 1:
time.sleep(3)
#import user + "'s balance.py"
from anthonys_balance import lbalance
lnbalance = lbalance +1
f = open(user + "'s file", "w")
f.write(f'{user} has a balance of {lnbalance +1} Coinons')
f.close()
f = open(f'{user}s_balance.py', "w")
f.write(f'lbalance = {lnbalance +1}')
f.close()
print(f'{user} has a balance of {lnbalance +1} Coinons')
print("mine success")
input("Continue?")
f = open(user + "'s file", "w")
f.write(f'{user} has a balance of {lnbalance +1} Coinons')
f.close()
mine()
mine()
pass
select()
I didn't run it but I see few problems
you have mess in code. To make code more readable you could
put mine() outside select(),
convert some code into function - ie. read_data, save_data, log,
put all functions directly after import
send values to function as arguments
(see more in PEP 8 -- Style Guide for PythonCode)
you use import to load data but import reads file only once and it remember it - so it doesn't load again the same file. Python assumes that code doesn't change and data should be in files like .txt, .json, .ini, .yaml, `pickle, etc. or in database
This is my version with all changes.
I keep balance in dictionary data and I save it in .json - so I can keep other information data and save it in file - ie. username.
I load data only once - at start - and later I uses value which I have in memory and only I save new value in file. If there is no file at start then I create new one with default values.
I use while True to run code many times and user may select X to exit it.
import os
import time
import json
import datetime
# --- functions ---
def log(username, text):
dt = datetime.datetime.now().strftime('%Y.%m.%d %H:%M.%S')
filename = f'{username}.log'
with open(filename, "a") as f: # append
f.write(f'{dt} {text} \n')
def load_data(username):
filename = f'{username}.json'
if os.path.exists(filename) and os.path.isfile(filename):
with open(filename) as f:
data = json.load(f)
else:
data = None
return data
def save_data(username, data):
filename = f'{username}.json'
with open(filename, 'w') as f:
json.dump(data, f)
def select(username, data):
while True:
print(f'{username} has a balance of {data["balance"]} Coinons')
print("1. Mine")
print("2. Send")
print("X. Exit")
answer = input("Selection:")
answer = answer.upper().strip()
if answer == "X":
return
elif answer == "1":
mine(username, data)
elif answer == "2":
print('not created')
#send(username, data)
def mine(username, data):
time.sleep(3)
data['balance'] += 1
save_data(username, data)
log(username, f'{username} has balance {data["balance"]} Coinon')
print(f'{username} has a balance of {data["balance"]} Coinons')
print("mine success")
input("Press any key to continue.")
# --- main ---
username = input("Username: ")
# load data from file or use default value
data = load_data(username)
if not data:
data = {'balance': 1, "username": username}
save_data(username, data)
log(username, f'{username} has balance {data["balance"]} Coinon')
os.system("clear")
select(username, data)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a python script that writes to several file formats via Pandas. It can write to CSV/JSON/HTML/Excel.
However for some reason the script is writing blank files. When I open the file this is what I see:
Before printing the file I am printing the dataframe to the screen output so I can validate that the data is there. For example with CSV the output to the screen is this:
CSV data: ,AWS Account,Account Number,Name,Instance ID,AMI ID,Volumes,Private IP,Public IP,Private DNS,Availability Zone,VPC ID,Type,Key Pair Name,State,Launch Date
0,project-client-lab,123456789101,bastion001,i-xxxxxxxxxxxxxxxxxxxx,ami-xxxxxxxxxxxxxxxxxxx,vol-xxxxxxxxxxxxxxx,10.238.2.166,3.214.15.175,ip-10-238-2-166.ec2.internal,us-east-1a,vpc-xxxxxxxxxxxxxxxxx,t3.small,project-client-int01,running,March 10 2020
1,project-client-lab,123456789101,logicmonitor001,i-xxxxxxxxxxxxxxxxxxxx,ami-xxxxxxxxxxxxxxxxxxx,vol-0xxxxxxxxxxxxxx,10.238.2.52,,ip-10-238-2-52.ec2.internal,us-east-1a,vpc-xxxxxxxxxxxxxxxxx,m5.large,project-client-int01,running,September 02 2019
2,project-client-lab,123456789101,project-cassandra001,i-xxxxxxxxxxxxxxxxxxxx,ami-xxxxxxxxxxxxxxxxxxx,"vol-xxxxxxxxxxxxxxxxxx, vol-xxxxxxxxxxxxxxxxx",10.238.2.221,,ip-10-238-2-221.ec2.internal,us-east-1a,vpc-xxxxxxxxxxxxxxxxx,m5.large,project-client-int01,running,January 14 2020
3,project-client-lab,123456789101,project-cassandra003,i-xxxxxxxxxxxxxxxxxxxx,ami-xxxxxxxxxxxxxxxxxxx,"vol-xxxxxxxxxxxxxxxxxx, vol-xxxxxxxxxxxxxxxxx",10.238.2.207,,ip-10-238-2-207.ec2.internal,us-east-1a,vpc-xxxxxxxxxxxxxxxxx,m5.large,project-client-int01,running,January 14 2020
4,project-client-lab,123456789101,project-cassandra003,i-xxxxxxxxxxxxxxxxxxxx,ami-xxxxxxxxxxxxxxxxxxx,"vol-xxxxxxxxxxxxxxxxxx, vol-xxxxxxxxxxxxxxxxx",10.238.2.203,,ip-10-238-2-203.ec2.internal,us-east-1a,vpc-xxxxxxxxxxxxxxxxx,c5.xlarge,project-client-int01,running,January 22 2020
5,project-client-lab,123456789101,project-cassandra001,i-xxxxxxxxxxxxxxxxxxxx,ami-xxxxxxxxxxxxxxxxxxx,"vol-xxxxxxxxxxxxxxxxxx, vol-xxxxxxxxxxxxxxxxx",10.238.2.209,,ip-10-238-2-209.ec2.internal,us-east-1a,vpc-xxxxxxxxxxxxxxxxx,c5.xlarge,project-client-int01,running,January 22 2020
6,project-client-lab,123456789101,haproxy001,i-xxxxxxxxxxxxxxxxx,ami-xxxxxxxxxxxxxxxxxxx,vol-xxxxxxxxxxxxxxxxxx,10.238.2.169,54.242.118.165,ip-10-238-2-169.ec2.internal,us-east-1a,vpc-xxxxxxxxxxxxxxxxx,m5.large,project-client-int01,running,February 20 2020
7,project-client-lab,123456789101,logicmonitor002,i-xxxxxxxxxxxxxxx,ami-xxxxxxxxxxxxxxxxxxx,vol-0c48ff6ebb031008a,10.238.2.69,,ip-10-238-2-69.ec2.internal,us-east-1b,vpc-xxxxxxxxxxxxxxxxx,m5.large,project-client-int01,running,September 13 2019
These are the functions that write to file:
def mongo_export_to_file(interactive, aws_account, aws_account_number,instance_col=None,date=None):
create_directories()
if date == None:
format= "%m-%d-%Y"
today = datetime.today()
today = today.strftime(format)
date = today
else:
format= "%m-%d-%Y"
date = datetime.strptime(date,"%m%d%Y")
date = date.strftime(format)
if not instance_col:
_, _, instance_col = set_db()
# make an API call to the MongoDB server
if interactive == 0:
mongo_docs = instance_col.find({})
else:
mongo_docs = instance_col.find({"Account Number": aws_account_number})
# Convert the mongo docs to a DataFrame
docs = pandas.DataFrame(mongo_docs)
# Discard the Mongo ID for the documents
docs.pop("_id")
if __name__ == "__main__":
print("Choose a file format")
print("1. CSV")
print("2. JSON")
print("3. HTML")
print("4. Excel")
choice = input("Enter a number 1-4: ")
choice = int(choice)
else:
choice = 1
if choice == 1:
if __name__ == "__main__":
# export MongoDB documents to CSV
csv_export = docs.to_csv(sep=",") # CSV delimited by commas
print ("\nCSV data:", csv_export)
# Set the CSV output directory
output_dir = os.path.join("..", "..", "output_files", "aws_instance_list", "csv", "")
if interactive == 1:
output_file = os.path.join(output_dir, "aws-instance-list-" + aws_account + "-" + date +".csv")
else:
output_file = os.path.join(output_dir, "aws-instance-master-list-" + date +".csv")
# export MongoDB documents to a CSV file, leaving out the row "labels" (row numbers)
docs.to_csv(output_file, ",", index=False) # CSV delimited by commas
elif choice == 2:
if __name__ == "__main__":
json_export = docs.to_json() # return JSON data
print ("\nJSON data:", json_export)
# Set the JSON output directory
output_dir = os.path.join("..", "..", "output_files", "aws_instance_list", "json", "")
if interactive == 1:
output_file = os.path.join(output_dir, "aws-instance-list-" + aws_account + "-" + date +".json")
else:
output_file = os.path.join(output_dir, "aws-instance-master-list-" + date +".json")
# export MongoDB documents to a CSV file, leaving out the row "labels" (row numbers)
docs.to_json(output_file)
elif choice == 3:
html_str = io.StringIO()
# export as HTML
docs.to_html(
buf=html_str,
classes="table table-striped"
)
if __name__ == "__main__":
# print out the HTML table
print (html_str.getvalue())
# Set the HTML output directory
output_dir = os.path.join("..", "..", "output_files", "aws_instance_list", "html", "")
if interactive == 1:
output_file = os.path.join(output_dir, "aws-instance-list-" + aws_account + "-" + date +".html")
else:
output_file = os.path.join(output_dir, "aws-instance-master-list-" + date + ".html")
# save the MongoDB documents as an HTML table
docs.to_html(output_file)
elif choice == 4:
# Set the Excel output directory
output_dir = os.path.join("..", "..", "output_files", "aws_instance_list", "excel", "")
time.sleep(5)
if interactive == 1:
output_file = os.path.join(output_dir, "aws-instance-list-" + aws_account + "-" + date + ".xlsx")
else:
output_file = os.path.join(output_dir, "aws-instance-master-list-" + date + ".xlsx")
# export MongoDB documents to a Excel file, leaving out the row "labels" (row numbers)
writer = ExcelWriter(output_file)
docs.to_excel(writer,"EC2 List",index=False)
writer.save()
writer.close()
if __name__ == "__main__":
exit = input("Exit program (y/n): ")
if exit.lower() == "y" or exit.lower() == "yes":
exit_program()
else:
main()
def print_reports(interactive,aws_account,aws_account_number):
set_db(instance_col=None)
inputDate = input("Enter the date in format 'dd/mm/yyyy': ")
day,month,year = inputDate.split('/')
isValidDate = True
try:
datetime(int(year),int(month),int(day))
except ValueError :
isValidDate = False
print_reports(interactive,aws_account,aws_account_number)
if(isValidDate) :
print(f"Input date is valid: {inputDate}")
format= "%m%d%Y"
inputDate = datetime.strptime(inputDate,"%m/%d/%Y")
inputDate = inputDate.strftime(format)
else:
print(f"Input date is not valid: {inputDate}")
print_reports(interactive,aws_account,aws_account_number)
myclient = connect_db()
mydb = myclient["aws_inventories"]
instance_col = "ec2_list_" + inputDate
instance_col = mydb[instance_col]
mongo_export_to_file(interactive, aws_account, aws_account_number,instance_col,date=inputDate)
This is all my code in this script.
Why is this happening and how to I correct that?
You can view the file in Excel by:
Opening Excel
Going to the "Data" tab
In the "Get & Transform Data" section, click "From Text/CSV"
I have a table that contains a few categories and two of them are: mac address and device name. I had a the list of my mac address written in my code (hardcoded) with their corresponding device names (ie deviceDict['00:00:00:00:00:00']= name)
Now, I passed those mac addresses and device names to a text file to be read from that same Python code and parse it onto my table. The code currently recognizes the text file but it is not parsing that information onto the table.
Here is the code:
# File: WapLogParser.py
# Desc: Parses a WAP log file and pulls out information relating to connected clients
# Usage: python WapLogParser.py [file glob]
import re
import sys
import glob
import os
deviceDict = dict()
# Base table for storing client info
# All names must match what is in the Wap Log file
# Exceptions: Date, Wap Name, Device Name - which are provided outside of the result parsing
table = [["Ssid", "Vlan", "Mac Address", "Connected Time", "Ip Address", "Rssi", "Date", "Wap Name", "Device Name"]]
def ParseResult(result, date, wapName):
lines = result.split('\n')
lines = list(filter(None, lines))
# Any useful info will be at least 2 lines long
if len(lines) == 1:
return
# create empty row
data = [""] * len(table[0])
# for each item in the result place it in the correct spot in the row
for line in lines:
if line != "":
# Parse the key/value pair
m = re.match(r"(.*):\s\.*\s?(.*)", line)
if m is not None:
for idx in range(len(table[0])):
if table[0][idx].lower() == m[1].lower():
data[idx] = m[2]
else:
break
# Remove the '(dBm)' from the RSSI value
data[5] = data[5].split()[0]
# Append WAP specific items to row
data[6] = date
data[7] = wapName
data[8] = GetDeviceName(data[2].upper())
# Add row to table
table.append(data)
def ParseFile(path):
with open(path) as f:
lines = f.readlines()
result = ""
command = ""
date = ""
# WAP name is always on the first line 16 characters in with 4
# unnecessary characters trailing
wapName = lines[0].strip()[16:-4]
for line in lines:
line = line.strip()
# Is an issued command?
if line.startswith("/#"):
if command != "":
ParseResult(result, date, wapName)
command = ""
# reset the result for the new command
result = ""
m = re.match(r"^/#.*show\sclient.*stats$", line)
if m is not None:
command = line
# Anything that is not a command add to the result
else:
result += line + "\n"
# Do we have the date?
if line.startswith("Current date:"):
date = line.replace("Current date: ", "")
# Print output to stderr
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# Print a 2d array in a csv format
def PrintAsCsv(table):
for row in table:
print(",".join(row))
def Main():
InitDeviceDict()
numArgs = len(sys.argv)
for filename in glob.iglob(sys.argv[numArgs - 1], recursive=True):
# Globs get directories too
if os.path.isfile(filename):
eprint("Parsing " + filename)
try:
ParseFile(filename)
except Exception as e: # Mainly for if we see a binary file
eprint("Bad file: " + e)
# Print in a format we can use
PrintAsCsv(table)
def GetDeviceName(macAddress):
if macAddress in deviceDict:
return deviceDict[macAddress]
manufacturerPart = macAddress[:8]
if manufacturerPart in deviceDict:
return deviceDict[manufacturerPart]
return 'Unknown Device'
def InitDeviceDict():
with open('try.txt','r') as fo:
for line in fo:
deviceDict = {}
line = line.split(',')
macAddress = line[0].strip()
manufacturerPart = line[1].strip()
if macAddress in deviceDict:
deviceDict[macAddress].append(manufacturerPart)
else:
deviceDict[macAddress]=(manufacturerPart)
print(deviceDict)
# entry point
# script arguments:
# WapLogParser.py [file glob]
if __name__ == "__main__":
Main()
The issue is on the functions GetDeviceName and InitDeviceDict. When I run the code and then a batch file to display my info on excel, I keep getting "unknown device" (as if it is not recognizing the mac address I entered to produce the device name)
Any way I can correct this? Thank you
The deviceDict that is populated in InitDeviceDict is not the global deviceDict. You are only modifying a function-local dictionary (and resetting it every line as well). Remove deviceDict = {} from that function and, at the top of the function use global deviceDict to declare that you are modifying the global.
def InitDeviceDict():
global deviceDict
with open('try.txt','r') as fo:
for line in fo:
line = line.split(',')
macAddress = line[0].strip()
manufacturerPart = line[1].strip()
if macAddress in deviceDict:
deviceDict[macAddress].append(manufacturerPart)
else:
deviceDict[macAddress]=[manufacturerPart]
import datetime
import json
print ('--------------- Crptocurrency Transfer --------------- \n')
name = 'y'
while name != 'n':
# add name of the sender
print (' Enter the name of the sender: ')
sender = input("\n")
# add name of reciever #
print (' Enter the name of the receiver: ')
receiver = input("\n")
# how much would you like to send #
print (' How much would you like to send :$ ')
amount = str(input("\n"))
# save details to a log and append to text file
trans1 = [
{"sender": sender},
{"receiver": receiver},
{"amount": amount}
]
# ask if any more transactions, if no then end program
name = input (' Are there any more transactions? ( Enter y or n ): ')
with open('TransactionHistory.json', 'w') as th:
json.dump(trans1, th)
save all transactions( example, trans1, trans2, trans3) different names & different amounts to json as long as user keeps entering y and then open json file in another py program to use transaction data
Your approach works, there's just a few bugs e.g. header (import and first print), print (record) rather than print (trans1) etc... You originally used break, I think != 'n' is a bit more pythonic.
Second request, you need to initialize the file (in this case you've already done it,
with open('TransactionHistory.json', mode='w', encoding='utf-8') as f:
json.dump([], f)
Once the file exists you append within the loop, there is no other way and this approach will be slow.
import json
print ('--------------- Crptocurrency Transfer --------------- ')
name = ''
with open('TransactionHistory.json', mode='r', encoding='utf-8') as feedsjson:
feeds = json.load(feedsjson)
while name != 'n':
print (' Enter the name of the sender: ')
sender = input("\n")
print (' Enter the name of the receiver: ')
receiver = input("\n")
print (' How much would you like to send :$ ')
amount = str(input("\n"))
name = input (' Are there any more transactions? ( Enter y or n ): ')
# trans1 = [
# {"sender": sender},
# {"receiver": receiver},
# {"amount": amount}
# ]
with open('TransactionHistory.json', "w") as myjson:
entry = {"sender": sender, "receiver": receiver, "amount": amount}
feeds.append(entry)
json.dump (feeds, myjson)
# Check that the json db is okay
with open('TransactionHistory.json', "r") as f:
data= json.load(f)
print ("Done\n")
Output, Done