Read Only Numbers From Text File Java Printout

Reading and Writing Text Files

Overview

Teaching: 60 min
Exercises: thirty min

Questions

  • How tin can I read in data that is stored in a file or write information out to a file?

Objectives

  • Be able to open a file and read in the data stored in that file

  • Empathize the difference between the file proper noun, the opened file object, and the data read in from the file

  • Exist able to write output to a text file with unproblematic formatting

Why do we want to read and write files?

Being able to open up and read in files allows us to work with larger information sets, where information technology wouldn't be possible to type in each and every value and shop them one-at-a-time as variables. Writing files allows us to process our data and then salve the output to a file and then we can look at it afterward.

Correct now, we will practice working with a comma-delimited text file (.csv) that contains several columns of data. However, what you learn in this lesson can exist applied to whatever general text file. In the next lesson, you will learn another way to read and procedure .csv data.

Paths to files

In social club to open a file, nosotros need to tell Python exactly where the file is located, relative to where Python is currently working (the working directory). In Spyder, we can do this past setting our electric current working directory to the folder where the file is located. Or, when we provide the file proper name, nosotros can give a complete path to the file.

Lesson Setup

We volition work with the practice file Plates_output_simple.csv.

  1. Locate the file Plates_output_simple.csv in the directory home/Desktop/workshops/bash-git-python.
  2. Copy the file to your working directory, home/Desktop/workshops/YourName.
  3. Make sure that your working directory is also set to the binder dwelling house/Desktop/workshops/YourName.
  4. As you are working, make sure that yous relieve your file opening script(southward) to this directory.

The File Setup

Allow's open and examine the structure of the file Plates_output_simple.csv. If you open the file in a text editor, you volition encounter that the file contains several lines of text.

DataFileRaw

However, this is fairly difficult to read. If you open up the file in a spreadsheet program such equally LibreOfficeCalc or Excel, you tin can see that the file is organized into columns, with each cavalcade separated by the commas in the epitome in a higher place (hence the file extension .csv, which stands for comma-separated values).

DataFileColumns

The file contains i header row, followed by viii rows of data. Each row represents a single plate image. If we expect at the column headings, we tin run into that nosotros have collected information for each plate:

  • The name of the image from which the information was collected
  • The plate number (there were 4 plates, with each plate imaged at two unlike time points)
  • The growth condition (either control or experimental)
  • The ascertainment timepoint (either 24 or 48 hours)
  • Colony count for the plate
  • The boilerplate colony size for the plate
  • The percent of the plate covered by bacterial colonies

Nosotros will read in this data file then work to analyze the data.

Opening and reading files is a 3-footstep process

We will open and read the file in 3 steps.

  1. Nosotros will create a variable to concur the name of the file that we want to open.
  2. We will phone call a open to open the file.
  3. We will call a office to actually read the data in the file and store it in a variable so that nosotros can process it.

And so, in that location'south one more pace to exercise!

  • When we are done, we should remember to close the file!

You can think of these three steps as beingness like to checking out a volume from the library. First, you have to go to the catalog or database to find out which book you need (the filename). And so, you lot have to become and become it off the shelf and open up the book up (the open office). Finally, to gain any data from the book, you take to read the words (the read function)!

Here is an case of opening, reading, and closing a file.

                          #Create a variable for the file name              filename              =              'Plates_output_simple.csv'              #This is simply a string of text              #Open the file              infile              =              open up              (              filename              ,              'r'              )              # 'r' says nosotros are opening the file to read, infile is the opened file object that nosotros will read from              #Store the data from the file in a variable              data              =              infile              .              read              ()              #Impress the data in the file              impress              (              data              )              #close the file              infile              .              shut              ()                      

In one case we have read the information in the file into our variable data, we can treat it like any other variable in our code.

Use consistent names to make your code clearer

It is a good idea to develop some consistent habits about the mode you open up and read files. Using the same (or similar!) variable names each time will make it easier for you to keep rail of which variable is the name of the file, which variable is the opened file object, and which variable contains the read-in data.

In these examples, nosotros will use filename for the text cord containing the file proper noun, infile for the open up file object from which we can read in data, and data for the variable holding the contents of the file.

Commands for reading in files

There are a variety of commands that allow the states to read in information from files.
infile.read() will read in the unabridged file as a single cord of text.
infile.readline() will read in one line at a time (each time you lot phone call this command, information technology reads in the adjacent line).
infile.readlines() volition read all of the lines into a listing, where each line of the file is an particular in the list.

