How Tyo Split While Reading From Files
Python is i of the well-nigh popular programming languages in the world. One reason for its popularity is that Python makes it piece of cake to piece of work with data.
Reading information from a text file is a routine task in Python. In this post, we're going to wait at the fastest manner to read and split a text file using Python. Splitting the data will convert the text to a list, making it easier to work with.
Nosotros'll also cover some other methods for splitting text files in Python, and explicate how and when these methods are useful.
In the following examples, we'll see how Python tin can assistance us master reading text information. Taking advantage of Python's many built-in functions volition simplify our tasks.
Introducing the split() method
The fastest way to split text in Python is with the carve up() method. This is a born method that is useful for separating a cord into its individual parts.
The split() method will return a listing of the elements in a string. By default, Python uses whitespace to split the cord, but yous can provide a delimiter and specify what grapheme(s) to use instead.
For instance, a comma(,) is often used to carve up string data. This is the case with Comma Separated Value (CSV) files. Whatever you choose as the separator, Python will utilize to dissever the string.
Splitting text file with the split() method
In our commencement example, we have a text file of employee data, including the names of employees, their phone numbers, and occupations.
We'll demand to write a Python programme that can read this randomly generated data and split up the data into lists.
employee_data.txt
Lana Anderson 485-3094-88 Electrician
Elian Johnston 751-5845-87 Interior Designer
Henry Johnston 777-6561-52 Astronomer
Dale Johnston 248-1843-09 Journalist
Luke Owens 341-7471-63 Teacher
Amy Perry 494-3532-17 Electrician
Chloe Baker 588-7165-01 Interior Designer
After using a Python with statement to open up the data file, nosotros can iterate through the file's contents with a for loop. Once the data is read, the split() method is used to separate the text into words.
In our case, the text is separated using whitespace, which is the default behavior of the divide() method.
Case 1: Splitting employee information with Python
with open("employee_data.txt",'r') every bit data_file: for line in data_file: data = line.split() print(data)
Output
['Lana', 'Anderson', '485-3094-88', 'Electrician'] ['Elian', 'Johnston', '751-5845-87', 'Interior', 'Designer'] ['Henry', 'Johnston', '777-6561-52', 'Astronomer'] ['Dale', 'Johnston', '248-1843-09', 'Journalist'] ['Luke', 'Owens', '341-7471-63', 'Instructor'] ['Amy', 'Perry', '494-3532-17', 'Electrician'] ['Chloe', 'Bakery', '588-7165-01', 'Interior', 'Designer']
Splitting strings with a comma
We provide an optional separator to the dissever() method to specify which character to divide the string with. The default delimiter is whitespace.
In the next example, we'll employ a comma to split test score data read from a file.
grades.txt
Janet,100,50,69
Thomas,99,76,100
Kate,102,78,65
Case two: Splitting grades with a comma
with open("grades.txt",'r') as file: for line in file: grade_data = line.strip().split(',') print(grade_data)
The strip() method is used here to remove the newline character (\northward) from the end of the lines.
Output
['Janet', '100', '50', '69'] ['Thomas', '99', '76', '100'] ['Kate', '102', '78', '65']
Splitting a text file with splitlines()
The splitlines() method is used to get a list of the lines in a text file. For the next examples, we'll pretend we run a website that'south dedicated to a theatre visitor. We're reading script information from text files and pushing information technology to the company's website.
juliet.txt
O Romeo, Romeo, wherefore art thou Romeo?
Deny thy father and refuse thy name.
Or if thou wilt not, be but sworn my love
And I'll no longer be a Capulet.
Nosotros can read the file and dissever the lines into a list with the splitlines() method. Afterwards, a for loop tin can be used to print the contents of the text data.
Example three: Using splitlines() to read a text file
with open("juliet.txt",'r') as script: speech = script.read().splitlines() for line in speech: impress(line)
Using a Generator to Split a Text File
In Python, a generator is a special routine that tin exist used to create an array. A generator is like to a function that returns an array, but information technology does and then i element at a fourth dimension.
Generators use the yield keyword. When Python encounters a yield statement, it stores the state of the part until subsequently, when the generator is called again.
In the next instance, we'll use a generator to read the beginning of Romeo's famous voice communication from Shakespeare'due south Romeo and Juliet. Using the yield keyword ensures that the state of our while loop is saved during each iteration. This can be useful when working with large files.
romeo.txt
But soft, what light through yonder window breaks?
It is the east, and Juliet is the sun.
Arise, fair sun, and kill the envious moon,
Who is already sick and pale with grief
That thou, her maid, art far more than fair than she.
Example 4: Splitting a text file with a generator
def generator_read(file_name): file = open(file_name,'r') while True: line = file.readline() if not line: file.close() intermission yield line file_data = generator_read("romeo.txt") for line in file_data: impress(line.split())
Reading File Data with List Comprehension
Python listing comprehension provides an elegant solution for working with lists. Nosotros can accept reward of shorter syntax to write our code with listing comprehension. In add-on, listing comprehension statements are usually easier to read.
In our previous examples, nosotros've had to utilize a for loop to read the text files. We can exchange our for loop for a unmarried line of lawmaking using listing comprehension.
Listing Comprehension Syntax:
my_list = [expression for element in list]
One time the information has been obtained via list comprehension, we use the split() method to dissever the lines and add them to a new listing.
Using the aforementioned romeo.txt file from the previous example, let's see how listing comprehension can provide a more elegant approach to splitting a text file in Python.
Example 5: Using listing comprehension to read file data
with open up("romeo.txt",'r') equally file: lines = [line.strip() for line in file] for line in lines: print(line.divide())
Separate a Text File into Multiple Smaller Files
What if we accept a big file that we'd like to split into smaller files? We split a large file in Python using for loops and slicing.
With list slicing, nosotros tell Python we desire to work with a specific range of elements from a given list. This is done by providing a kickoff betoken and end point for the slice.
In Python, a list tin can be sliced using a colon. In the post-obit example, we'll apply list slicing to carve up a text file into multiple smaller files.
Split a File with Listing Slicing
A list can be split up using Python listing slicing. To exercise so, we commencement read the file using the readlines() method. Next, the top half of the file is written to a new file chosen romeo_A.txt. We'll apply list slicing within this for loop to write the first half of the original file to a new file.
Using a second for loop, we'll write the rest of the text to some other file. In order to perform the slice, we need the len() method to find the full number of lines in the original file.
Lastly, the int() method is used to convert the effect of the division to an integer value.
Example half dozen: Splitting a single text file into multiple text files
with open("romeo.txt",'r') as file: lines = file.readlines() with open up("romeo_A.txt",'due west') every bit file: for line in lines[:int(len(lines)/2)]: file.write(line) with open("romeo_B.txt",'w') equally file: for line in lines[int(len(lines)/two):]: file.write(line)
Running this program in the aforementioned directory every bit romeo.txt volition create the following text files.
romeo_A.txt
Simply soft, what lite through yonder window breaks?
Information technology is the e, and Juliet is the lord's day.
romeo_B.txt
Arise, fair sun, and kill the envious moon,
Who is already ill and pale with grief
That chiliad, her maid, fine art far more fair than she.
Related Posts
Nosotros've seen how to use the split() method to split a text file. Additionally, our examples accept shown how split() is used in tandem with Python generators and list comprehension to read large files more than elegantly.
Taking advantage of Python'due south many congenital-in methods, such equally split() and readlines(), allows us to process text files more rapidly. Using these tools volition salve the states time and try.
If yous're serious about mastering Python, information technology'southward a good thought to invest some time in learning how to use these methods to ready your own solutions.
If you'd like to acquire more than about programming with Python, please visit the following tutorials from Python for Beginners.
- How a Python annotate can make or break your plan
- Turbo charge your code with Python list comprehension
Recommended Python Grooming
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Acquire how to create real globe applications and master the nuts.
Source: https://www.pythonforbeginners.com/files/the-fastest-way-to-split-a-text-file-using-python
0 Response to "How Tyo Split While Reading From Files"
Post a Comment