Why does my function fail to create lists? - python

I am trying to create 5 lists out of 1 data file, the error I keep getting states that "airlines is not defined", yet it is the first thing I define in the function, how is this possible? What should I do to correctly create a list of airlines, arrival times, departure times, prices, and flight number?
USAir,1269,6:15,10:57,210
Delta,5138,16:20,22:10,212
UNITED,6001,14:12,20:50,217
Delta,5054,12:30,20:22,227
UNITED,5949,9:30,14:43,264
JetBlue,1075,17:00,20:06,280
Delta,1263,6:00,11:30,282
Delta,3824,9:00,14:45,282
USAir,1865,16:55,21:33,300
USAir,3289,18:55,23:41,300
USAir,1053,8:00,13:02,300
USAir,2689,12:55,18:09,300
USAir,3973,9:25,14:00,302
USAir,3267,11:30,16:13,302
USAir,3609,13:25,18:28,302
USAir,3863,15:35,20:54,302
USAir,3826,17:45,23:19,302
USAir,1927,7:00,12:53,302
Delta,3601,12:00,17:29,307
Delta,4268,7:15,12:46,307
UNITED,4676,6:00,10:45,321
UNITED,4103,11:00,16:16,321
USAir,3139,11:51,16:29,332
JetBlue,475,7:30,10:42,340
USAir,3267,11:30,18:15,367
UNITED,2869,16:55,21:33,406
UNITED,2865,6:15,10:57,406
UNITED,2729,8:00,13:02,406
UNITED,2645,7:00,12:53,445
and the code I am using is
def getFlights():
airlines = []
flightNums = []
depTimes = []
arriveTimes = []
prices = []
fname = input("Enter name of data file: ")
infile = open(fname, 'r')
line = infile.readline()
line = line.strip()
while line != "":
line = line.strip()
airline, flightNum, depTime, arriveTime, price = line.split(',')
airlines.append(airline)
flightNums.append(flightNum)
depTimes.append(depTime)
arriveTimes.append(arriveTime)
prices.append(price)
line = infile.readline()
line = line.strip()
infile.close()
return airlines, flightNums, depTimes, arriveTimes, prices
getFlights()
print(airlines, flightNums, depTimes, arriveTimes, prices)

Local variables inside a function are not accessible outside of the function call. If you want to use the returned values of getFlights you must assign them to variables in the calling context.
(airlines, flightNums, depTimes, arriveTimes, prices) = getFlights()
print(airlines, flightNums, depTimes, arriveTimes, prices)

What b4hand has said is correct, however, the Pythonic way of doing this is using csv.reader and a with statement, eg:
import csv
filename = input('Enter filename: ')
with open(filename, 'rb') as fin:
csvin = csv.reader(fin)
airlines, flightNums, depTimes, arriveTimes, prices = zip(*csvin)

Related

How to remove an element from a JSON array using Python?

