Python print split take char 5-10 for instance - python

I want to split string that I have.
Lets say string is hello how are you.
I want to print only the how are (meaning start after hello and finish after are
My code for now just start after the hello, but print all the rest.
Want to avoid the you.
ReadJSONFile=JSONResponseFile.read() # this is the txt file with the line
print ReadJSONFile.split('hellow',1)[1] # this gives me everything after hello

You could use string slicing:
>>> s = "hello how are you"
>>> s[6:13]
'how are'

Combine two str.split calls:
>>> s = 'hello how are you'
>>> s.split('hello', 1)[-1]
' how are you'
>>> s.split('hello', 1)[-1].split('you', 1)[0]
' how are '
>>> s.split('hello', 1)[-1].split('you', 1)[0].strip() # remove surrounding spaces
'how are'

If you have the start and end indices you can extract an slice of the string by using the slice notation:
str = 'Hello how are you"
# you want from index 6 (h) to 12 (e)
print str[6:12+1]

This should help: (Using index and slicing)
>>> start = h.index('hello')+len('hello')
>>> end =h.index('you')
>>> h[start:end].strip()
'how are'

Related

How to get all string except the first word in Python

How do you get rid of the first word of the string? I want to get rid of the number and get the rest as a whole string.
Input text is:
1456208278 Hello world start
What I wanted for output was:
'Hello world start'
Here was my approach:
if isfile('/directory/text_file'):
with open('/directory/test_file', 'r') as f:
lines = f.readlines()
try:
first = str((lines[0].strip().split()))
final = first.split(None, 1)[1].strip("]")
print final
except Exception as e:
print str(e)
The output of the code was:
'Hello', 'world', 'start'
I do not want " ' " for every single string.
Alternative solution with the partition() method:
In [41]: '1456208278 Hello world start'.partition(' ')[2]
Out[41]: 'Hello world start'
If you split and then join, you may lose spaces which may be relevant for your application. Just search the first space and slice the string from the next character (I think it is also more efficient).
s = '1456208278 Hello world start'
s[s.index(' ') + 1:]
EDIT
Your code is way too complex for the task: first you split the line, getting a list, then you convert the list to a string, this means that you will get ' and ] in the string. Then you have to split again and clean the stuff. It's overly complex :)
Another approach you could use it to split and join, but as I said earlier, you may lose spaces:
s = '1456208278 Hello world start'
t1 = s.split() # ['1456208278', 'Hello', 'world', 'start']
t2 = s[1:] # ['Hello', 'world', 'start']
s2 = ' '.join(t2)
or more concisely
s2 = ' '.join(s.split()[1:])
This approach could be better if you want to use comma to separate the tokens, e.g.
s3 = ', '.join(s.split()[1:])
will produce
s3 = 'Hello, world, start'

Python Complicated Splitting of a String

Say that I have this string:
"hello":"noun":"a greeting";"hello":"verb":"the;action;of;greeting"
How can I make it so string.split(";") or string.split(":") will ignore any characters in quotation marks?
Thanks,
PM
If you can't get a cleaner input than that, I'd recommend using a regular expression and creating a list of tuples with findall():
>>> import re
>>> mystring = '"hello":"noun":"a greeting";"hello":"verb":"the;action;of;greeting"'
>>> result = re.findall(r'"(.+?)":"(.+?)":"(.+?)"', mystring)
>>> for item in result:
... print(*item)
...
hello noun a greeting
hello verb the;action;of;greeting
You can format the output with str.format():
>>> for item in result:
... print('{} - {}, {}'.format(*(part.replace(';', ' ') for part in item)))
...
hello - noun, a greeting
hello - verb, the action of greeting
Your question doesn't make it 100% clear if all strings are inside quoatation marks. Anyway, this should work. It doesn't remove the quotation marks around the string (you can do this afterwards if you want to).
In [20]: [x for x in re.split(r'(:|;|".*?")', s) if x not in [":",";",""]]
Out[20]:
['',
'"hello"',
'"noun"',
'"a greeting"',
'"hello"',
'"verb"',
'"the;action;of;greeting"']

Get string between 2 other strings - Python 2.7.8

So I have a huge string, where some strings occur a lot. I need the text in between.
"I don't need this""This is what I need""I also don't need this."
This happens many times, and I'd like all the strings I need in a list.
There's also a lot of special characters, but no ' so I can use them for strings.
I have tried with the re library, but I can't get it to work.
I tried splitting too
listy = hugestring.split('delim1')
for element in listy:
element = element.split('delim2')
But the second splitting doesn't work.
You could use a regex like this
>>> import re
>>> your_str = "foo This is what I need bar foo This is what I need too bar"
>>> left_delim = "foo "
>>> right_delim = " bar"
>>> pattern = "(?<={})[ \w]*?(?={})".format(left_delim,right_delim)
>>> re.findall(pattern,your_str)
['This is what I need', 'This is what I need too']
This will give you a list of all strings within quotes contained in a string:
import re
in_str = "I don't need this\"This is what I need\"I also don't need this."
out_str = re.findall(r'\"(.+?)\"', in_str)
print out_str
So in the above example, print out_str[0] will give you what you need as there's only the one quote in there.
this is the result of what you say in comment , so whats problem now ?:
>>> n= s.split("I don't need this")
['', "This is what I needI also don't need this."]
>>> [i.split("I also don't need this") for i in n]
[[''], ['This is what I need', '.']]

