Capitalize letter in the middle of a string using python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been using the following code to capitalize words:
with open("capitalize.txt") as f:
for line in f:
print line.title(),
It works fine but I want to be able to capitalize letters in the middle of the string e.g
change javascript to JavaScript, how can I do this using python?

It seems that you're not describing an algorithmic transformation (eg first letter, last letter, word boundaries, etc) but rather an arbitrary capitalization scheme in the context of known words.
As such, you'll probably want a permutation of the following using replace:
with open("capitalize.txt") as f:
for line in f:
print line.replace("javascript", "JavaScript")
If you've got a known set of words, then you can make it fancier, such as creating a dict {'javascript': 'JavaScript'} and then looping through the keys replacing each key with its value, but the basic approach will be more manual than you're envisioning.

Related

Guidance on basic python assignment [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Need to create a python code to provide a list of tuples (searched words, list of occurrences).
the searched words are listed in a Thesaurus which need to be searched in a series of documents in a Corpus.
Any suggestion/guidance?
After you read the file, you could simply use split on space to get a list of words. This however would include punctuation. To remove the punctuation you could get a list of punctuation from "string" library's "punctuation" attribute and replace the occurences of punctuation in the words list obtained above with empty string,"". Your words might have special symbols such as "/" to represent or. Then you would need regular expressions to extract the words.

How to extract a interrogation sentence from a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a string. For example :
"This is a string.Is this a question?What is the Question? I Dont know what the question is. Can you please list out the question?"
I want to extract the questions from this text using regex
what i tried
re.findall(r'(how|can|what|where|describe|who|when)(.*?)\s*\?',message,re.I|re.M))
But it gives out other things as well and if I gives the questions it separates the (how what which etc) and the rest of the question
For the above example my output is
[('is', ' is a string.Is this a question'), ('What', ' is the Question'), ('what', ' the question is. Can you please list out the question')]
Where as I want the entire question to be together.
It's totally impractical to search for key words when determining whether a sentence is a question. Given your list: how|can|what|where|describe|who|when, I can easily write sentences containing one of those words, which are not questions!
There are many ways you could tackle matching a sentence. For example, taking this as a baseline:
^\s*[A-Za-z,;'"\s]+[.?!]$
We could first alter it to match multiple sentences in the same string:
(^|(?<=[.?!]))\s*[A-Za-z,;'"\s]+[.?!]
This uses a look-behind to ensure that a sentence has just finished (unless we're at the start of the string).
And then adjust it to match only sentences which end with ?:
(^|(?<=[.?!]))\s*[A-Za-z,;'"\s]+\?
Here is an online demo of my regex, on your original string.
To have the entire question together, you should just enclose the whole pattern in parenthesis.
Here is another, simplified version:
\b([A-Z][^.!]*[?])
Thank you for helping me out
the answer was provided by #Fredrik
and can be found here https://regex101.com/r/rT1mQ0/2
\s*([^.?]*(?:how|can|what|where|describe|who|when)[^.?]*?\s*\?)

Read each line in a text file, store the values in a list, and compute scores [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Read each line in a text file, store the values in a list, and compute scores
The best approach this problem is to list all the functions you need to do the task. A typical example is:
Read file.
Read list.
Take list and get each string delimited by space.
Store string into an array.
...
Then go to the Python website and lookup how to do each function.
Example: To do input and output function in python:
https://docs.python.org/2/tutorial/inputoutput.html
Also, you can look up function by asking google. Google will then point you to answers to your questions:
Q: How to find mean of a list?
A: Finding the average of a list
If you do this enough times, eventually you will be able to write a problem that solves the problem listed.
Good Luck!

In python, how do you locate a specific word in a string file. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I know you can use seek() to find a specific byte and start reading from there. How do you find a specific word in a file and start reading from there. For example, how do I start reading a file from the word 'Origin'! Thanks for any help!!
You can implement this efficiently by using the same algorithm that grep uses to find words. This is the Boyer-Moore string search algorithm.
Fundamentally you search for the last letter of the string. You do this by creating a list of all of the letters in your target word, and then you inspect letters in the file using seek. If you find a letter which is not in the word then you know that the word cannot end before the full length of the word, so you can skip that far ahead and test again. If the letter is in the word then you use the possible positions of it in the word to refine your search. If you find the last letter, then you can move back to the expected start of the word and check that it is as you expect.

How to make a this specific Pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having a problem with patterns.
I have string like this:
string1 = "27.86.80.76.83.45.66.71.80.45.76.68.80.45.67.97.108.108.45.84.105.116.45.77.97.114.105.111"
The strings appear in the middle of one file, with different lengths.
For instance I am reading a file line by line and I need to know if the line has this pattern.
Can you guys point me in the right direction?
There's two different ways to go about this:
Build a parser - much work, but very flexible and possibly best performance (depending on implementation)
Use a regular expression. In your case this could be something like (\d{2,3}\.)+\d{2,3} (shortest string matched should be "111.11")

Categories