I'm currently trying to make a Chromebook rental application for my high school that stores checkout information in a JSON file. Everything works except removing data from the JSON array. I found a YouTube video(link) that I thought would work as a solution, so I followed along with that. However, whenever there's more than two elements and I enter anything higher than two, it doesn't delete anything. Even worse, when I enter the number one, it deletes everything but the zero index(whenever the array has more than two elements in it).
Here's the Python code:
def view_data(): # Prints JSON Array to screen
with open(filename, "r") as f:
data = json.load(f)
i = 0
for item in data:
name = item["name"]
chromebook = item["chromebook"]
check_out = item["time&date"]
print(f"Index Number: {i}")
print(f"Name : {name}")
print(f"Chromebook : {chromebook}")
print(f"Time Of Checkout: {check_out} ")
print("\n\n")
i = i + 1
def delete_data(): # Deletes an element from the array
view_data()
new_data = []
with open(filename, "r") as f:
data = json.load(f)
data_length = len(data) - 1
print("Which index number would you like to delete?")
delete_option = input(f"Select a number 0-{data_length}: ")
i = 0
for entry in data:
if i == int(delete_option):
pass
i = + 1
else:
new_data.append(entry)
i = + 1
with open(filename, "w") as f:
json.dump(new_data, f, indent=4)
Here's the JSON file code:
[
{
"name": "Tyler",
"chromebook": "12123223",
"time&date": "Check Out Time: 13:33:22 May-11-2021"
},
{
"name": "Craig",
"chromebook": "41222224",
"time&date": "Check Out Time: 13:33:34 May-11-2021"
},
{
"name": "Bill",
"chromebook": "3235223",
"time&date": "Check Out Time: 13:33:46 May-11-2021"
}
]
For example, say the user wanted to remove the second index in the JSON array. Is there a better way to implement that in my Python script?
I'm still a fairly new and learning Python developer, and if there's any better solution I'm open to suggestions. If you need for info, I'll be active.
First question
However, whenever there's more than two elements and I enter anything higher than two, it doesn't delete anything. Even worse, when I enter the number one, it deletes everything but the zero index(whenever the array has more than two elements in it).
Inside delete_data() you have two lines reading i = + 1, which just assignes +1 (i.e., 1) to i. Thus, you're never increasing your index. You probably meant to write either i = i+1 or i += 1.
def delete_data(): # Deletes an element from the array
view_data()
new_data = []
with open(filename, "r") as f:
data = json.load(f)
data_length = len(data) - 1
print("Which index number would you like to delete?")
delete_option = input(f"Select a number 0-{data_length}: ")
i = 0
for entry in data:
if i == int(delete_option):
i += 1 # <-- here
else:
new_data.append(entry)
i += 1 # <-- and here
with open(filename, "w") as f:
json.dump(new_data, f, indent=4)
Second question: further improvements
Is there a better way to implement that in my Python script?
First, you can get rid of manually increasing i by using the builtin enumerate generator. Second, you could make your functions reusable by giving them parameters - where does the filename in your code example come from?
# view_data() should probably receive `filename` as a parameter
def view_data(filename: str): # Prints JSON Array to screen
with open(filename, "r") as f:
data = json.load(f)
# iterate over i and data simultaneously
# alternatively, you could just remove i
for i, item in enumerate(data):
name = item["name"]
chromebook = item["chromebook"]
check_out = item["time&date"]
print(f"Index Number: {i}")
print(f"Name : {name}")
print(f"Chromebook : {chromebook}")
print(f"Time Of Checkout: {check_out} ")
print("\n\n")
# not needed anymore: i = i + 1
# view_data() should probably receive `filename` as a parameter
def delete_data(filename: str): # Deletes an element from the array
view_data()
new_data = []
with open(filename, "r") as f:
data = json.load(f)
data_length = len(data) - 1
print("Which index number would you like to delete?")
delete_option = input(f"Select a number 0-{data_length}: ")
# iterate over i and data simultaneously
for i, entry in enumerate(data):
if i != int(delete_option):
new_data.append(entry)
with open(filename, "w") as f:
json.dump(new_data, f, indent=4)
Furthermore, you could replace that for-loop by a list comprehension, which some may deem more "pythonic":
new_data = [entry for i, entry in enumerate(data) if i != int(delete_option)]
There are easier ways to delete an element by index from a Python list.
Given li = ["a", "b", "c"], you can delete element 1 ("b") by index in (at least) the following ways:
li.pop(1) # pop takes an index (defaults to last) and removes and returns the element at that index
del li[1] # the del keyword will also remove an element from a list
So, here's some updated code:
def view_data(): # Prints JSON Array to screen
with open(filename, "r") as f:
data = json.load(f)
i = 0
for item in data:
name = item["name"]
chromebook = item["chromebook"]
check_out = item["time&date"]
print(f"Index Number: {i}")
print(f"Name : {name}")
print(f"Chromebook : {chromebook}")
print(f"Time Of Checkout: {check_out} ")
print("\n\n")
i = i + 1
def delete_data(): # Deletes an element from the array
view_data()
with open(filename, "r") as f:
data = json.load(f)
data_length = len(data) - 1
print("Which index number would you like to delete?")
delete_option = input(f"Select a number 0-{data_length}: ")
del data[int(delete_option)] # or data.pop(int(delete_option))
with open(filename, "w") as f:
json.dump(data, f, indent=4)
import json
data = json.loads(jsonString) #convert json string to object
delete_option = input(f"Select a number 0-{data_length}: ")
del data[int(delete_option)]

How to print in a specific element in a list in a sorted order in python

I'm tasked to create a function that takes a string filename as an argument, reads the file with the name filename, and prints
all confirmed reservations in order of the time.
A line in the file is formatted like so
name, time, reservation_status (can either be confirmed or canceled in uppercase)
An example of a file "reservations.txt" could look like this:
Alex, 20, CONFIRMED
Thomas, 16, CANCELLED
William, 18, CONFIRMED
The correct output when calling the function on this example file should look like this:
show_reservations("reservations.txt")
>>> William, 18
Alex, 20
My solution:
def show_reservations(filename):
with open(filename) as f:
for line in f.readlines():
line = line.replace(',', '').split()
status = line[2]
if status == "CONFIRMED":
name = line[0]
time = line[1]
print(name + ",", time)
However, calling my solution on the example file above gives me following output:
show_reservations("reservations.txt")
>>> Alex, 20
William, 18
What to do?
you don't store your data and you also need some sorting
def funct(e):
return e['time']
def show_reservations(filename):
with open(filename) as f:
l=[]
for line in f.readlines():
line = line.replace(',', '').split()
dict={}
status = line[2]
if status == "CONFIRMED":
dict["name"] = line[0]
dict["time"] = line[1]
name = line[0]
time = line[1]
l.append(dict)
l.sort(key=funct)
for i in l:
print('{0} , {1}'.format(i["name"],i["time"]))
show_reservations("input.txt")
It happens because your original sequence comes in this way:
Alex, 20, CONFIRMED
...
William, 18, CONFIRMED
So you can save your filtered elements into a list and apply sorting on it. For example using sorted function.
You could try something like this:
def show_reservations(filename):
confirmed_orders = []
with open(filename) as f:
for line in f.readlines():
line = line.replace(',', '').split()
name, _time, status = line
if status == "CONFIRMED":
confirmed_orders.append((name, _time))
confirmed_orders_by_time = sorted(confirmed_orders, key=lambda x: x[1])
for name, _time in confirmed_orders_by_time:
print(name + ",", _time)
Also several additional suggestions in case the snippet you've provided is a real production code:
time is a bad name for variable because it can clash with built-in Python's module time.
split gives you tuple, so instead of messing with accessing by index you can unpack it:
name, _time, status = line
This thing line.replace(',', '').split() won't work correctly if name or status in the file will have a "space". Consider to use csv or something else for parsing data file.
If you use Python 3.5 or higher f-string is a preferable way instead of manual string concatenation:
print(f"{name}, {_time}")
def show_reservations(filename):
reservations = []
with open(filename) as f:
for line in f.readlines():
splitted_line = line.replace(',', '').split()
status = splitted_line[2]
if status == "CONFIRMED":
time = splitted_line[1]
name = splitted_line[0]
reservations.append({"time":time, "name":name})
return sorted(reservations, key=lambda k: k['time'])
for reservation in show_reservations("reservations.txt"):
print(reservation["name"] + ",", reservation["time"])
Instead of directly printing, append the entries to a list of tuples (time, name). Then after the list, sort it (li.sort()), and loop through it again, this time printing.

Trying to call a variable from a function to another function

def editselection():
#this converts the text in the files into a list in a list
with open("stocks", "r") as stocks:
for line in stocks:
stripped_line = line.strip()
line_list = stripped_line.split()
list_of_items.append(line_list)
itemselection = input('Choice: ')
if itemselection.isalpha() == True:
ManageStock()
elif itemselection == '':
ManageStock()
itemselection = int(itemselection)
os.system('clear')
#the square brackets are the indexes so for example if they select 0, the first item turned into a list would be known as specific item
specificitem = list_of_items[itemselection]
changeitem(specificitem)
return specificitem
I'm trying to call the variable 'specificitem' to the function AddItem()
def AddToCart(specificitem):
os.system('clear')
number = 0
os.system('clear')
print ("""Here is the current stock
--------------------------
Name, Price, Quantity
--------------------------
""")
with open ('stocks', 'r') as stocks:
for i in stocks:
number = str(number)
print (number+'.' , i)
number = int(number)
number = number + 1
#this converts the text in the files into a list in a list
with open("stocks", "r") as stocks:
for line in stocks:
stripped_line = line.strip()
line_list = stripped_line.split()
list_of_items.append(line_list)
itemselection = input('Choice: ')
if itemselection.isalpha() == True:
AddToCart()
if itemselection == '':
MakeASale()
itemselection = int(itemselection)
#the square brackets are the indexes so for example if they select 0, the first item turned into a list would be known as specific item
quantity = input('How many would you like? ')
chosenitem2 = list_of_items[itemselection]
with open ('cart' , 'a') as cart:
chosenitem2 = str(chosenitem2)
cart.write(chosenitem2 + '\n')
with open("cart", "r") as cart:
for line in cart:
stripped_line = line.strip()
line_list = stripped_line.split()
list_of_cart.append(line_list)
with open ("cart" , "r+") as cart:
data = cart.read()
data = data.replace(chosenitem2[2], quantity)
cart.close
cart = open('cart' , 'wt')
cart.write(data)
cart.close()
with open ("stocks" , "r+") as stocks:
data = stocks.read()
data = data.replace(specificitem[2], chosenitem2[2])
stocks.close
stocks = open('stocks' , 'wt')
stocks.write(data)
stocks.close()
print(chosenitem2)
though it comes up with AddToCart() missing 1 required positional argument: 'specificitem'
I'm trying to use the variable from editselection to edit the quantity for example when the user enters a value it adds it to the file cart and 'subtracts' if you will from the file stocks, the use of global is unavailable due to the fact that I'll just get marked down. I've been stuck on this for 2 days now
In the first function write (function name)editselection.(variable name)specificitem=(value)list_of_items[itemselection]
And on the second function call the variable for example like this:
print(editselection.specificitem)
And this will print the value of the variable.
This is called a function variable (or something like this)

Parsing Input File in Python

I have a plain text file with some data in it, that I'm trying to open and read using a Python (ver 3.2) program, and trying to load that data into a data structure within the program.
Here's what my text file looks like (file is called "data.txt")
NAME: Joe Smith
CLASS: Fighter
STR: 14
DEX: 7
Here's what my program looks like:
player_name = None
player_class = None
player_STR = None
player_DEX = None
f = open("data.txt")
data = f.readlines()
for d in data:
# parse input, assign values to variables
print(d)
f.close()
My question is, how do I assign the values to the variables (something like setting player_STR = 14 within the program)?
player = {}
f = open("data.txt")
data = f.readlines()
for line in data:
# parse input, assign values to variables
key, value = line.split(":")
player[key.strip()] = value.strip()
f.close()
now the name of your player will be player['name'], and the same goes for all other properties in your file.
import re
pattern = re.compile(r'([\w]+): ([\w\s]+)')
f = open("data.txt")
v = dict(pattern.findall(f.read()))
player_name = v.get("name")
plater_class = v.get('class')
# ...
f.close()
The most direct way to do it is to assign the variables one at a time:
f = open("data.txt")
for line in f: # loop over the file directly
line = line.rstrip() # remove the trailing newline
if line.startswith('NAME: '):
player_name = line[6:]
elif line.startswith('CLASS: '):
player_class = line[7:]
elif line.startswith('STR: '):
player_strength = int(line[5:])
elif line.startswith('DEX: '):
player_dexterity = int(line[5:])
else:
raise ValueError('Unknown attribute: %r' % line)
f.close()
That said, most Python programmers would stored the values in a dictionary rather than in variables. The fields can be stripped (removing the line endings) and split with: characteristic, value = data.rstrip().split(':'). If the value should be a number instead of a string, convert it with float() or int().

Group and Check-mark using Python

