简体   繁体   中英

How do you split a string at a specific point?

I am new to python and want to split what I have read in from a text file into two specific parts. Below is an example of what could be read in:

f = ['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]

So what I want to achieve is to be able to execute the second part of the program is:

words = ['Cats','like','dogs','as','much','cats.']

numbers = [1,2,3,4,5,4,3,2,6]

I have tried using:

words,numbers = f.split("][")

However, this removes the double bracets from the two new variable which means the second part of my program which recreates the original text does not work.

Thanks.

I assume f is a string like

f = "['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]" 

then we can find the index of ][ and add one to find the point between the brackets

i = f.index('][')
a, b = f[:i+1], f[i+1:]
print(a)
print(b)

output:

['Cats','like','dogs','as','much','cats.']
[1,2,3,4,5,4,3,2,6]

Another Alternative if you want to still use split()

f = "['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]"
d="]["
print f.split(d)[0]+d[0]
print d[1]+f.split(d)[1]

If you can make your file look something like this:

[["Cats","like","dogs","as","much","cats."],[1,2,3,4,5,4,3,2,6]]

then you could simply use Python's json module to do this for you. Note that the JSON format requires double quotes rather than single.

import json
f = '[["Cats","like","dogs","as","much","cats."],[1,2,3,4,5,4,3,2,6]]'
a, b = json.loads(f)
print(a)
print(b)

Documentation for the json library can be found here: https://docs.python.org/3/library/json.html

An alternative to Patrick's answer using regular expressions:

import re

data = "f = ['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]"
pattern = 'f = (?P<words>\[.*?\])(?P<numbers>\[.*?\])'

match = re.match(pattern, data)
words = match.group('words')
numbers = match.group('numbers')

print(words)
print(numbers)

Output

['Cats','like','dogs','as','much','cats.']
[1,2,3,4,5,4,3,2,6]

If I understand correctly, you have a text file that contains ['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6] and you just need to split that string at the transition between brackets. You can do this with the string.index() method and string slicing. See my console output below:

>>> f = open('./catsdogs12.txt', 'r')
>>> input = f.read()[:-1]  # Read file without trailing newline (\n)
>>> input
"['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]"
>>> bracket_index = input.index('][')  # Get index of transition between brackets
>>> bracket_index
41
>>> words = input[:bracket_index + 1]  # Slice from beginning of string
>>> words
"['Cats','like','dogs','as','much','cats.']"
>>> numbers = input[bracket_index + 1:]  # Slice from middle of string
>>> numbers
'[1,2,3,4,5,4,3,2,6]'

Note that this will leave you with a python string that looks visually identical to a list (array). If you needed the data represented as python native objects (ie so that you can actually use it like a list), you'll need to use some combination of string[1:-1].split(',') on both strings and list.map() on the numbers list to convert the numbers from strings to numbers.

Hope this helps!

Another thing you can do is first replace ][ with ]-[ and then do a split or partition using - but i will suggest you to do split as we don't want that delimiter.

SPLIT

f = "['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]" 
f = f.replace('][',']-[')
a,b = f.split('-')

Output

>>> print(a)
['Cats','like','dogs','as','much','cats.']
>>> print(b)
[1,2,3,4,5,4,3,2,6]

PARTITION

f = "['Cats','like','dogs','as','much','cats.'][1,2,3,4,5,4,3,2,6]"
f = f.replace('][',']-[')
a,b,c = f.partition('-')

Output

>>> print(a)
['Cats','like','dogs','as','much','cats.']
>>> print(c)
[1,2,3,4,5,4,3,2,6]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM