Split in string and join words under Python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to split and capture the 4 words in a sentence and encountered this error: empty separator?
My codes
s1 = input("Enter 3 random strings, separated by commas:") s1 = s1.split(sep = '') print (s1[4])
Thank you for any advices!

try this
s1 = input("Enter 3 random strings, separated by commas:")
s1 = s1.split(sep = ',')
print (s1)
you didnt have the, in sep==""
hope it helped

there's two error in your code
you may change s1=s1.split(), this will split your words.
and at the end of your code, I think you want to print the lest world?
if so, you may be print(s1[2])
because the python count the list from 0, the first one is 0, the third one is 2,
due to your input is 3 words, so the length of the list is 3.you can also print(s1[-1]), it always print out the last one.

For it not to be an empty separator, you should insert a comma inside the two quotes, or else it will technically be empty. In fact, you don't even need the sep because you could just do .split(",")

Related

Regex re.compile not working on jupyter notebook [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm using jupyter notebook to do some simple Regex patterns but it keeps returning none for these two cases and I can’t see why.
I want to search for 3 to 5 digits pattern
digitRegex = re.compile('r(\d){3,5}')
digitRegex.search('123456789')
should return '12345' but it returns none :(
Same problem here, when trying to find 3 consecutive US phone numbers and I want optional: area code and separated by a comma
phoneRegex = re.compile(r'((\d\d\d-)?\d\d\d-\d\d\d\d(,)?){3}')
phoneRegex.search('My numbers are 415-555-1234,555-4242,212-555-000')
should return the 3 phone numbers but also returns none :(
Thank you...
In your first code, you put the r prefix inside the string, so it won't work. (Such prefix are used for raw strings.)
Working code:
digitRegex = re.compile(r'\d{3,5}')
digitRegex.search('123456789')
In the second sample, the string won't match because it attempts to get three phone numbers at all and the last one ends with three figures instead of four. You need to fix either your regexp or your phone number.
Working sample with valid numbers matching the original regex:
phoneRegex = re.compile(r'((\d\d\d-)?\d\d\d-\d\d\d\d(,)?){3}')
phoneRegex.search('My numbers are 415-555-1234,555-4242,212-555-0000')
Working sample with a edited regex matching the original numbers:
phoneRegex = re.compile(r'((\d\d\d-)?\d\d\d-\d\d\d\d?(,)?){3,4}')
phoneRegex.search('My numbers are 415-555-1234,555-4242,212-555-0000')

python: csv.writer - commas between numbers in the output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
i suspect that this is a simple answer, but i cannot figure out the answer. (I did give it due diligence)
i wrote a simple python program to identify prime numbers. the program is function, but i'm receiving strange results in the output. when i have it write a number with multiple digits, each number is comma separated; for example, 13 is added to the document as 1,3. I would like to have a comma after each full number (13,) and don't want commas within the number (1,3 or 1,301). eventually, i want to have each number on its own row (one of the issue that i ran into in my g1 program is that the row became too long around 50mill ;-)
Any thoughts?
#!/bin/python3
import time
import os
import csv
folderLocation = "c:/notNow/"
primeName = "primeNumbers.csv"
# notPrimeName = "noPrimeNumbers.csv"
primePath=folderLocation + primeName
# notPrimePath=folderLocation + notPrimeName
no=13
os.makedirs(folderLocation)
f = open(primePath, "w")
writer = csv.writer(f)
writer.writerow(str(no))
output: 1,3
writerow expects a sequence of items (e.g list). A string is just seen as a sequence of individual characters, try this instead:
writer.writerow([no])

I use re.sub in python3 and set count to 1,but it replaces all targets? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This my code:
import os,re
textFile = open('D:\\hehe.txt')
textContent = textFile.read()
print(textContent)
adjRegex = re.compile(r'ADJECTIVE')
nounRegex = re.compile(r'NOUN')
verbRegex = re.compile(r'VERB')
for i in range(len(textContent)):
if(adjRegex.search(textContent) != None):
print('Enter a adjective')
textContent = adjRegex.sub(input(),textContent)
elif(nounRegex.search(textContent) != None):
print('Enter a noun')
textContent = nounRegex.sub(input(),textContent)
elif(verbRegex.search(textContent) != None):
print('Enter a verb')
textContent = verbRegex.sub(input(),textContent,count=0)
else:
break
print(textContent)
textFile.close()
If this is your real code (i. e. you aren't actually using any complex regular expressions, just string matching), you could use simple str.replace:
adjRegex = "ADJECTIVE"
textContent = textContent.replace(adjRegex, input(), 1)
Note that then all the regexes will be mere strings.
And instead of searching for regexes, you could try checking whether the substrings are present in textContent:
if adjRegex in textContent:
# replace it!
You are using re.sub the wrong way.
Why are you iterating over every character in your text? You can
just go line by line using readlines() then replace()
Put the prompt message inside input('Enter a noun') instead of
printing it
Assign input to a variable to increase readability

Why is the While loop creating a syntax error on the next statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Hi I've been trying to write a program in python 2.7 that takes a word as its input and outputs the number of letters in a word. At first it was working but something happened and now it keeps returning an error by the first line that is not part of the while loop.
This is a part of the code:
def number_of_letters(input):
nol = input.find(input[-1])
while input[nol:] != input[-1]:
nol = input.find(input[-1], input.find(input[-1] + 1)
nol = nol + 1
print nol
The python interpreter keeps returning a syntax error by whatever I try to put after the while block (in this case 'nol = nol + 1 ')
I've tried playing around with it but nothing worked. PLease help.
By the way if there are any modules that may help with this program that would be great but I'd also like to know why this one isn't working
You are missing a closing paren:
nol = input.find(input[-1], input.find(input[-1] + 1)) #<- add here
If you want to count the number of actual letters you can use str.isalpha:
return sum(ch.isalpha() for ch in inp)
If you don't care what characters are there just use len(inp).
Avoid input as a variable name as it shadows the python function.
Change this
nol = input.find(input[-1], input.find(input[-1] + 1)
to this
nol = input.find(input[-1], input.find(input[-1] + 1))
Notice that parenthesis in the end.
There is a built in function for getting the length of a string in python.
word = "test"
length = len(word)

Index into a string in a list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Suppose I have a list containing 9 fields, and the 9th field is a string.
Then print(line) will print the entire line, print(line[9]) will return something like:
1/0:.:PASS:90:204,90:201,88:48,39:-204,0,-90:-48,0,-39:14:9,5:5
but print(line[9[0:1]])
will return
File "FileParser.py", line 9, in ?
print(line[9[0:1]])
TypeError: unsubscriptable object
If I assign line[9] to a second var, then I can manipulate it like a string, but this seems like a silly extra step.
Is there a way to index directly into the string while still part of the list?
Thank you
You can index it like this
line[9][0:1]
line[9] will get the actual string and you get the range of characters from that string.
When you do line[9[0:1]] you are trying to get the range of values from the number 9, which is not possible. That is why your code fails.
Since line[9] is a string, then you have to do
line[9][0:1]
Let's say line[9] = "some string". Then you can manipulate it with
line[9][0:1]
which will be equivalent to
"some string"[0:1]

Categories