How to Read in a File in Python and Use Split

Python is one of the most popular programming languages in the world. Ane reason for its popularity is that Python makes information technology easy to work with data.

Reading data from a text file is a routine chore in Python. In this post, we're going to look at the fastest way to read and split a text file using Python. Splitting the data volition convert the text to a list, making it easier to piece of work with.

We'll also cover another methods for splitting text files in Python, and explain how and when these methods are useful.

In the following examples, we'll run across how Python can help us master reading text information. Taking advantage of Python'due south many built-in functions will simplify our tasks.

Introducing the split() method

The fastest way to split text in Python is with the split() method. This is a congenital-in method that is useful for separating a string into its individual parts.

The split() method will return a list of the elements in a string. By default, Python uses whitespace to separate the string, merely you lot can provide a delimiter and specify what grapheme(s) to utilise instead.

For instance, a comma(,) is often used to carve up string data. This is the example with Comma Separated Value (CSV) files. Whatever you choose equally the separator, Python will use to dissever the string.

Splitting text file with the split() method

In our first case, we have a text file of employee data, including the names of employees, their telephone numbers, and occupations.

Nosotros'll need to write a Python program that can read this randomly generated information 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

Subsequently using a Python with argument to open the data file, we can iterate through the file'due south contents with a for loop. Once the information is read, the split() method is used to separate the text into words.

In our example, the text is separated using whitespace, which is the default behavior of the split() method.

Instance ane: Splitting employee data with Python

            with open up("employee_data.txt",'r') equally data_file:     for line in data_file:         data = line.divide()         print(information)                      

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', 'Announcer'] ['Luke', 'Owens', '341-7471-63', 'Teacher'] ['Amy', 'Perry', '494-3532-17', 'Electrician'] ['Chloe', 'Baker', '588-7165-01', 'Interior', 'Designer']                      

Splitting strings with a comma

We provide an optional separator to the separate() method to specify which character to split the string with. The default delimiter is whitespace.

In the side by side example, nosotros'll use a comma to carve up test score information read from a file.

grades.txt
Janet,100,50,69
Thomas,99,76,100
Kate,102,78,65

Instance 2: 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 hither to remove the newline character (\n) from the terminate of the lines.

Output

            ['Janet', '100', 'l', '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 adjacent examples, nosotros'll pretend we run a website that's dedicated to a theatre visitor. We're reading script data from text files and pushing it to the company's website.

juliet.txt
O Romeo, Romeo, wherefore art thou Romeo?
Deny thy father and reject thy name.
Or if 1000 wilt non, be simply sworn my love
And I'll no longer be a Capulet.

Nosotros can read the file and carve up the lines into a listing with the splitlines() method. Afterwards, a for loop can be used to print the contents of the text information.

Example iii: Using splitlines() to read a text file

            with open("juliet.txt",'r') equally 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 be used to create an array. A generator is similar to a office that returns an assortment, simply it does so 1 element at a fourth dimension.

Generators use the yield keyword. When Python encounters a yield statement, it stores the state of the function until later, when the generator is chosen again.

In the next case, we'll use a generator to read the outset of Romeo'due south famous voice communication from Shakespeare's Romeo and Juliet. Using the yield keyword ensures that the state of our while loop is saved during each iteration. This tin be useful when working with large files.

romeo.txt
Merely soft, what light through yonder window breaks?
It is the e, and Juliet is the sun.
Ascend, fair sun, and impale the envious moon,
Who is already ill and pale with grief
That 1000, her maid, art far more off-white than she.

Instance 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()             pause         yield line  file_data = generator_read("romeo.txt") for line in file_data:     impress(line.carve up())                      

Reading File Data with List Comprehension

Python list comprehension provides an elegant solution for working with lists. We can take advantage of shorter syntax to write our code with list comprehension. In addition, list comprehension statements are unremarkably easier to read.

In our previous examples, nosotros've had to utilise a for loop to read the text files. Nosotros tin can exchange our for loop for a single line of code using list comprehension.

Listing Comprehension Syntax:
my_list = [expression for element in listing]

In one case the data has been obtained via list comprehension, we use the split() method to split the lines and add them to a new list.

Using the same romeo.txt file from the previous case, let's see how list comprehension tin can provide a more elegant approach to splitting a text file in Python.

Example 5: Using list 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.split())                      

Split a Text File into Multiple Smaller Files

What if we have a large file that nosotros'd like to split into smaller files? We dissever a large file in Python using for loops and slicing.

With list slicing, we tell Python we want to piece of work with a specific range of elements from a given list. This is done by providing a start betoken and end indicate for the slice.

In Python, a list can be sliced using a colon. In the following instance, we'll use list slicing to split a text file into multiple smaller files.

Split a File with List Slicing

A list can exist split using Python list slicing. To do so, we starting time read the file using the readlines() method. Side by side, the peak half of the file is written to a new file chosen romeo_A.txt. We'll employ list slicing within this for loop to write the first one-half of the original file to a new file.

Using a second for loop, nosotros'll write the rest of the text to another file. In society to perform the slice, nosotros need the len() method to find the total number of lines in the original file.

Lastly, the int() method is used to convert the result of the division to an integer value.

Example half dozen: Splitting a single text file into multiple text files

            with open up("romeo.txt",'r') as file:     lines = file.readlines()  with open up("romeo_A.txt",'w') as file:     for line in lines[:int(len(lines)/2)]:         file.write(line)  with open up("romeo_B.txt",'w') every bit file:     for line in lines[int(len(lines)/2):]:         file.write(line)                      

Running this program in the same directory every bit romeo.txt will create the following text files.

romeo_A.txt
Just soft, what light through yonder window breaks?
It is the east, and Juliet is the dominicus.

romeo_B.txt
Ascend, fair sun, and kill the envious moon,
Who is already sick and pale with grief
That 1000, her maid, art far more fair than she.

Related Posts

We've seen how to use the divide() method to split up a text file. Additionally, our examples have shown how divide() is used in tandem with Python generators and listing comprehension to read large files more elegantly.

Taking advantage of Python'southward many built-in methods, such as split() and readlines(), allows usa to process text files more speedily. Using these tools will save us time and try.

If you're serious about mastering Python, it's a good idea to invest some time in learning how to use these methods to prepare your own solutions.

If you'd like to learn more about programming with Python, delight visit the following tutorials from Python for Beginners.

  • How a Python comment can make or break your programme
  • Turbo charge your code with Python listing comprehension

Recommended Python Grooming

Course: Python iii For Beginners

Over xv hours of video content with guided instruction for beginners. Learn how to create existent earth applications and master the basics.

palaciowassibly.blogspot.com

Source: https://www.pythonforbeginners.com/files/the-fastest-way-to-split-a-text-file-using-python

0 Response to "How to Read in a File in Python and Use Split"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel