Download presentation
Presentation is loading. Please wait.
1
Python I/O
2
Array recap
3
Task 1 Create a new folder called Create a new text file
Add the following details to your file, then save to the File IO folder – Superhero name, city of origin, real name, rating out of ten
4
Why? Python is able to access the information you have just stored in your text file Take a look at Task 2 in your A453 assessment. Why do you think we need to learn how to access information from a separate file for this task?
6
In Python… Type these two lines at the top of your code.
What do you think these two lines will do? What do you think ‘csv’ stands for?
7
In Python… Type these two lines at the top of your code.
Line 1 – Opens your ‘Superheroes’ text file inside Python Line 2 – Converts your text file into a format that Python can understand. Comma Separated Values What separates the values in our text file Name of original text file variable New variable name Csv.reader function
8
In Python… Type these lines at the top of your code.
Line 1 – Creates an empty Names array Line 2 – Instructs python to iterate over every row inside the CSV file Line 3 – Adds the first row of the text file to the Names array 1 2 3
9
Tasks Add separate arrays for City of Origin, Real Name and Rating
Edit your For loop to append these arrays with the relevant information from your CSV file Change the data type of your Rating array from strings to integers (look at your A453 Task 1 code for a hint…) 1 2 3
10
Add this to the bottom of your code
11
In Python Ask the user to type in which superhero they would like to search for If the name we type in (q) exists in our Name array, do this: Sets the index of Name array of what we typed in Prints out the name we searched for Prints out the relevant information from each individual array
12
Tasks Edit your code so that the user can search for a superheroes real name and have Python tell them the relevant information (Easy) Add to your code so that the user can choose whether to search by either superhero name, city of origin or real name, and print the relevant information (Medium) Tell the user what the average rating is of all the superheroes (Medium) Hint: use the ‘sum’ and ‘len’ commands
13
Input and Output We have learned to input data into Python from an external source (a text file) and sort through it to find the data we need The next thing we need to do is output this data in a way that is easy for the user to read and understand You will need to know how to do this for Task 2 and 3 in your A453 project, so listen and learn!
14
In Python… The first thing we need to do is create a new text file from within Python. This is where our information will be output This creates a new text file, Superhero Information, and stores it in Python under the variable name new_text_file The w+ operator at the end of the line means we can read and write to the file
15
Add this to the bottom of your code
16
1) Asks the user to input which superhero they want information about
2) If what we type in (writing) exists in the Name array, do this: 3) Set our array index to the location of the superhero we typed in 4) Writes the column headings ‘Name’ and ‘Real Name’ to the new text file 5) Creates a new line in our text file (like pressing Enter on the keyboard) 6) Writes a new line in our text file with the relevant variables from our array – Name and Real Name
17
Run Your Code Open the text file in your folder
18
Tasks Edit your code so that the program outputs “Superhero not found” if the user types in a superhero that doesn’t exist (Easy) Edit your code so that the user can search for a superhero. The program should output the Name, City, Real Name and Rating to a new text file (Medium) Edit your code so that the user can search for a superhero by their City of Origin. The program should output the Name, City, Real Name and Rating to a new text file (Medium) Edit your code so that the user can decide what the name of the outputted text file is (Medium)
20
Summary We can import data into Python from text files
We do this by converting the text file into a CSV file so Python can understand it This is then stored into separate arrays which we can search and manipulate We can output data into separate text files We can select which data we output using user input and iteration
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.