Mixing these commands can take some unexpected results.

                          #Create a variable for the file name              filename              =              'Plates_output_simple.csv'              #Open the file              infile              =              open              (              filename              ,              'r'              )              #Print the beginning two lines of the file              print              (              infile              .              readline              ())              print              (              infile              .              readline              ())              #call infile.read()              print              (              infile              .              read              ())              #close the file              infile              .              close              ()                      

Notice that the infile.read()command started at the tertiary line of the file, where the first two infile.readline() commands left off.

Think of information technology like this: when the file is opened, a pointer is placed at the meridian left corner of the file at the beginning of the start line. Any time a read function is called, the cursor or arrow advances from where it already is. The first infile.readline() started at the beginning of the file and advanced to the end of the beginning line. At present, the pointer is positioned at the outset of the 2d line. The second infile.readline() avant-garde to the end of the 2d line of the file, and left the arrow positioned at the beginning of the third line. infile.read() began from this position, and advanced through to the finish of the file.

In general, if you desire to switch between the unlike kinds of read commands, y'all should close the file and then open up it once again to start over.

Reading all of the lines of a file into a listing

infile.readlines() will read all of the lines into a list, where each line of the file is an item in the list. This is extremely useful, because in one case we have read the file in this way, we can loop through each line of the file and process information technology. This approach works well on data files where the data is organized into columns similar to a spreadsheet, considering it is likely that we will desire to handle each line in the same way.

The example below demonstrates this approach:

                          #Create a variable for the file name              filename              =              "Plates_output_simple.csv"              #Open the file              infile              =              open              (              filename              ,              'r'              )              lines              =              infile              .              readlines              ()              for              line              in              lines              :              #lines is a list with each item representing a line of the file              if              'control'              in              line              :              print              (              line              )              #print lines for control status              infile              .              close              ()              #shut the file when you're done!                      

Using .split() to carve up "columns"

Since our data is in a .csv file, we tin utilise the split command to split each line of the file into a list. This can be useful if we want to admission specific columns of the file.

                          #Create a variable for the file name                            filename              =              "Plates_output_simple.csv"              #Open the file              infile              =              open              (              filename              ,              'r'              )              lines              =              infile              .              readlines              ()              for              line              in              lines              :              sline              =              line              .              split              (              ','              )              # separates line into a listing of items.  ',' tells information technology to divide the lines at the commas              print              (              sline              )              #each line is now a list              infile              .              close              ()              #Always close the file!                      

Consequent names, again

At first glance, the variable proper noun sline in the case above may not brand much sense. In fact, nosotros chose information technology to exist an abbreviation for "dissever line", which exactly describes the contents of the variable.

Yous don't have to use this naming convention if you don't want to, but you should piece of work to use consistent variable names across your lawmaking for mutual operations similar this. Information technology will make it much easier to open an old script and rapidly understand exactly what it is doing.

Converting text to numbers

When we chosen the readlines() command in the previous code, Python reads in the contents of the file as a string. If we want our code to recognize something in the file every bit a number, we need to tell it this!

For case, float('5.0') will tell Python to treat the text string '5.0' equally the number 5.0. int(sline[4]) will tell our lawmaking to treat the text cord stored in the 5th position of the list sline as an integer (non-decimal) number.

For each line in the file, the ColonyCount is stored in the 5th column (index 4 with our 0-based counting).
Modify the code above to print the line but if the ColonyCount is greater than 30.

Solution

                                  #Create a variable for the file proper noun                  filename                  =                  'Plates_output_simple.csv'                  ##Open the file                  infile                  =                  open up                  (                  filename                  ,                  'r'                  )                  lines                  =                  infile                  .                  readlines                  ()                  for                  line                  in                  lines                  [                  ane                  :]:                  #skip the outset line, which is the header                  sline                  =                  line                  .                  split                  (                  ','                  )                  # separates line into a listing of items.  ',' tells it to separate the lines at the commas                  colonyCount                  =                  int                  (                  sline                  [                  4                  ])                  #store the colony count for the line every bit an integer                  if                  colonyCount                  >                  thirty                  :                  print                  (                  sline                  )                  #close the file                  infile                  .                  close                  ()                              

Writing data out to a file

Often, nosotros will want to write data to a new file. This is especially useful if nosotros accept done a lot of computations or data processing and we want to be able to save it and come dorsum to it afterward.

Writing a file is the same multi-stride process

