DW - DW Internal 1 - Merged
DW - DW Internal 1 - Merged
def write_to_file(filename):
for _ in range(num_students):
file.write(f"{name},{roll_no},{marks}\n")
def read_from_file(filename):
print("\nStudent Details:")
def main():
filename = "students.csv"
while True:
print("3. Exit")
if choice == "1":
write_to_file(filename)
read_from_file(filename)
break main()
Experiment – 2
import csv
def read_csv_as_dict(filename):
try:
with open(filename, mode='r') as file:
csv_reader = csv.DictReader(file)
print("\nCSV File Contents:")
for row in csv_reader:
print(dict(row))
except FileNotFoundError:
print(f"Error: The file '{filename}' was not found.")
except Exception as e:
print(f"An error occurred: {e}")
def main():
filename = input("Enter the CSV file name (with .csv extension): ")
read_csv_as_dict(filename)
main()
Experiment – 3
import json
# Data to write
data = {
"name": "Alice",
"age": 30,
"city": "New York"
}
import xml.etree.ElementTree as ET
import pandas as pd
import numpy as np
# c. Insert a column in the sixth position and fill it with NaN values
#df.insert(5, 'New_Column', np.nan) # Index 5 corresponds to the 6th column
print("\nDataFrame After Adding New Column:")
print(df.head()) # Display the first few rows to confirm the new column
import pandas as pd
file_path = "child7exp.xlsx"
xls = pd.ExcelFile(file_path)
df = pd.read_excel(xls, sheet_name=sheet)
output_csv = f"{sheet}.csv"
df.to_csv(output_csv, index=False)
import sqlite3
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
cursor.execute("""
salesman_id INTEGER,
name TEXT,
city TEXT,
commission REAL
);
""")
try:
cursor.execute("""
VALUES (?, ?, ?, ?)
except sqlite3.Error as e:
finally:
import agate
table = agate.Table.from_csv('data10.csv')
print("Table Columns:")
table.print_table(max_rows=5)
print("\nSummary Statistics:")
if isinstance(column.data_type, agate.Number):
print(f"{column.name}:")
col1 = [float(value) for value in table.columns['CPI 2013 Score'] if isinstance(value, (int, float))]
print(table.columns['Total (%)'])
Program 11
data = [
{'Country': 'Country A', 'CPI 2013 Score': 90, 'Total (%)': 5},
{'Country': 'Country B', 'CPI 2013 Score': 80, 'Total (%)': 15},
{'Country': 'Country C', 'CPI 2013 Score': 70, 'Total (%)': 25},
plt.figure(figsize=(10, 6))
plt.grid(True)
plt.show()
Program 12
import pygal
custom_style = Style(
background='white',
plot_background='white',
foreground='black',
foreground_strong='black',
foreground_subtle='gray',
child_labour_data = {
'cn': 5, # China
'br': 7, # Brazil
'eg': 15 # Egypt
worldmap = pygal.maps.world.World(style=custom_style)
worldmap.render_to_file('world.svg')
import requests
response = requests.get("https://en.wikipedia.org/robots.txt")
test = response.text
print("=" * 60)
print(test)