How to Read Words Separated by Symbol in Text File in C?
Reading and Writing Text Files
Overview
Didactics: 60 min
Exercises: 30 minQuestions
How tin I read in information that is stored in a file or write data out to a file?
Objectives
Be able to open up a file and read in the data stored in that file
Understand the deviation between the file name, the opened file object, and the information read in from the file
Exist able to write output to a text file with simple formatting
Why practice we want to read and write files?
Being able to open up and read in files allows u.s.a. to work with larger information sets, where it wouldn't be possible to blazon in each and every value and store them 1-at-a-time equally variables. Writing files allows united states to process our data and then save the output to a file and then we can look at it later.
Right now, we will practice working with a comma-delimited text file (.csv) that contains several columns of data. All the same, what you learn in this lesson tin can exist applied to any general text file. In the next lesson, y'all will larn another way to read and process .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 can do this by setting our electric current working directory to the folder where the file is located. Or, when we provide the file name, nosotros can requite a complete path to the file.
Lesson Setup
We will work with the do file Plates_output_simple.csv.
- Locate the file Plates_output_simple.csv in the directory abode/Desktop/workshops/bash-git-python.
- Re-create the file to your working directory, home/Desktop/workshops/YourName.
- Make sure that your working directory is also prepare to the folder dwelling house/Desktop/workshops/YourName.
- Every bit you are working, make sure that you save your file opening script(southward) to this directory.
The File Setup
Let's open and examine the structure of the file Plates_output_simple.csv. If you open the file in a text editor, you volition see that the file contains several lines of text.
However, this is adequately difficult to read. If y'all open the file in a spreadsheet program such as LibreOfficeCalc or Excel, you can see that the file is organized into columns, with each column separated past the commas in the image above (hence the file extension .csv, which stands for comma-separated values).
The file contains one header row, followed by eight rows of data. Each row represents a single plate image. If we look at the column headings, we tin meet that nosotros have nerveless data for each plate:
- The name of the image from which the data was collected
- The plate number (in that location were four 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 average colony size for the plate
- The percentage of the plate covered past bacterial colonies
We will read in this data file and then work to analyze the data.
Opening and reading files is a three-step procedure
We will open and read the file in iii steps.
- We will create a variable to concord the proper noun of the file that nosotros want to open.
- We will call a open to open the file.
- Nosotros will call a function to actually read the data in the file and store it in a variable so that we can process it.
And and then, at that place'south one more pace to exercise!
- When we are done, we should remember to close the file!
Yous can recall of these three steps as being like to checking out a volume from the library. First, you have to go to the catalog or database to find out which book y'all need (the filename). Then, you lot accept to go and become information technology off the shelf and open the book up (the open part). Finally, to gain whatsoever information from the volume, y'all have to read the words (the read part)!
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 string of text #Open the file infile = open ( filename , 'r' ) # 'r' says we are opening the file to read, infile is the opened file object that we will read from #Store the data from the file in a variable data = infile . read () #Print the data in the file impress ( data ) #close the file infile . shut ()
One time we have read the data in the file into our variable information, we can care for information technology like any other variable in our code.
Use consistent names to make your code clearer
It is a proficient idea to develop some consistent habits nearly the manner you lot open and read files. Using the same (or similar!) variable names each time will make information technology easier for yous to keep rails 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, nosotros volition utilise
filename
for the text string containing the file proper name,infile
for the open file object from which nosotros tin can read in data, anddata
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()
volition read in the unabridged file as a single string of text.
infile.readline()
will read in one line at a fourth dimension (each fourth dimension you telephone call this command, it reads in the adjacent line).
infile.readlines()
will read all of the lines into a list, where each line of the file is an item in the list.
Mixing these commands can 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 outset two lines of the file print ( infile . readline ()) print ( infile . readline ()) #phone call infile.read() print ( infile . read ()) #close the file infile . close ()
Notice that the infile.read()
command started at the third line of the file, where the offset ii infile.readline()
commands left off.
Retrieve of information technology like this: when the file is opened, a arrow is placed at the top left corner of the file at the beginning of the offset line. Whatever time a read function is called, the cursor or pointer advances from where it already is. The outset infile.readline()
started at the beginning of the file and advanced to the end of the outset line. Now, the pointer is positioned at the beginning of the second line. The 2nd infile.readline()
avant-garde to the terminate of the second 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 general, if you lot want to switch betwixt the dissimilar kinds of read commands, you should shut the file and and so open it once more to start over.
Reading all of the lines of a file into a list
infile.readlines()
volition read all of the lines into a list, where each line of the file is an detail in the list. This is extremely useful, because once nosotros have read the file in this way, we can loop through each line of the file and procedure it. This approach works well on information files where the data is organized into columns similar to a spreadsheet, because it is likely that we volition want 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 up the file infile = open up ( filename , 'r' ) lines = infile . readlines () for line in lines : #lines is a listing with each particular representing a line of the file if 'control' in line : print ( line ) #print lines for command condition infile . shut () #close the file when y'all're done!
Using .split()
to separate "columns"
Since our information is in a .csv file, we can use the split
command to separate each line of the file into a list. This can exist useful if nosotros want to access specific columns of the file.
#Create a variable for the file name filename = "Plates_output_simple.csv" #Open up the file infile = open up ( filename , 'r' ) lines = infile . readlines () for line in lines : sline = line . split up ( ',' ) # separates line into a listing of items. ',' tells information technology to carve up the lines at the commas impress ( sline ) #each line is now a list infile . shut () #Always close the file!
Consistent names, once more
At first glance, the variable name
sline
in the example above may not make much sense. In fact, we chose it to be an abbreviation for "split up line", which exactly describes the contents of the variable.Y'all don't take to use this naming convention if yous don't desire to, but you should work to use consequent variable names across your code for common operations like this. Information technology will brand it much easier to open up an old script and rapidly empathize exactly what it is doing.
Converting text to numbers
When nosotros called 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 as a number, we need to tell it this!For example,
float('5.0')
will tell Python to treat the text string 'v.0' as the number v.0.int(sline[4])
will tell our code to care for the text string 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 (alphabetize four with our 0-based counting).
Modify the lawmaking above to print the line only if the ColonyCount is greater than 30.Solution
#Create a variable for the file proper name filename = 'Plates_output_simple.csv' ##Open up the file infile = open ( filename , 'r' ) lines = infile . readlines () 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 colonyCount = int ( sline [ 4 ]) #store the colony count for the line as an integer if colonyCount > 30 : print ( sline ) #shut the file infile . close ()
Writing data out to a file
Often, we volition want to write information to a new file. This is especially useful if we have done a lot of computations or data processing and we want to be able to relieve it and come back to information technology afterward.
Writing a file is the same multi-step procedure
Simply like reading a file, we will open and write the file in multiple steps.
- Create a variable to hold the proper noun of the file that we desire to open. Often, this volition be a new file that doesn't however exist.
- Call a function to open the file. This fourth dimension, nosotros volition specify that nosotros are opening the file to write into it!
- Write the data into the file. This requires some conscientious attention to formatting.
- When nosotros are done, we should remember to shut the file!
The lawmaking below gives an example of writing to a file:
filename = "output.txt" #due west tells python we are opening the file to write into it outfile = open ( filename , 'w' ) 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 we're done!
Where did my file end up?
Whatever time you open a new file and write to information technology, the file volition exist saved in your current working directory, unless you specified a different path in the variable filename.
Newline characters
When you examine the file you lot just wrote, you volition see that all of the text is on the aforementioned line! This is because nosotros must tell Python when to start on a new line by using the special string graphic symbol '\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 we are opening the file to write into information technology outfile = open ( filename , 'w' ) outfile . write ( "This is the start line of the file \n " ) outfile . write ( "This is the second line of the file \n " ) outfile . close () #Close the file when we're done!
Go open the file you only 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 last file reading example that the printed output included newline characters at the finish of each line of the file:
['colonies02.tif', '2', 'exp', '24', '84', '3.2', '22\due north']
['colonies03.tif', '3', 'exp', '24', '792', 'iii', '78\n']
['colonies06.tif', 'ii', 'exp', '48', '85', 'five.2', '46\n']We tin get rid of these newlines past using the
.strip()
role, 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 up ( filename , 'r' ) lines = infile . readlines () for line in lines [ 1 :]: #skip the offset line, which is the header sline = line . strip () #get rid of trailing newline characters at the finish of the line sline = sline . dissever ( ',' ) # separates line into a list of items. ',' tells it to carve up 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 ) #shut the file infile . close ()
Writing numbers to files
Only like Python automatically reads files in as strings, the write()
role expects to only write strings. If we want to write numbers to a file, nosotros will need to "bandage" them every bit strings using the office str()
.
The lawmaking below shows an example of this:
numbers = range ( 0 , 10 ) filename = "output_numbers.txt" #west 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 washed!
Writing new lines and numbers
Go open and examine the file you lot just wrote. You will run into that all of the numbers are written on the aforementioned line.
Modify the code to write each number on its own 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 . shut () #Shut the file when we're done!
The file you just wrote should be saved in your Working Directory. Open the file and bank check that the output is correctly formatted with one number on each line.
Opening files in unlike 'modes'
When we take opened files to read or write data, nosotros have used the role parameter
'r'
or'w'
to specify which "way" to open the file.
'r'
indicates we are opening the file to read data from it.
'westward'
indicates we are opening the file to write data into it.Be very, very careful when opening an existing file in 'westward' mode.
'west'
will over-write any data that is already in the file! The overwritten information will be lost!If yous desire to add on to what is already in the file (instead of erasing and over-writing information technology), you can open up the file in append manner by using the
'a'
parameter instead.
Pulling it all together
Read in the data from the file Plates_output_simple.csv that we accept been working with. Write a new csv-formatted file that contains simply the rows for control plates.
You will need to exercise the post-obit steps:
- Open the file.
- Use
.readlines()
to create a listing of lines in the file. Then shut the file!- Open a file to write your output into.
- Write the header line of the output file.
- Use a for loop to allow you to loop through each line in the list of lines from the input file.
- For each line, cheque if the growth condition was experimental or control.
- For the control lines, write the line of data to the output file.
- Close the output file when yous're done!
Solution
Here's one mode to do information technology:
#Create a variable for the file proper noun filename = 'Plates_output_simple.csv' ##Open the file infile = open ( filename , 'r' ) lines = infile . readlines () #We volition procedure the lines of the file later #close the input file infile . close () #Create the file we will write to filename = 'ControlPlatesData.txt' outfile = open up ( filename , 'w' ) outfile . write ( lines [ 0 ]) #This will write the header line of the file for line in lines [ i :]: #skip the commencement 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 ] #store the condition for the line as a string if condition == "control" : outfile . write ( line ) #The variable line is already formatted correctly! outfile . close () #Close the file when nosotros're done!
Challenge Trouble
Open and read in the data from Plates_output_simple.csv. Write a new csv-formatted file that contains simply the rows for the control condition and includes only the columns for Time, colonyCount, avgColonySize, and percentColonyArea. Hint: you lot can apply the .bring together() part to join a list of items into a string.
names = [ 'Erin' , 'Mark' , 'Tessa' ] nameString = ', ' . join ( names ) #the ', ' tells Python to join the list with each item separated by a comma + space print ( nameString )
'Erin, Mark, 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 later #close the file infile . close () # Create the file we volition write to filename = 'ControlPlatesData_Reduced.txt' outfile = open ( filename , 'w' ) #Write the header line headerList = lines [ 0 ] . split ( ',' )[ iii :] #This will return the listing of cavalcade headers from 'time' on headerString = ',' . join ( headerList ) #bring together the items in the listing with commas outfile . write ( headerString ) #There is already a newline at the finish, so no need to add one #Write the remaining lines for line in lines [ 1 :]: #skip the first line, which is the header sline = line . divide ( ',' ) # separates line into a list of items. ',' tells it to split the lines at the commas condition = sline [ two ] #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 . shut () #Close the file when we're washed!
Key Points
Opening and reading a file is a multistep process: Defining the filename, opening the file, and reading the data
Data stored in files tin be read in using a diverseness of commands
Writing data to a file requires attention to data types and formatting that isn't necessary with a
print()
argument
Source: https://eldoyle.github.io/PythonIntro/08-ReadingandWritingTextFiles/
0 Response to "How to Read Words Separated by Symbol in Text File in C?"
Enregistrer un commentaire