Just similar reading a file, we will open up and write the file in multiple steps.

  1. Create a variable to hold the proper noun of the file that we desire to open up. Often, this will be a new file that doesn't still exist.
  2. Call a function to open the file. This time, we will specify that we are opening the file to write into it!
  3. Write the data into the file. This requires some careful attention to formatting.
  4. When nosotros are done, we should remember to close the file!

The code below gives an instance of writing to a file:

                          filename              =              "output.txt"              #west tells python we are opening the file to write into it              outfile              =              open up              (              filename              ,              'west'              )              outfile              .              write              (              "This is the first line of the file"              )              outfile              .              write              (              "This is the second line of the file"              )              outfile              .              close              ()              #Close the file when nosotros're done!                      

Where did my file end upwards?

Any time you open a new file and write to it, the file will be saved in your current working directory, unless you specified a different path in the variable filename.

Newline characters

When y'all examine the file you merely wrote, you volition see that all of the text is on the same line! This is because we must tell Python when to showtime on a new line by using the special string character '\n'. This newline character volition tell Python exactly where to start each new line.

The example below demonstrates how to utilise newline characters:

                          filename              =              'output_newlines.txt'              #w tells python nosotros are opening the file to write into it              outfile              =              open              (              filename              ,              'w'              )              outfile              .              write              (              "This is the first line of the file              \n              "              )              outfile              .              write              (              "This is the 2d line of the file              \n              "              )              outfile              .              shut              ()              #Close the file when nosotros're done!                      

Get open the file you only wrote and and check that the lines are spaced correctly.:

Dealing with newline characters when yous read a file

Yous may accept noticed in the final file reading example that the printed output included newline characters at the stop of each line of the file:

['colonies02.tif', '2', 'exp', '24', '84', '3.2', '22\n']
['colonies03.tif', 'three', 'exp', '24', '792', 'three', '78\northward']
['colonies06.tif', '2', 'exp', '48', '85', '5.2', '46\n']

We can get rid of these newlines by using the .strip() function, which will get rid of newline characters:

                              #Create a variable for the file name                filename                =                'Plates_output_simple.csv'                ##Open up the file                infile                =                open                (                filename                ,                'r'                )                lines                =                infile                .                readlines                ()                for                line                in                lines                [                1                :]:                #skip the starting time line, which is the header                sline                =                line                .                strip                ()                #get rid of trailing newline characters at the cease of the line                sline                =                sline                .                split                (                ','                )                # separates line into a list of items.  ',' tells information technology to split the lines at the commas                colonyCount                =                int                (                sline                [                iv                ])                #store the colony count for the line as an integer                if                colonyCount                >                xxx                :                impress                (                sline                )                #close the file                infile                .                close                ()                          

Writing numbers to files

Just like Python automatically reads files in as strings, the write()role expects to simply write strings. If nosotros desire to write numbers to a file, nosotros will need to "bandage" them as strings using the role str().

The code below shows an case of this:

                          numbers              =              range              (              0              ,              10              )              filename              =              "output_numbers.txt"              #w tells python nosotros are opening the file to write into it              outfile              =              open up              (              filename              ,              'due west'              )              for              number              in              numbers              :              outfile              .              write              (              str              (              number              ))              outfile              .              shut              ()              #Close the file when nosotros're done!                      

Writing new lines and numbers

Go open and examine the file you lot just wrote. You will see that all of the numbers are written on the same line.

Modify the code to write each number on its ain line.

Solution

                                  numbers                  =                  range                  (                  0                  ,                  10                  )                  #Create the range of numbers                  filename                  =                  "output_numbers.txt"                  #provide the file name                  #open the file in 'write' mode                  outfile                  =                  open                  (                  filename                  ,                  'w'                  )                  for                  number                  in                  numbers                  :                  outfile                  .                  write                  (                  str                  (                  number                  )                  +                  '                  \n                  '                  )                  outfile                  .                  close                  ()                  #Close the file when nosotros're done!                              

The file yous just wrote should be saved in your Working Directory. Open the file and check that the output is correctly formatted with ane number on each line.

Opening files in different 'modes'

When we have opened files to read or write information, we have used the part parameter 'r' or 'w' to specify which "way" to open the file.
'r' indicates we are opening the file to read data from it.
'w' indicates we are opening the file to write data into information technology.

Be very, very careful when opening an existing file in 'westward' mode.
'westward' volition over-write whatsoever data that is already in the file! The overwritten data will be lost!

