Skip to content Skip to sidebar Skip to footer

How to Read and Store Every Other Line in C++

Reading and Writing Text Files

Overview

Teaching: 60 min
Exercises: 30 min

Questions

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

Objectives

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

  • Empathise the deviation between the file proper noun, the opened file object, and the data read in from the file

  • Be able to write output to a text file with simple formatting

Why do nosotros want to read and write files?

Being able to open up and read in files allows us to work with larger data sets, where it wouldn't exist possible to blazon in each and every value and store them one-at-a-time as variables. Writing files allows us to process our data and then relieve the output to a file so we tin wait at it later.

Right now, nosotros volition practice working with a comma-delimited text file (.csv) that contains several columns of data. However, what you learn in this lesson can be applied to whatever full general text file. In the adjacent lesson, y'all volition learn another way to read and procedure .csv data.

Paths to files

In order to open a file, we need to tell Python exactly where the file is located, relative to where Python is currently working (the working directory). In Spyder, we tin can do this by setting our 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 will piece of work with the practice file Plates_output_simple.csv.

  1. Locate the file Plates_output_simple.csv in the directory home/Desktop/workshops/fustigate-git-python.
  2. Copy the file to your working directory, abode/Desktop/workshops/YourName.
  3. Brand sure that your working directory is also set to the binder home/Desktop/workshops/YourName.
  4. As you are working, make sure that yous save your file opening script(s) to this directory.

The File Setup

Let'due south open and examine the construction of the file Plates_output_simple.csv. If you open up the file in a text editor, you lot will see that the file contains several lines of text.

DataFileRaw

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

DataFileColumns

The file contains one header row, followed by 8 rows of data. Each row represents a single plate paradigm. If we look at the column headings, we tin meet that we have nerveless data for each plate:

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

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

Opening and reading files is a three-stride process

We will open and read the file in three steps.

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

So, there'south one more step to practice!

  • When we are washed, nosotros should call back to shut the file!

You can think of these three steps as being similar to checking out a book from the library. First, you have to get to the catalog or database to find out which volume you need (the filename). Then, you take to become and become it off the shelf and open the book up (the open role). Finally, to gain whatsoever information from the book, you take to read the words (the read function)!

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

                          #Create a variable for the file name              filename              =              'Plates_output_simple.csv'              #This is simply a cord of text              #Open up the file              infile              =              open              (              filename              ,              'r'              )              # 'r' says we 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              information              =              infile              .              read              ()              #Impress the information in the file              print              (              information              )              #close the file              infile              .              close              ()                      

Once nosotros have read the data in the file into our variable data, nosotros can treat information technology like any other variable in our code.

Use consistent names to make your code clearer

It is a skillful thought to develop some consequent habits about the style you lot open up and read files. Using the same (or similar!) variable names each time will make it easier for y'all to keep runway of which variable is the proper name of the file, which variable is the opened file object, and which variable contains the read-in data.

In these examples, we volition utilize filename for the text string containing the file proper noun, infile for the open file object from which we can read in information, and data for the variable holding the contents of the file.

Commands for reading in files

At that place are a variety of commands that allow us to read in data from files.
infile.read() will read in the entire file as a single string of text.
infile.readline() will read in one line at a time (each time y'all call this command, it reads in the next line).
infile.readlines() will read all of the lines into a listing, where each line of the file is an item in the list.

Mixing these commands tin have some unexpected results.

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

Observe that the infile.read()command started at the third line of the file, where the commencement two infile.readline() commands left off.

Recollect of information technology similar this: when the file is opened, a pointer is placed at the meridian left corner of the file at the beginning of the commencement line. Whatever time a read function is called, the cursor or pointer advances from where it already is. The first infile.readline() started at the beginning of the file and advanced to the finish of the first line. Now, the pointer is positioned at the beginning of the second line. The second infile.readline() advanced to the end of the 2nd line of the file, and left the pointer positioned at the beginning of the third line. infile.read() began from this position, and advanced through to the end of the file.

In full general, if you want to switch betwixt the unlike kinds of read commands, yous should close the file and and so open it again to beginning 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 once we take read the file in this way, we can loop through each line of the file and process it. This approach works well on data files where the information is organized into columns like to a spreadsheet, because information technology is likely that we will want to handle each line in the same mode.

The example below demonstrates this approach:

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

Using .separate() to separate "columns"

Since our information is in a .csv file, we tin use the split control 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              .              carve up              (              ','              )              # separates line into a list of items.  ',' tells information technology to split the lines at the commas              impress              (              sline              )              #each line is now a list              infile              .              close              ()              #Always close the file!                      

Consistent names, again

At get-go glance, the variable name sline in the case in a higher place may not make much sense. In fact, we chose information technology to be an abbreviation for "split line", which exactly describes the contents of the variable.

You don't have to utilise this naming convention if you don't want to, but yous should work to apply consistent variable names across your code for mutual operations like this. It will go far much easier to open up an old script and speedily empathize exactly what it is doing.

Converting text to numbers

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

For example, bladder('five.0') volition tell Python to treat the text string '5.0' equally the number 5.0. int(sline[4]) volition tell our code to treat the text string stored in the 5th position of the listing sline every bit an integer (not-decimal) number.

For each line in the file, the ColonyCount is stored in the fifth column (index 4 with our 0-based counting).
Change the code higher up to print the line merely if the ColonyCount is greater than 30.

Solution

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

Writing data out to a file

Oft, nosotros will want to write data to a new file. This is especially useful if we have done a lot of computations or data processing and we desire to exist able to salve information technology and come dorsum to it later.

Writing a file is the aforementioned multi-step process

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

  1. Create a variable to concur the name of the file that we want to open. Often, this will be a new file that doesn't still exist.
  2. Call a part to open the file. This time, nosotros will specify that nosotros are opening the file to write into it!
  3. Write the data into the file. This requires some careful attention to formatting.
  4. When we are washed, nosotros should remember to shut the file!

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

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

Where did my file end upwards?

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

Newline characters

When you examine the file you but wrote, you will see that all of the text is on the aforementioned line! This is because we must tell Python when to commencement on a new line past using the special cord character '\n'. This newline character will tell Python exactly where to start each new line.

The example beneath demonstrates how to use newline characters:

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

Go open up the file you merely wrote and and check that the lines are spaced correctly.:

Dealing with newline characters when you read a file

You may have noticed in the concluding file reading example that the printed output included newline characters at the terminate of each line of the file:

['colonies02.tif', 'ii', 'exp', '24', '84', '3.2', '22\n']
['colonies03.tif', 'three', 'exp', '24', '792', '3', '78\n']
['colonies06.tif', '2', 'exp', '48', '85', 'v.2', '46\due north']

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

                              #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                [                1                :]:                #skip the first line, which is the header                sline                =                line                .                strip                ()                #get rid of trailing newline characters at the cease of the line                sline                =                sline                .                carve up                (                ','                )                # separates line into a list of items.  ',' tells information technology to split the lines at the commas                colonyCount                =                int                (                sline                [                four                ])                #shop the colony count for the line as an integer                if                colonyCount                >                xxx                :                print                (                sline                )                #close the file                infile                .                close                ()                          

Writing numbers to files

Just like Python automatically reads files in as strings, the write()part expects to only write strings. If nosotros want to write numbers to a file, we volition need to "bandage" them as strings using the function str().

The code below shows an instance of this:

                          numbers              =              range              (              0              ,              x              )              filename              =              "output_numbers.txt"              #westward tells python we are opening the file to write into it              outfile              =              open              (              filename              ,              'w'              )              for              number              in              numbers              :              outfile              .              write              (              str              (              number              ))              outfile              .              shut              ()              #Shut the file when we're done!                      

Writing new lines and numbers

Go open and examine the file you simply wrote. You will run across that all of the numbers are written on the same line.

Modify the lawmaking to write each number on its own line.

Solution

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

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

Opening files in different 'modes'

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

Exist very, very careful when opening an existing file in 'w' mode.
'w' will over-write any data that is already in the file! The overwritten information volition be lost!

If you want to add on to what is already in the file (instead of erasing and over-writing information technology), you can open the file in append mode past using the 'a' parameter instead.

Pulling it all together

Read in the data from the file Plates_output_simple.csv that nosotros 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. Apply a for loop to allow yous to loop through each line in the listing 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 data to the output file.
  8. Shut the output file when yous're washed!

Solution

Here's one way to practise information technology:

                                  #Create a variable for the file proper noun                  filename                  =                  'Plates_output_simple.csv'                  ##Open up the file                  infile                  =                  open up                  (                  filename                  ,                  'r'                  )                  lines                  =                  infile                  .                  readlines                  ()                  #We will process the lines of the file later on                  #shut the input file                  infile                  .                  close                  ()                  #Create the file we will 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                  [                  one                  :]:                  #skip the get-go line, which is the header                  sline                  =                  line                  .                  split                  (                  ','                  )                  # separates line into a list of items.  ',' tells it to carve up the lines at the commas                  condition                  =                  sline                  [                  2                  ]                  #store the condition for the line every bit a cord                  if                  condition                  ==                  "control"                  :                  outfile                  .                  write                  (                  line                  )                  #The variable line is already formatted correctly!                  outfile                  .                  shut                  ()                  #Close the file when nosotros're washed!                              

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 only the columns for Fourth dimension, colonyCount, avgColonySize, and percentColonyArea. Hint: you can utilise the .join() office to bring together a list of items into a string.

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

'Erin, Marking, Tessa'

Solution

                                  #Create a variable for the input file name                  filename                  =                  'Plates_output_simple.csv'                  ##Open the file                  infile                  =                  open                  (                  filename                  ,                  'r'                  )                  lines                  =                  infile                  .                  readlines                  ()                  #We will process the lines of the file afterwards                  #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                  ]                  .                  separate                  (                  ','                  )[                  3                  :]                  #This will return the list of column headers from 'time' on                  headerString                  =                  ','                  .                  bring together                  (                  headerList                  )                  #join the items in the list with commas                  outfile                  .                  write                  (                  headerString                  )                  #In that location is already a newline at the terminate, and then no need to add one                  #Write the remaining lines                  for                  line                  in                  lines                  [                  ane                  :]:                  #skip the outset 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 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                  ()                  #Close the file when we're washed!                              

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 tin 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

babinsuble1994.blogspot.com

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

Post a Comment for "How to Read and Store Every Other Line in C++"