Hướng dẫn split text file python

I am working on text files like this:

Chapter 01

Lorem ipsum

dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt

Chapter 02

consectetur adipiscing

sed do eiusmod tempor

Chapter 03

et dolore magna aliqua.

with delimiters like "chapter", "Chapter", "CHAPTER", etc... and 1 or 2 digits ("Chapter 1" or "Chapter 01").

I managed to open and read the file in Python, with .open() and .read()

mytext = myfile.read()

Now I need to split my string, in order to get text for "Chapter XX".

For Chapter 02, that would be :

consectetur adipiscing

sed do eiusmod tempor

I'm new to Python, I read about regex, match, map, or split, but... well...

(I'm writing a Gimp Python-fu plugin, so I use Python version bundled in Gimp, which is 2.7.15).



Hàm split() trong Python chia chuỗi theo delimeter đã cho (là space nếu không được cung cấp) và trả về danh sách các chuỗi con; nếu bạn cung cấp đối số num thì chia chuỗi thành num + 1 chuỗi con.


Cú pháp

Cú pháp của split() trong Python:

str.split(str="", num=string.count(str))

Chi tiết về tham số:

  • str: Đây là bất kỳ phân chia chuỗi - delimeter nào, mặc định là khoảng trống.

  • num: Số chuỗi con num + 1 được tạo ra.


Ví dụ sau minh họa cách sử dụng của split() trong Python.

str1 = "Line1-Python Line2-Java Line3-PHP";

print("Test 1:");
arr1 = str1.split();
for arr in arr1:
    print (arr);

print("\nTest 2:");
arr1 = str1.split(' ', 1);
for arr in arr1:
    print (arr);

Chạy chương trình Python trên sẽ cho kết quả:

Test 1:
Line1-Python
Line2-Java
Line3-PHP

Test 2:
Line1-Python
Line2-Java Line3-PHP


You have the right idea with escaping the backslashes, but despite how it looks, your input string doesn't actually have any backslashes in it. You need to escape them in the input, too!

Nội dung chính

  • Python Programming
  • How do you print a backwards slash in Python?
  • How do you split text in Python?
  • How do you split a string with a dash in Python?

Nội dung chính

  • Python Programming
  • How do you print a backwards slash in Python?
  • How do you split text in Python?
  • How do you split a string with a dash in Python?
>>> a = "1\\2\\3\\4"  # Note the doubled backslashes here!
>>> print(a.split('\\'))  # Split on '\\'
['1', '2', '3', '4']

You could also use a raw string literal for the input, if it's likely to have many backslashes. This notation is much cleaner to look at (IMO), but it does have some limitations: read the docs!

>>> a = r"1\2\3\4"
>>> print(a.split('\\'))
['1', '2', '3', '4']

If you're getting a elsewhere, and a.split('\\') doesn't appropriately split on the visible backslashes, that means you've got something else in there instead of real backslashes. Try print(repr(a)) to see what the "literal" string actually looks like.

>>> a = '1\2\3\4'
>>> print(a)
1☻♥♦
>>> print(repr(a))
'1\x02\x03\x04'

>>> b = '1\\2\\3\\4'
>>> print(b)
1\2\3\4
>>> print(repr(b))
'1\\2\\3\\4'

Python Programming

s = r'canada\japan\australia'
l = s.split('\\')

print(l)

Output

['canada', 'japan', 'australia']

Check if given String is Palindrome in Python

Cutting and slicing strings and examples of substring

Convert String variable into float, int or boolean

Convert Camel Case to Snake Case and Change Case of a particular character in a given string

Reverse a string in different ways

Generate random string of N characters

How do I trim white space in Python?

Different ways to pad or add zeroes to string

How to check if the string is empty or blank?

Different ways to count the number of occurrences of a character in a string

How to check if string ends with one of the strings from a list?

How to Split Strings on Multiple Delimiters or specified characters?

How do I append one string to another?

How do I iterate over a string in Python?

How to strip punctuation from a string in Python?

How to convert a list into string?

How to convert JSON into String?

How to compare strings in Python?

How to check whether a string starts with XXXX in Python?

How to sort a list of strings?

Different ways to mesh or interleave two strings together in Python

How do I compare two strings in Python?

How to compare two string with some characters only in Python?

Why does comparing strings using either '==' or 'is' sometimes produce a different result?

How to compare individual characters in two strings in Python?

How can the indices of a string be compared for equality in Python?

How to compare a string with a Enum in Python?

String comparison in Python is vs ==

Comparing characters in a string in Python

Python compare two strings and check how many chars they have in common

How to check if a variable is equal to one string or another string?

Python compare two strings retain difference from one end

Checking if two strings contain the same characters in Python

Find the position of difference between two strings

How can I check if a string has the same characters in Python?

How to check if two strings contain same letters and numbers in Python?

How to use string.replace() in Python?

How to replace multiple substrings of a string in Python?

How to replace the first character alone in a string in Python?

How to replace specific instance of string in string in Python?

Python to replace strings starting from the end?

Concatenate strings in multiline in Python

Replacing a character from a certain index in Python?

Python string replace multiple occurrences

Python string replace character

How to concatenate a fixed string and a variable in Python

How to concatenate two variable one is string and other is int in Python?

Concatenate strings and variable values in Python

Append several variables to a list in Python

How can I concatenate str and int objects?

TypeError: cannot concatenate 'str' and 'float' objects Python

How to remove leading and trailing spaces in Python?

How do I trim whitespace in Python?

Trim specified number of spaces from left and right in Python?

Trim specific leading and trailing characters from a string in Python

How do I strip all leading and trailing punctuation in Python?

How to remove leading and trailing zeros in a string in Python?

Remove last character if it's a backslash in Python

How to capitalize a string in Python?

How to change a string into uppercase in Python?

How can I capitalize the first letter of each word in a string in Python?

Capitalize a string in Python

Randomly capitalize letters in string in Python

Capitalize first letter of the first word in a list in Python

How do I lowercase a string in Python?

Convert UPPERCASE string to sentence case in Python

Change some lowercase letters to uppercase in string in Python

Check if string is upper, lower, or mixed case in Python

How to capitalise last letter of string in Python?

How to capitalize first and last letters of line in a Python string?

How to capitalize every 3rd letter of string in Python?

Capitalizes a string according to the Index positions in Python

Capitalise every second letter in a string in Python?

Extract Uppercase and Lowercase characters from String Python?

Count number of Uppercase and Lowercase characters in String in Python?

How to get the size of a string in Python?

How to get size of string in bytes in Python

How can I fill out a Python string with spaces?

How to pad with n characters in Python?

Python padding strings of different length

Python String Padding

How to pad a numeric string with zeros to the right in Python?

How to pad zeroes to a string?

How to pad a string with leading zeros in Python3?

Dynamically calculated zero padding in format string in python

Format integer as string with leading zeros in Python?

Python string formatting padding negative numbers

How to add X number of spaces to a string in Python

Display string multiple times in Python

Python - How to add space on each 4th character?

Paragraph formatting in Python

Python regex add space whenever a number is adjacent to a non-number

How can I add space between parentheses and string in Python?

Add space between number and string in Python

Python string formatting fixed width

How to replace all characters in a string with one character in Python?

Find a whole word is in given string in Python?

Python pad string to fixed length

Python pad string with zeros

Efficient way to add spaces between characters in a string in Python

How to get the position of a character in Python?

How to find all positions of a character in a string in Python?

Find a substring in a string and returning the index of the substring in Python

How to find all occurrences of a substring?

Example of string find in Python

Python Find pattern in a string

Find index of last occurrence of a substring in a string in Python

Python find first occurrence of character after index

How to find all the indexes of all the occurrences of a word in a string in Python?

Python split string into multiple string

Split string on backslash in Python

Splits the string at every underscore and stop after the Nth position

How to split a string into an list of characters in Python?

Split string into two parts only in Python

Split a string only by first space in Python

Split string in Python to get first value?

Split a string on the last occurrence of the delimiter in the string in Python

Split a string by the position of a character in string in Python

Split string into strings by length in Python

Split string on forward slash in Python

Python split on newline

How to split string without losing split character in Python?

Split on non alpha-numeric and keep the separators in Python

Splitting a string with multiple delimiters in Python

Split string only after double quotes in Python

How to split a byte string into separate bytes in Python?

Split a string at the words AND OR NOT keeping the separators

How can I find the first occurrence of a sub-string in a Python string?

How to split a string by multiple punctuations with Python?

Split string on punctuation

Split Strings into words with multiple word boundary delimiters

Python count number of string appears in given string

Python center align the string using a specified character

Check if the string ends with given string or character in Python

Python set the tab size to the specified number of whitespaces

Check if all the characters in string are digits in Python

Find last business or working day of a month in Python

Follow @Pythonblogging

PYTHON TUTORIAL

Variables If…Else While Loop For Loops Lists Dictionary Tuples Classes and Objects Inheritance Method Overriding Operator Overloading NumPy

PYTHON EXAMPLES

Basic Date Time Strings Pandas Matplotlib NLP Object Oriented Programming Twitter Data Mining

TensorFlow BASIC

Introduction and Installation Hello World Tensors Tensor Calculations Computation Graph Variables

©2017-20 PythonProgramming.in

Drop us a line

To top

How do you print a backwards slash in Python?

Use the syntax "\\" within the string literal to represent a single backslash.

How do you split text in Python?

Use split() method to split by delimiter. If the argument is omitted, it will be split by whitespace, such as spaces, newlines \n , and tabs \t . Consecutive whitespace is processed together. A list of the words is returned.

How do you split a string with a dash in Python?

Use the str. split() method to split a string by hyphen, e.g. my_list = my_str. split('-') .