update
This commit is contained in:
parent
bc51a707ef
commit
203f3cbd92
4
by_department.csv
Normal file
4
by_department.csv
Normal file
@ -0,0 +1,4 @@
|
||||
name,username,department
|
||||
Sol Mansi,solm,IT infrastructure
|
||||
Lio Nelson,lion,User Experience Research
|
||||
Charlie Grey,greyc,Development
|
|
2
hosts.csv
Normal file
2
hosts.csv
Normal file
@ -0,0 +1,2 @@
|
||||
workstation.local,192.168.25.46
|
||||
webserver.cloud,10.2.5.6
|
|
@ -113,7 +113,9 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
"source": [
|
||||
"# Reading And Writing CSV"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@ -136,11 +138,98 @@
|
||||
" print(\"Name: {}, Phone: {}, Role: {}\".format(name, phone, role))\n",
|
||||
"f.close()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Reading CSV"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import csv\n",
|
||||
"f = open(\"csv_file.txt\")\n",
|
||||
"csv_f = csv.reader(f)\n",
|
||||
"for row in csv_f:\n",
|
||||
" name, phone, role = row\n",
|
||||
" print(\"Name: {}, Phone: {}, Role: {}\".format(name, phone, role))\n",
|
||||
"f.close()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Generating CSV"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import csv\n",
|
||||
"\n",
|
||||
"hosts = [[\"workstation.local\", \"192.168.25.46\"],[\"webserver.cloud\", \"10.2.5.6\"]]\n",
|
||||
"with open('hosts.csv', 'w') as hosts_csv:\n",
|
||||
" writer = csv.writer(hosts_csv)\n",
|
||||
" writer.writerows(hosts)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Reading and writing CSV files with dictionaries"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"users = [ {\"name\": \"Sol Mansi\", \"username\": \"solm\", \"department\": \"IT infrastructure\"}, \n",
|
||||
" {\"name\": \"Lio Nelson\", \"username\": \"lion\", \"department\": \"User Experience Research\"}, \n",
|
||||
" {\"name\": \"Charlie Grey\", \"username\": \"greyc\", \"department\": \"Development\"}]\n",
|
||||
"keys = [\"name\", \"username\", \"department\"]\n",
|
||||
"with open('by_department.csv', 'w') as by_department:\n",
|
||||
" writer = csv.DictWriter(by_department, fieldnames=keys)\n",
|
||||
" writer.writeheader()\n",
|
||||
" writer.writerows(users)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
Loading…
Reference in New Issue
Block a user