Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SciProg2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pclark3
SciProg2024
Commits
e4e16573
Commit
e4e16573
authored
1 year ago
by
cprutean
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
737ed5ad
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
CodeExamples/ReadTextFile.ipynb
+60
-0
60 additions, 0 deletions
CodeExamples/ReadTextFile.ipynb
with
60 additions
and
0 deletions
CodeExamples/ReadTextFile.ipynb
0 → 100644
+
60
−
0
View file @
e4e16573
{
"cells": [
{
"cell_type": "markdown",
"id": "3747cf6a",
"metadata": {},
"source": [
"# Read Text File"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d510374a",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
" Read a text file and print out to terminal\n",
"\"\"\"\n",
"\n",
"def main():\n",
" filename = str(input(\"Input file name : \"))\n",
" filein = open(filename, \"r\") # Open file for reading\n",
"\n",
"\n",
" input_data = filein.readlines() # Read in data as list of strings\n",
"\n",
" for line in input_data: # Scan through writing lines\n",
" print(line) # Note extra \"\\n\" problem \n",
"\n",
" for line in input_data:\n",
" print (line[:-1]) # This removes the last character\n",
"\n",
"main()"
]
}
],
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:markdown id:3747cf6a tags:
# Read Text File
%% Cell type:code id:d510374a tags:
```
python
"""
Read a text file and print out to terminal
"""
def
main
():
filename
=
str
(
input
(
"
Input file name :
"
))
filein
=
open
(
filename
,
"
r
"
)
# Open file for reading
input_data
=
filein
.
readlines
()
# Read in data as list of strings
for
line
in
input_data
:
# Scan through writing lines
print
(
line
)
# Note extra "\n" problem
for
line
in
input_data
:
print
(
line
[:
-
1
])
# This removes the last character
main
()
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment