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])
Related
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 1 year ago.
Improve this question
I'm trying to write a Python program that writes out 20 steps between 0 and 2*pi, with the value and the sin and cos of that value.
I have:
import math
f=open('question10.txt','w')
x=0
pi=math.pi
f.write("x, y=sin(x), z=cos(x)\n")
while x<=2*pi:
f.write("{}, {}, {}\n".format(x, math.sin(x), math.cos(x)))
x = x+(pi/10)
f.close()
I have no idea what isn't working. It won't even create the data file, and isn't giving me any sort of error.
One thing you can try is to use with instead of writing and closing directly. This will ensure that the file will be closed after writing without you having to manage it:
import math
x=0
pi=math.pi
with open("question10.txt", "w") as out_file:
out_file.write("x, y=sin(x), z=cos(x)\n")
while x<=2*pi:
out_file.write("{}, {}, {}\n".format(x, math.sin(x), math.cos(x)))
x = x+(pi/10)
If you are still having problems, perhaps you can explain the error you are encountering?
There's nothing wrong with your code. You should check that you run your python file correctly.
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')
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(",")
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
So, I have a list of lists and trying to write the values to a file with tab delimited;
sorted_results=[
["test1", 01],
["test2", 02],
]
with open('outfile.txt', 'a') as write_file:
for i in sorted_results:
write_file.write("{}\t{}\n".format(i[0], i[1]))
The end result comes out as:
test1 01
test2 02
Values are space delimited not tab. What am I missing? If I add a space before \t then end result will have a space and a tab between the values.
You can read the file back in and inspect the resulting data.
>>> open('outfile.txt').read()
'test1\t1\ntest2\t2\n'
This shows that the tab character is indeed written to the file. If you are still in doubt use a hex editor to view the characters.
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]