I have several files, each of which has data like this (filename:data inside separated by newline):
Mike: Plane\nCar
Paula: Plane\nTrain\nBoat\nCar
Bill: Boat\nTrain
Scott: Car
How can I create a csv file using python that groups all the different vehicles and then puts a X on the applicable person, like:
Assuming those line numbers aren't in there (easy enough to fix if they are), and with an input file like following:
Mike: Plane
Car
Paula: Plane
Train
Boat
Car
Bill: Boat
Train
Scott: Car
Solution can be found here : https://gist.github.com/999481
import sys
from collections import defaultdict
import csv
# see http://stackoverflow.com/questions/6180609/group-and-check-mark-using-python
def main():
# files = ["group.txt"]
files = sys.argv[1:]
if len(files) < 1:
print "usage: ./python_checkmark.py file1 [file2 ... filen]"
name_map = defaultdict(set)
for f in files:
file_handle = open(f, "r")
process_file(file_handle, name_map)
file_handle.close()
print_csv(sys.stdout, name_map)
def process_file(input_file, name_map):
cur_name = ""
for line in input_file:
if ":" in line:
cur_name, item = [x.strip() for x in line.split(":")]
else:
item = line.strip()
name_map[cur_name].add(item)
def print_csv(output_file, name_map):
names = name_map.keys()
items = set([])
for item_set in name_map.values():
items = items.union(item_set)
writer = csv.writer(output_file, quoting=csv.QUOTE_MINIMAL)
writer.writerow( [""] + names )
for item in sorted(items):
row_contents = map(lambda name:"X" if item in name_map[name] else "", names)
row = [item] + row_contents
writer.writerow( row )
if __name__ == '__main__':
main()
Output:
,Mike,Bill,Scott,Paula
Boat,,X,,X
Car,X,,X,X
Plane,X,,,X
Train,,X,,X
Only thing this script doesn't do is keep the columns in order that the names are in. Could keep a separate list maintaining the order, since maps/dicts are inherently unordered.
Here is an example of how-to parse these kind of files.
Note that the dictionary is unordered here. You can use ordered dict (in case of Python 3.2 / 2.7) from standard library, find any available implmentation / backport in case if you have older Python versions or just save an order in additional list :)
data = {}
name = None
with open(file_path) as f:
for line in f:
if ':' in line: # we have a name here
name, first_vehicle = line.split(':')
data[name] = set([first_vehicle, ]) # a set of vehicles per name
else:
if name:
data[name].add(line)
# now a dictionary with names/vehicles is available
# let's convert it to simple csv-formatted string..
# a set of all available vehicles
vehicles = set(v for vlist in data.values()
for v in vlist)
for name in data:
name_vehicles = data[name]
csv_vehicles = ''
for v in vehicles:
if v in name_vehicles:
csv_vehicles += v
csv_vehicles += ','
csv_line = name + ',' + csv_vehicles
Assuming that the input looks like this:
Mike: Plane
Car
Paula: Plane
Train
Boat
Car
Bill: Boat
Train
Scott: Car
This python script, places the vehicles in a dictionary, indexed by the person:
#!/usr/bin/python
persons={}
vehicles=set()
with open('input') as fd:
for line in fd:
line = line.strip()
if ':' in line:
tmp = line.split(':')
p = tmp[0].strip()
v = tmp[1].strip()
persons[p]=[v]
vehicles.add(v)
else:
persons[p].append(line)
vehicles.add(line)
for k,v in persons.iteritems():
print k,v
print 'vehicles', vehicles
Result:
Mike ['Plane', 'Car']
Bill ['Boat', 'Train']
Scott ['Car']
Paula ['Plane', 'Train', 'Boat', 'Car']
vehicles set(['Train', 'Car', 'Plane', 'Boat'])
Now, all the data needed are placed in data-structures. The csv-part is left as an exercise for the reader :-)
The most elegant and simple way would be like so:
vehiclesToPeople = {}
people = []
for root,dirs,files in os.walk('/path/to/folder/with/files'):
for file in files:
person = file
people += [person]
path = os.path.join(root, file)
with open(path) as f:
for vehicle in f:
vehiclesToPeople.setdefault(vehicle,set()).add(person)
people.sort()
table = [ ['']+people ]
for vehicle,owners in peopleToVehicles.items():
table.append([('X' if p in vehiclesToPeople[vehicle] else '') for p in people])
csv = '\n'.join(','.join(row) for row in table)
You can do pprint.pprint(table) as well to look at it.

Categories