1234USSR4321 1234USSR4321 - 1 year ago 68
Python Question

how do i read a string from a file up to a set character (for example reading the words "hello world ¬ this is a string" up to the¬) in python 3

sorry if my English is off, i am making something in python and need help with fixing a problem i have encountered. the problem im having is i need to be able to take information in a txt file up to a point signalled by a key character such as ¬, and then need to be able to take the next part of the string after the 1st ¬ to the next ¬ and so on. the reason for this is because all the strings will be of various lengths that can and will change, so if i have the string

'znPbB t7<)!\oWk_feGTIT:7{.¬ZO9?S9$v9vpd}Z#EMKC¬'

in a note pad file i need it to come out as

'znPbB t7<)!\oWk_feGTIT:7{.'

and when i want the 2nd one, it should come out as


Answer Source

I would use split:

s = 'znPbB t7<)!\oWk_feGTIT:7{.¬ZO9?S9$v9vpd}Z#EMKC¬'
# returns
['znPbB t7<)!\\oWk_feGTIT:7{.', 'ZO9?S9$v9vpd}Z#EMKC', '']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download