Sodapdf
Sodapdf
INTRODUTCTION:
--------------
*This Python program allows you to create a CSV file containing student information and search for
specific entries in that file. The program includes the following features:
REQUREMENTS:
------------
*No external libraries are required for this program as it uses Python’s built-in ‘csv‘ module for
reading and writing CSV files.
CODE EXPLANATION:
---- ------------
*1. Importing Libraries
‘‘‘python
import csv
‘‘‘
This line imports the ‘csv‘ module, which provides functionality to read from and write to CSV files.
‘‘‘python
def create():
with open(’trios.csv’, ’w’, newline=’’) as fout:
writer = csv.writer(fout)
h = [’rno’, ’name’, ’clsss’, ’house’, ’game’, ’area’]
class_12th =[
[1, ’akash’,
’12th’, ’abhimanyu’, ’volly ball’, ’illuppur’],
[2, ’buvanan’, ’12th’, ’bheeshma’, ’foot ball’, ’viralimala’],
[3, ’deepak’, ’12th’, ’arjuna’, ’kabaddi’, ’illuppur’],
[4, ’dharshon’, ’12th’, ’bheeshma’, ’volly ball’, ’township’],
[5, ’dinesh kumar’, ’12th’, ’karna’, ’volly ball’, ’viralimalai’],
[6, ’rasik’, ’12th’, ’abhimanyu’, ’volly ball’, ’illuppur’],
[7, ’sakthi vel’, ’12th’, ’arjuna’, ’volly ball’, ’township’],
[8, ’sridher’, ’12th’, ’bheeshma’, ’kabaddi’, ’illuppur’],
[9, ’sudharshan’, ’12th’, ’karna’, ’volly ball’, ’illuppur’],
[10, ’tamil selvan’, ’12th’, ’abhimanyu’, ’kabaddi’, ’annavasal’],
[11, ’thio flus’, ’12th’, ’arjuna’, ’vollyball’, ’illuppur’],
[22, ’ashfaq’, ’12th’, ’abhimanyu’, ’vollyball’, ’annavasal’]
]
writer.writerows(class_12th)
print(’Created CSV file with titles:’, h)
‘‘‘
- The ‘create()‘ function generates a CSV file named ‘trios.csv‘ containing student data. Each row
represents a student with details like roll number, name, class, house, game, and area.
- The column titles (‘rno‘, ‘name‘, ‘clsss‘, ‘house‘, ‘game‘, ‘area‘) are written as the header.
- A list of student data is then written to the CSV file.
- After execution, the program will print a confirmation message indicating the file has been created.
* 3. ‘search()‘ FunctioN
‘‘‘python
def search():
ent = input(’Check the title and give an input to search: ’)
l = []
for j in l:
for k in j:
if str(k) == ent:
print(j)
‘‘‘
- The ‘search()‘ function prompts the user to input a keyword or value to search for in the CSV file.
- It reads the contents of ‘trios.csv‘ into a list, ‘l‘.
- Then, it loops through the list and searches each item (student record) for the input keyword.
- If the input matches any value in a student record, that entire record (the row) is printed.
‘‘‘python
create() # Creates the CSV file with student data
ch = ’yes’
while ch == ’yes’:
search() # Calls the search function
ch = input(’Do you want to continue? (yes or no): ’)
‘‘‘
- The program first calls the ‘create()‘ function to create the CSV file with student data.
- It then enters a loop, allowing the user to perform multiple searches. The loop continues as long as the
user responds with "yes" to the prompt: "Do you want to continue?"
- If the user enters anything other than "yes," the program ends.
3. **Input to Search**:
- The program will first create a CSV file (‘trios.csv‘) containing student data.
- You will then be prompted to enter a search term. The program will search for this term in the CSV file
and print any matching student records.
4. **Repeat or Exit**:
- After each search, you will be asked if you want to continue. Enter "yes" to perform another search, or
"no" to exit the program.
## Example Usage
1. **Program Execution**:
‘‘‘
created titles= [’rno’, ’name’, ’clsss’, ’house’, ’game’, ’area’]
Check the title and give an input to search: ’akash’
[’1’, ’akash’, ’12th’, ’abhimanyu’, ’volly ball’, ’illuppur’]
2. **Output Explanation**:
- After entering ‘’akash’‘, the program found the student record for Akash and printed it.
- After entering ‘’foot ball’‘, it found the record for Buvanan and printed it.
- The program then asked if you wanted to continue. When you chose "no", the program ended.
## Conclusion
This program provides a simple way to store and search for student information in a CSV file. It can be
extended to include more advanced features such as:
- Case-insensitive searching.
- Searching by multiple columns (e.g., searching by both game and area).
- Saving search results to a file.