How to use string.replace() in python 3.x

The string.replace() is deprecated on python 3.x. What is the new way of doing this?
As in 2.x, use str.replace().
Example:
>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'
replace() is a method of <class 'str'> in python3:
>>> 'hello, world'.replace(',', ':')
'hello: world'
The replace() method in python 3 is used simply by:
a = "This is the island of istanbul"
print (a.replace("is" , "was" , 3))
#3 is the maximum replacement that can be done in the string#
>>> Thwas was the wasland of istanbul
# Last substring 'is' in istanbul is not replaced by was because maximum of 3 has already been reached
You can use str.replace() as a chain of str.replace(). Think you have a string like 'Testing PRI/Sec (#434242332;PP:432:133423846,335)' and you want to replace all the '#',':',';','/' sign with '-'. You can replace it either this way(normal way),
>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
>>> string = string.replace('#', '-')
>>> string = string.replace(':', '-')
>>> string = string.replace(';', '-')
>>> string = string.replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
or this way(chain of str.replace())
>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
Official doc for str.replace of Python 3
official doc: Python 3's str.replace
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
corresponding VSCode's syntax notice is:
str.replace(self: str, old, new, count) -> str
Two method to use str.replace
Method 1: use builtin str's replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "from", "to")
Method 2: use str variable's replace -> strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("from", "to")
Full demo
code:
originStr = "Hello world"
# Use case 1: use builtin str's replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "world", "Crifan Li")
print("case 1: %s -> %s" % (originStr, replacedStr1))
# Use case 2: use str variable's replace -> strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("world", "Crifan Li")
print("case 2: %s -> %s" % (originStr, replacedStr2))
output:
case 1: Hello world -> Hello Crifan Li
case 2: Hello world -> Hello Crifan Li
screenshot:
My related (Chinese) post: 【详解】Python 3中字符串的替换str.replace
Try this:
mystring = "This Is A String"
print(mystring.replace("String","Text"))
FYI, when appending some characters to an arbitrary, position-fixed word inside the string (e.g. changing an adjective to an adverb by adding the suffix -ly), you can put the suffix at the end of the line for readability. To do this, use split() inside replace():
s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'
Simple Replace:         .replace(old, new, count) .
text = "Apples taste Good."
print(text.replace('Apples', 'Bananas')) # use .replace() on a variable
Bananas taste Good. <---- Output
print("Have a Bad Day!".replace("Bad","Good")) # Use .replace() on a string
Have a Good Day! <----- Output
print("Mom is happy!".replace("Mom","Dad").replace("happy","angry")) #Use many times
Dad is angry! <----- Output
ss = s.replace(s.split()[1], +s.split()[1] + 'gy')
# should have no plus after the comma --i.e.,
ss = s.replace(s.split()[1], s.split()[1] + 'gy')

Filter out a specific string in a larger string?

I'm new to Python and after this one script I probably won't work with Python at all. I'm extracting some data using Scrapy and have to filter out some string (I've already done this with digits using isdigit()). Googling gives me pages about filtering out special strings, but what I want is really just a small part of a larger string.
This is the string:
Nima Python: how are you?
What I want left:
how are you?
so this part removed:
Nima Python:
Thanks in advance guys.
This works:
>>> s = "Nima Python: how are you?"
>>> s.replace("Nima Python: ", "") # replace with empty string to remove
'how are you?'
I'm assuming there will be other strings like this... so I'm guessing str.split() might be a good bet.
>>> string = "Nima Python: how are you (ie: what's wrong)?"
>>> string.split(': ')
['Nima Python', 'how are you (ie', " what's wrong)?"]
>>> string.split(': ', 1)[1]
"how are you (ie: what's wrong)?"
>>> string = 'Nima Python: how are you?'
>>> string.split(':')[1].strip()
'how are you?'
String slicing: (This is the easiest way, but isn't very flexible)
>>> string = "Nima Python: how are you?"
>>> string
'Nima Python: how are you?'
>>> string[13:] # Used 13 because we want the string from the 13th character
'how are you?'
String replace:
>>> string = "Nima Python: how are you?"
>>> string.replace("Nima Python: ", "")
'how are you?'
String split: (splitting the string into two parts using the ":")
>>> string = "Nima Python: how are you?"
>>> string.split(":")[1].strip()
'how are you?'

Categories

Resources