-2
votes
0answers
34 views

Python substring searching speed

In python, how does the speed of finding a substring in a string vary depending on the length of the pattern (the needle), all else constant? EDIT: The hypothetical example: 'mn' in ...
0
votes
2answers
42 views

How to read user input until EOF?

My current code reads user input until line-break. But I am trying to change that to a format, where the user can write input until strg+d to end his input. I currently do it like this: input = ...
3
votes
2answers
46 views

Printing until a specific character position in python

First I apologize if this is a question that has been answered many times, I searched around a while for an answer, but much of what I found went over my head since I am just beginning to learn ...
0
votes
3answers
32 views

Removing common words from a string?

I'm trying to filter common words to end up with a city name. Here's what I have: import re ask = "What's the weather like in Lexington, SC?" REMOVE_LIST = ["like", "in", "how's", "hows", "weather", ...
0
votes
3answers
34 views

Write volatile strings to file in Python

I have a lot of strings: about 14,000 in a list of tuples. alot of the strings have commas and newlines and maybe even unicode delimiters - not 100% sure. I need to write the tuples to file, ...
4
votes
2answers
39 views

Python regex to match begin of string or whitespace

I have the following string 'abc[123]defgh ijk[456]lm no[78] pq' And I would like to extract all parts which are either between the begin of the string and [ or between whitespace and [. For the ...
0
votes
2answers
18 views

Writing column of strings to CSV without quotation marks

I want to write the list someList=["a=1.0,2.0,3.0","b=1.0,2.0,3.0"] to a single-column, two-row file which looks like (with no quotation marks around each row): a=1.0,2.0,3.0 b=1.0,2.0,3.0 However, ...
2
votes
4answers
53 views

Python join list of strings with comma but with some conditions - code refractoring

Assume i have this list l: l = ['a', 'b', 'c', 'd', 'e'] I know i can join them using .join with comma separated as: s = ', '.join(l) >>> a, b, c, d, e But i want to respect these ...
-4
votes
1answer
36 views

Capitalization of the first letters of words in a sentence using python code

The code below capitalizes the first word of the words in "w".I would like to know how the function captilize(sentence) works? Specifically what does sentence.split do? ` import random def test(): ...
3
votes
3answers
44 views

Python Convert String Literal to Float

I am working through the book "Introduction to Computation and Programming Using Python" by Dr. Guttag. I am working on the finger exercises for Chapter 3. I am stuck. It is section 3.2, page 25. ...
-1
votes
2answers
23 views

function search in a list

I have a list :lst=[(1, 'AAT'), (2, 'C'), (33, 'GCC'), (4, 'T'), (11, 'ATC'), (12, 'A')] I want to create a function, if I give it a number it search in the list lst then it gives me the corresponding ...
1
vote
2answers
27 views

Python regex, remove all punctuation except hyphen for unicode string

I have this code for removing all punctuation from a regex string: import regex as re re.sub(ur"\p{P}+", "", txt) How would I change it to allow hyphens? If you could explain how you did it, ...
3
votes
3answers
52 views

Python: How can I include the delimiter(s) in a string split?

I would like to split a string, with multiple delimiters, but keep the delimiters in the resulting list. I think this is a useful thing to do an an initial step of parsing any kind of formula, and I ...
0
votes
1answer
44 views

Replacing integers in a mathematical expression

I have a large mathematical expression in a txt file that looks something like this, Z3N = -24*y[1]*r*pow(EXP,-2.0*phi)*y[5]*y[5]+(32*r-44*y[1]*r+32*r*y[4]) only much longer. What I'd like to do is ...
1
vote
1answer
17 views

how do i randomly pick characters out of a list the amount of times the user asks for using input in python?

For my assignment i have to create a password generator using python. it has to ask the user how many characters long they want the password, then it has to create it by using random.randint and ...
9
votes
2answers
101 views

Python string with space and without space at the end and immutability

I learnt that in immutable classes, __new__ may return a cached reference to an existing object with the same value; this is what the int, str and tuple types do for small values. This is one of the ...
1
vote
2answers
18 views

Python re.sub to change only the word 'a' as opposed to every instance of 'a' as a letter

For example, let's say I want to change every word 'a' into 'an' in the following text: "a apple is a| awesome fruit." Assume that the "|" character is there as a garbage character that needs to be ...
0
votes
5answers
49 views

index of second repeated character in a string

I am trying a hangman code in python. For matching a character of a word , iam using index function to get the location of character. Ex :word = 'COMPUTER' user_input = raw_input('Enter a character ...
0
votes
1answer
35 views

Python - wordset() function

test(wordset(["now", "is", "time", "is", "now", "is", "is"]) == ["is", "now", "time"]) test(wordset(["I", "a", "a", "is", "a", "is", "I", "am"]) == ["I", "a", "am", "is"]) test(wordset(["or", "a", ...
0
votes
4answers
36 views

return a sliced string to the orginal string

if I have two strings s1='abcdefghi' s2='jklmnopqr' I slice the first string s1 into sub-string ['abc','def','ghi'],then I encode it with function which gives number for each sub-sequence ...
0
votes
2answers
39 views

What happens when comparing strings with >,< operators? Python

I am trying to understand what happens when you compare strings with the >,< operators in Python. I know for sure that it doesn't compare the length of the string. Does it compare the sum of their ...
1
vote
1answer
38 views

dynamically building strings 'string'.format()

I am trying to write a function that prints tables of various dimensions. May not be the best attempt the way I did it, but almost there. Last piece of the puzzle is creating the strings with a ...
-6
votes
2answers
46 views

Python - How to count the number of characters on a user input [on hold]

Not quite sure why I'm not getting what I need, but here's what I have so far: sentence = "" sentence = raw_input("Enter a sentence: ") print "The number of characters is", sentence.count_letters() ...
0
votes
1answer
29 views

Formatting datetime in python

print str(now.month) + str(now.day) + str(now.year) Result: 1162014 How do I further format the result to add slashes: 11/6/2014 Also how would I do formatting for hour, minute, and second? ...
0
votes
1answer
35 views

Python 3 unicode annoyances

I have a string, created from a list. When I try to send the string as the body of an email, I get this error: UnicodeEncodeError: 'ascii' codec can't encode character '\u2013' in position 1242: ...
-1
votes
1answer
66 views

PythonXY, spyder and python any() function

I'm trying to search words in a text with the function any() of python, I was expecting the same behaviour for the two following, but it's not: keyword_list=['WASHER', 'SCREW'] all_text = ...
0
votes
1answer
22 views

Python Find Strings in txt file writing in new file in specific form

I want my python program to search for some specific portion of strings in a text file and write them into a new text file. I have a txt-file in this form. Input-Text-File: … different text … ...
1
vote
2answers
41 views

Can't replace printed line with \r

I am trying to print lines, which replace always the one before print "\r Getting hosts %i" % int(100/count*len(lines)) This should result in Gettings hosts 0 - 100 % in one dynamic line, but it ...
0
votes
3answers
35 views

String literals for file names

I am new to Python - but not to programming, and on a bit of a steep learning curve. I have a programme that reads several input files - the first input file contains (amongst other things) the path ...
-2
votes
1answer
37 views

String formatting in Python?

I'm having some difficulty getting string formatting working - how do i get a string inside here? VARIABLEHERE = "myToken" import json,httplib connection = httplib.HTTPSConnection('api.parse.com', ...
4
votes
1answer
52 views

Replace for multiple letters python

I am learning python and came to know of the replace function. The replace function works fine. Now is there any function in python to replace multiple letters, each with different letters? For ex: ...
2
votes
3answers
71 views

Python: Convert a string to its binary representation

I use Python 2.7.X. I have a text file with the following content: \xe87\x00\x10LOL Note that this is the text itself, and not its binary representation (meaning the first char is '\\', not ...
1
vote
1answer
40 views

Parsing from a stream of XML string fragment in Python

I have an eye-tracker uses TCP/IP communication and XML to send data between client (application) and server (eye-tracker). The following is an example of the XML data string I receive continuously ...
1
vote
1answer
50 views

Capitalize a substring within a string

I'm trying to create something like: string: How do you do today? substring: o >>> hOw dO yOu dO tOday? I've already written the rest of the code (prompting for strings etc.), I am just ...
0
votes
3answers
28 views

How to check if the lines in a csv connection are strings or numbers

I have a CSV file, /home/user/file.csv, such as: V1 1.24 4.55 3 I am trying to read in only the numbers, without the prior knowledge that there is a header. How do I read this and skip all String ...
0
votes
3answers
38 views

Python: if list contains string print all the indexes / elements in the list that contain it

I am able to detect matches but unable to locate where are they. Given the following list: ['A second goldfish is nice and all', 3456, 'test nice'] I need to search for match (i.e. "nice") and ...
0
votes
3answers
57 views

convert number to string

if I have a string s='ABCDEFJHI', and I slice it like this ['ABC','DEF','JHI']. I have function encode(some calculation) which convert the sliced string into numbers. for example 'encode('ABC' ) ...
1
vote
2answers
43 views

Matching and parsing to extract information from complicated string

I have a string with the arguments from a function such as 'arg0, arg1=1, arg2=None' and would like to extract the names and default values for each argument. I have no problems handling simple cases. ...
2
votes
2answers
63 views

Can I remake a string with a loop of some kind?

I've got a string looking like this: variable1 = '1+2+3*2-1' and I need to change it so that it looks like this: variable2 = '((((1+2)+3)*2)-1)' Is it possible to do with some kind of ...
0
votes
3answers
57 views

Best way to tokenize names

Say you have a string that contains names, as follows: "Leonard, A., Fraternali, F., Daraio, C." Now, there are three people in this string, and I would like to find the best way to obtain these ...
0
votes
1answer
35 views

Split array between 2 symbols

I have a really long array from where I want to get some data, but the data I'm interested in is between this signs < and > I have many of them. If I wanted them all in a single array, how ...
0
votes
3answers
33 views

print bytes which contains NULL

I have a bytes string, str = 'string ends with null\x00\x11u\x1ai\t' and I expect that str should terminate after the word null, because a NULL \x00 follows immediately, however when I print str, ...
0
votes
1answer
43 views

Split a Python String Using Multiple Delimiters

I have a somewhat complex filename following the pattern s[num][alpha1][alpha2].ext that I'm trying to tokenize. The lexicons from which alpha1 and alpha2 are drawn are contained in two lists. I ...
1
vote
1answer
31 views

Custom Sort Complicated Strings in Python

I have a list of filenames conforming to the pattern: s[num][alpha1][alpha2].ext I need to sort, first by the number, then by alpha1, then by alpha2. The last two aren't alphabetical, however, but ...
2
votes
1answer
35 views

string formatting inside a dict?

How do i string format inside a dict... I tried a few things already...to no avail I have this dict. PUTVARIABLE = sys.argv[0] ...
0
votes
1answer
28 views

Python regex to match subreddit names

I'm just learning python and trying to pull subreddits out of a file with a regex, but not having much luck.. import re string1 = "asdf asdf 587985 (*&(*& /r/whatever3/fjfjfjjrr /r/whatever2 ...
0
votes
0answers
16 views

MCEdit filter - String Index out of Range Python

Used for an MCEdit Filter: Get Error: String Index out of range inputs = ( ("Change Relative Values", True), ("Change Exact Values", True), ("Use Selected Region", False), ("Min X:", ...
0
votes
2answers
66 views

python split() vs rsplit() performance?

I have a string in python. I want to split it with maxsplit = 1 on separator which is pretty close to end of the string. For e.g. a = "abcdefghijklmnopqrstuvwxyz,1". Will a.split(",", 1) be better ...
2
votes
3answers
88 views

Improve performance: Remove all strings in a (big) list appearing only once

I have a big list (25000 items, 14000 words) like this: before (please have a look below for the right list): texts = ['Lorem hello ipsum', 'Lorem ipsum generator machine', ... 'hello Lorem ...
1
vote
1answer
41 views

Python string format function gives unexpected results when specifying precision

When I do any of these I get the expected results as shown: >>> '{text:{fill}{align}{width}}'.format(text = 'abcdef', width = 8, fill = '~', align = '^') '~abcdef~' >>> ...

15 30 50 per page