Files
hpos-web/device_5_result_csv_final.ipynb

147 lines
5.6 KiB
Plaintext
Raw Normal View History

2023-08-08 06:53:49 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "edbb2bd6-e678-4401-9c0d-83d9ad948bb7",
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"\n",
"def copy_columns(source_file, destination_file, header_names):\n",
" with open(source_file, 'r', newline='') as source_csvfile, open(destination_file, 'w', newline='') as destination_csvfile:\n",
" reader = csv.DictReader(source_csvfile)\n",
" fieldnames = header_names\n",
" writer = csv.DictWriter(destination_csvfile, fieldnames=fieldnames)\n",
"\n",
" writer.writeheader()\n",
"\n",
" for row in reader:\n",
" selected_data = {key: row[key] for key in fieldnames if key in row}\n",
" writer.writerow(selected_data)\n",
"\n",
"if __name__ == \"__main__\":\n",
" source_csv = \"path/to/source_file.csv\" # Replace with the path to your source CSV file\n",
" destination_csv = \"path/to/destination_file.csv\" # Replace with the path to your destination CSV file\n",
" header_names = [\"Name\", \"ABHA ID\", \"Age\", \"Gender\", \"Category\", \"Marital Status\", \"Care-of\", \"Address\", \"District\", \"State\", \"Pincode\", \"Mobile Number\", \"Blood group\", \"Test Result\"] # Replace with the header names you want to copy\n",
"\n",
" copy_columns(source_csv, destination_csv, header_names)\n",
"\n",
" print(\"Data copied successfully.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c242e21e-48a9-462c-8e3f-453913f84564",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data copied successfully with new header.\n"
]
}
],
"source": [
"import csv\n",
"from datetime import datetime\n",
"\n",
"def calculate_age(birth_year):\n",
" current_year = datetime.now().year\n",
" return current_year - birth_year\n",
"\n",
"def copy_csv_with_new_header(source_file, destination_file, destination_header):\n",
" # Create a list to store the filtered data from the source CSV file\n",
" temp_data = []\n",
"\n",
" # Create a mapping for the destination header names to the source header names\n",
" header_mapping = {\n",
" \"Name\": \"name\",\n",
" \"ABHA ID\": \"abhaId\",\n",
" \"Age\": \"birthYear\",\n",
" \"Gender\": \"gender\",\n",
" \"Category\": \"category\",\n",
" \"Marital Status\": \"maritalStatus\",\n",
" \"Care-of\": \"careOf\",\n",
" \"Address\": \"house\",\n",
" \"District\": \"district\",\n",
" \"State\": \"state\",\n",
" \"Pincode\": \"pinCode\",\n",
" \"Mobile Number\": \"phoneNumber\",\n",
" \"Blood Group\": \"bloodGroup\",\n",
" \"Test Result\": \"Class\"\n",
" }\n",
"\n",
" with open(source_file, 'r', newline='') as source_csvfile:\n",
" reader = csv.DictReader(source_csvfile)\n",
"\n",
" # Read the data from the source CSV file\n",
" for row in reader:\n",
" # Assuming the column \"birthYear\" exists in the CSV\n",
" birth_year = int(row.get(\"birthYear\", 0))\n",
" updated_age = calculate_age(birth_year)\n",
"\n",
" # Filter out unwanted fields from the row based on the destination header\n",
" filtered_row = {key: row[header_mapping[key]] if key != \"Age\" else updated_age for key in destination_header}\n",
"\n",
" # Add the filtered row to the temporary data list\n",
" temp_data.append(filtered_row)\n",
"\n",
" with open(destination_file, 'w', newline='') as destination_csvfile:\n",
" writer = csv.DictWriter(destination_csvfile, fieldnames=destination_header)\n",
"\n",
" # Write the new header to the destination CSV file\n",
" writer.writeheader()\n",
"\n",
" # Write the updated data to the destination CSV file\n",
" writer.writerows(temp_data)\n",
"\n",
"if __name__ == \"__main__\":\n",
" source_csv = r\"C:\\Users\\Durga\\Desktop\\from27.csv\" # Replace with the path to your source CSV file\n",
"\n",
" # Replace with the destination header names you want in the new CSV\n",
" destination_header = [\"Name\", \"ABHA ID\", \"Age\", \"Gender\", \"Category\", \"Marital Status\", \"Care-of\", \"Address\", \n",
" \"District\", \"State\", \"Pincode\", \"Mobile Number\", \"Blood Group\", \"Test Result\"]\n",
"\n",
" destination_csv = r\"C:\\Users\\Durga\\Desktop\\result tilljuly31.csv\" # Replace with the path to your destination CSV file\n",
"\n",
" copy_csv_with_new_header(source_csv, destination_csv, destination_header)\n",
"\n",
" print(\"Data copied successfully with new header.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "965db908-c717-40a3-81cb-3da73b744ec5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}