0

is there a way in python to only take from a file a string in between certain characters? for example, if i have this in a file:

city state[long, lat]population

how can i take from the line just what is in between the brackets (that is, come up with [long, lat])? please help me. thank you!

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by nbaztec
0

i have to read from a file. i need to find the information between certain characters for every line in the file, not just from one string set. but thank you anyway

0

well yes, i suppose so, but i need to add what's in between the []s to a list, and when i tried that definition, i got a typeerror: can only concatenate list (not "tuple") to list

0

@pansquare, pyTony has provided the correct answer. However you seem to be not bothering to search the documentation on docs.python.org.

s.partition(sep) returns a tuple which essentially is a set of 3 values (before, separator, after)

'foo[baz]bar'.partition('[') returns a tuple 'foo', '[', 'baz]bar' -> (before, _, after)
after.partition(']') returns a tuple 'baz', ']', 'bar' -> (mid, _, after)

What needs to be returned is before, mid, after which is a tuple while you only need mid for l.append(mid).
The file in question is essentially a collection of strings.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.