If you lot want to add on to what is already in the file (instead of erasing and over-writing it), you can open the file in append style by using the 'a' parameter instead.

Pulling it all together

Read in the data from the file Plates_output_simple.csv that we have been working with. Write a new csv-formatted file that contains only the rows for control plates.
You will need to do the following steps:

  1. Open the file.
  2. Use .readlines() to create a list of lines in the file. Then close the file!
  3. Open a file to write your output into.
  4. Write the header line of the output file.
  5. Employ a for loop to let you to loop through each line in the list of lines from the input file.
  6. For each line, check if the growth condition was experimental or control.
  7. For the control lines, write the line of information to the output file.
  8. Close the output file when you lot're done!

Solution

Here'southward one way to exercise it:

                                  #Create a variable for the file proper name                  filename                  =                  'Plates_output_simple.csv'                  ##Open the file                  infile                  =                  open                  (                  filename                  ,                  'r'                  )                  lines                  =                  infile                  .                  readlines                  ()                  #We will process the lines of the file later                  #shut the input file                  infile                  .                  close                  ()                  #Create the file we volition write to                  filename                  =                  'ControlPlatesData.txt'                  outfile                  =                  open                  (                  filename                  ,                  'w'                  )                  outfile                  .                  write                  (                  lines                  [                  0                  ])                  #This will write the header line of the file                                    for                  line                  in                  lines                  [                  1                  :]:                  #skip the first line, which is the header                  sline                  =                  line                  .                  split                  (                  ','                  )                  # separates line into a list of items.  ',' tells it to split the lines at the commas                  status                  =                  sline                  [                  2                  ]                  #store the status for the line as a cord                  if                  status                  ==                  "control"                  :                  outfile                  .                  write                  (                  line                  )                  #The variable line is already formatted correctly!                  outfile                  .                  close                  ()                  #Close the file when we're done!                              

Challenge Problem

Open and read in the data from Plates_output_simple.csv. Write a new csv-formatted file that contains only the rows for the control status and includes but the columns for Fourth dimension, colonyCount, avgColonySize, and percentColonyArea. Hint: you can use the .join() part to join a list of items into a cord.

                              names                =                [                'Erin'                ,                'Mark'                ,                'Tessa'                ]                nameString                =                ', '                .                join                (                names                )                #the ', ' tells Python to join the listing with each item separated by a comma + space                print                (                nameString                )                          

'Erin, Mark, Tessa'

Solution

                                  #Create a variable for the input file proper name                  filename                  =                  'Plates_output_simple.csv'                  ##Open up the file                  infile                  =                  open                  (                  filename                  ,                  'r'                  )                  lines                  =                  infile                  .                  readlines                  ()                  #Nosotros will process the lines of the file after                  #shut the file                  infile                  .                  close                  ()                  # Create the file we will write to                  filename                  =                  'ControlPlatesData_Reduced.txt'                  outfile                  =                  open                  (                  filename                  ,                  'w'                  )                  #Write the header line                  headerList                  =                  lines                  [                  0                  ]                  .                  split                  (                  ','                  )[                  3                  :]                  #This will return the list of column headers from 'fourth dimension' on                  headerString                  =                  ','                  .                  join                  (                  headerList                  )                  #join the items in the list with commas                  outfile                  .                  write                  (                  headerString                  )                  #At that place is already a newline at the stop, so no need to add one                  #Write the remaining lines                  for                  line                  in                  lines                  [                  1                  :]:                  #skip the beginning line, which is the header                  sline                  =                  line                  .                  split                  (                  ','                  )                  # separates line into a list of items.  ',' tells it to split the lines at the commas                  condition                  =                  sline                  [                  2                  ]                  #shop the colony count for the line as an integer                  if                  condition                  ==                  "control"                  :                  dataList                  =                  sline                  [                  3                  :]                  dataString                  =                  ','                  .                  join                  (                  dataList                  )                  outfile                  .                  write                  (                  dataString                  )                  #The variable line is already formatted correctly!                  outfile                  .                  close                  ()                  #Shut the file when nosotros're done!                              

Key Points

  • Opening and reading a file is a multistep procedure: Defining the filename, opening the file, and reading the data

  • Information stored in files can be read in using a variety of commands

  • Writing data to a file requires attending to data types and formatting that isn't necessary with a print() statement

ruizcaus1978.blogspot.com

Source: https://eldoyle.github.io/PythonIntro/08-ReadingandWritingTextFiles/

0 Response to "Read Only Numbers From Text File Java Printout"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel