Tagged Questions
-1
votes
2answers
39 views
Add elements of string in python
I have a file who has two columns with numbers. I want to sum all elements of each column. I tried splitting that file using:
for line in f:
line=line.strip()
tokens=line.split()
Then I tried to ...
0
votes
2answers
29 views
ValueError: could not convert string to float in simple code
# -*- coding: cp1250 -*-
print ('euklides alpha 1.0')
a = raw_input('podaj liczbę A : ')
b = raw_input('podaj liczbę B : ')
a = float('a')
b = float('b')
if 'a'=='b':
print 'a'
elif 'a' ...
0
votes
3answers
42 views
How do I make a list in alphabetical order and format it properly?
I have a project question that says "Write a method that takes the user entered words/strings and sort them into alphabetical order."
I have the basic method worked out, but the problem is I need to ...
2
votes
2answers
36 views
How do I display my user's errors?
My user wants to make a password and I'm supposed to be checking if it's valid or not. So far, I have down the code to check if it is valid/not valid. Now, the next step (after determining it is not ...
5
votes
2answers
85 views
Multiplying a string by zero
In terms of computer science, and specifically in Python, how to explain that multiplying a string by zero (which is an int) produces an empty string?
>>> 'hello' * 0
>>> ''
0
votes
2answers
41 views
Where To Put str.strip
I wrote a code for a computer generated madlibs:
from random import randrange
print 'Welcome to Madlibs!'
def choose():
f = open('/usr/share/dict/words')
content = f.readlines()
return ...
3
votes
1answer
24 views
python, how split string with limit by the end
i have the following string:
"hello.world.foo.bar"
and I want to split (with the "." as delimiter, and only want to get two elemets starting by the end) this in the following:
["hello.world.foo", ...
0
votes
5answers
39 views
How to pad a string to a fixed length with spaces in Python?
I'm sure this is covered in plenty of places, but I don't know the exact name of the action I'm trying to do so I can't really look it up. I've been reading an official Python book for 30 minutes ...
0
votes
1answer
26 views
Removing double quotes for items in list
>>> s = "name='smith'"
>>> s.split('=')
['name', "'smith'"]
However, how do I remove the double quotes such that when I do the similiar thing I get:
>>> s = ...
0
votes
1answer
46 views
Python - File handling - Can't convert 'int' object to str implicitly [duplicate]
I'm trying to read the story of an adventure game from a file into a dictionary, then have the player dictate the advances through the game with either "next" or "back".
The first function work's ...
0
votes
2answers
43 views
python: reading a string to int for comparison in for loop.
I'm new to this stuff so sorry if this is a dumb question. I've got this data which lists countries by GDP (from factbook). Here it is compiled into a single string:
'\n1\tEuropean Union\t$ ...
1
vote
0answers
43 views
Operator IS for string compare in python [duplicate]
I try compare strings and get strange thing. First of all I try make it with short strings.
>>> str1 = 'x' * 10
>>> str2 = 'x' * 10
>>> str1 is str2
True
>>> ...
1
vote
2answers
62 views
Python - remove stopwords from a string
I am having trouble creating code which removes stop words from a string input. Currently, here is my code:
stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \
...
2
votes
2answers
67 views
Losing accents with for loop in Python
When I use the next code in Python:
line = "áaáaáaá"
for c in line:
print c
My out is:
�
�
a
�
�
a
�
�
a
�
�
How can I fix this?
2
votes
0answers
49 views
Print web page source code in python
I want to print a web page source code but python print command just prints empty space and I think it's because of its large size. Is there any way to print page source code in shell or at list in a ...
-2
votes
1answer
54 views
Formating Python strings with \n [on hold]
I am trying to format this string into a board using \n, there is syntax error somewhere but I don't know where!
f1= ("a" "b" "c" + "|"\n + " -------------"+"\n" + "1 ...
1
vote
2answers
35 views
trying to parse a string and convert it to nested lists
I'm new to Python and blocking on this problem:
trying to go from a string like this:
mystring = '[ [10, 20], [20,50], [ [0,400], [50, 328], [22, 32] ], 30, 12 ]'
to the nested list that is ...
0
votes
5answers
39 views
How do I take a bunch of integer variables and display as one string
I have three int variables representing different octets in an IP address and an 'i' variable that is part of a loop. I am trying to set all four variables to a single string and save it to a variable ...
1
vote
3answers
54 views
How to check if a string is an rgb hex string
I am trying to create a way to proofread command console input and check to make sure that the string is an rgb hex string. (Ex: #FAF0E6) Currently I am working with a try: except: block.
def ...
1
vote
3answers
49 views
Python Add string to each line in a file
I need to open a text file and then add a string to the end of each line.
So far:
appendlist = open(sys.argv[1], "r").read()
1
vote
3answers
52 views
string to numeric array
From one program I generate bunch of data and it is stored in a file. An example of the file's contents is
[[1, 2, 3], [4, 5, 6]]
As you can see, the data has the exact form of an array. Later in ...
1
vote
1answer
63 views
Python: Find maximum length of all n-word-length substrings shared by two strings
I am working to produce a Python script that can find the (longest possible) length of all n-word-length substrings shared by two strings. Given two strings:
"this is a sample string"
"this ...
1
vote
1answer
36 views
Peculiar behaviour of JellyFish damerau levenshtein distance
I am trying to use Jellyfish to work with fuzzy strings. I am noticing some strange behaviour of the damerau_levenshtein_distance algorithm. For example
import jellyfish as jf
In [0]: ...
0
votes
1answer
27 views
Is there any documentation for Jellyfish?
silly question. Does anyone know where to find any documentation for the string matching library, jellyfish? I have installed it, but can't for the life of me find any documentaion....
1
vote
1answer
57 views
Python re.sub considered slow?
I am fairly new to Python. I am building a script to grovel through a log file, like I have done a hundred times in Perl. I am using a hash to count occurrences of certain fields in the log file, like ...
1
vote
4answers
43 views
__str__ method in Python
I can't manage to get this __str__ to work.
I created a class,
class Hangman :
and then
def __str__(self) :
return "WORD: " + self.theWord + "; you have " + \
self.numberOfLives + "lives left"
...
0
votes
0answers
20 views
Opening file from path without knowing full file name (python)
I am trying to open a file using the full path, but I don't know the complete name of the file, just a unique string within it.
i = identifier
doc = open(r'C:\my\path\*{0}*.txt'.format(i), 'r')
...
1
vote
1answer
36 views
Converting string that looks like a list into a real list - python
My input files have lines that looks like this:
[(0, 1), (1, 3), (2, 1), (3, 1), (4, 1)]
[(0, 1, 6), (1, 3,7), (3, 1,4), (3, 1,3), (8, 1,2)]
[1,2,3,5,3]
There are no letters, no decimals, only ...
0
votes
1answer
23 views
Writing string variables from Tkinter.Entry
So I'm new to programming and am using Python 2.5 (for Autodock). I have made a simple GUI to take values from the user, I am trying to generate an output file using the said values but I keep getting ...
0
votes
2answers
51 views
convert a whole string to list when the string is actually a list
I have a very weird problem ...... I have a some lists in my data set which actually became
strings . Is there anyway to make them list again?
for example I have a string b like
In [47]: b
...
0
votes
1answer
52 views
Parsing members of a variable length python string
I am using sed in python to read the text from a log file into a single string.
Here is the command:
sys_output=commands.getoutput('sed -n "/SYS /,/Tot /p" %s.log' % cim_input_prefix)
and here is ...
0
votes
1answer
32 views
How to locate a string while is running .read() on Python?
i'm using urllib2
try:
msmalvo = urllib2.urlopen(website)
except URLError as e:
msmalvo = e
if msmalvo.code == 200:
if msmalvo.read() == '<head>':
print 'Exits!'
else:
...
0
votes
4answers
32 views
Regex to retrieve the last few characters of a string
Regex to retrieve the last portion of a string:
https://play.google.com/store/apps/details?id=com.lima.doodlejump
I'm looking to retrieve the string followed by id=
The following regex didn't seem ...
0
votes
1answer
33 views
Python IndexError On Conditional Statement
If I test the "empty" condition of the following, I get an IndexError that states the string index is out of range. Why is that? I want the script to print "empty" if the user input is empty.
pyg = ...
0
votes
3answers
52 views
Detecting Vowels vs Consonants In Python [duplicate]
What silly mistake am I making here that is preventing me from determining that the first letter of user input is a consonant? No matter what I enter, it allows evaluates that the first letter is a ...
6
votes
1answer
43 views
Why does this Python script have a \ before the multi-line string and what does it do?
In a Python script I'm looking at the string has a \ before it:
print """\
Content-Type: text/html\n
<html><body>
<p>The submited name was "%s"</p>
</body></html>
...
0
votes
3answers
38 views
How do I separate a space-delimited string into multiple variables?
I'm trying to write a program that uses Pascal's triangle to FOIL binomials. Any binomial FOILed using this method will follow a basic pattern. I already have a general idea of what to do, I just need ...
3
votes
2answers
43 views
Put the read file in directory
So I'm writing a function to open a file, read it and put it's content in a dictionary.
Basically the file that I'm reading looks like this:
Bread 10
Butter 6
Cheese 9
Candy 11
Soda 5
I want to ...
0
votes
2answers
48 views
Python script to filter a list of strings based on ending
I dont know any python but i need to customize a script a little bit.
There are strings parsed in the script and put to a list (I guess).
Then these strings are filtered based on whether they start ...
1
vote
3answers
37 views
How to iterator over every [:2] overlapping characters in a string of DNA code?
Let's say I have a string of DNA 'GAAGGAGCGGCGCCCAAGCTGAGATAGCGGCTAGAGGCGGGTAACCGGCA'
Consider the first 5 letters: GAAGG
And I want to replace each overlapping bi-gram 'GA','AA','AG','GG' with some ...
0
votes
2answers
33 views
Python Extracting strings from multiple lines of strings
Hi I would like to extract strings from input file like the below:
>a11
UCUUUGGUUAUCUAGCUGUAUGA
>a11
UCUUUGGUUAUCUAGCUGUAUGA
>b22
UGGUCGACCAGUUGGAAAGUAAU
>b22
ACUUCACCUGGUCCACUAGCCGU
...
0
votes
2answers
45 views
How do I reverse items from a list and store it as a string in python
I have a list where each element is a letter. Like this:
myList = ['L', 'H', 'V', 'M']
However, I want to reverse these letters and store them as a string. Like this:
myString = 'MVHL'
is there ...
0
votes
1answer
35 views
How to make many tuples with variable name
I have a list like this:
somelist = ['apple','grapes','wood']
but somelist may be either 3 items in length or even 20 items in length
I want to make a tuple for each of those items like:
tup1 = ...
3
votes
2answers
102 views
Why is concatenating strings running faster than joining them? [duplicate]
As I understand it "".join(iterable_of_strings) is the preferred way to concatenate strings because it allows for optimizations that avoid having to rewrite the immutable object to memory more times ...
1
vote
1answer
56 views
Generating combinations of two strings using Python
I have a string str1 = "ABC" and I need to insert 0 to len(str1) number of "x"s in the string str1 except at the last position.
So in the above example, I want a resultant list with 0 to 2 "x"s ...
0
votes
1answer
25 views
Expected string or buffer Python
I'm really new to python, it's the first time I'm writing it actually.
And I'm creating a program that gets the number of views on a Twitch.tv stream, but I'm getting an error Expected string or ...
0
votes
2answers
47 views
What is wrong with this Palindrome function?
UPDATE: Thanks to the answers below, I now know that it is an error in the function and has nothing to do with Python's behavior. (My foul) I've since changed the title from "Could Python skip spaces ...
0
votes
1answer
24 views
Finding a specific character in a string
I'm trying to create a function that takes a string input, and checks if it has "=" or ">" inside it, then returns a list after applying the built in string method "strip" to it on "=" or ">". In ...
0
votes
1answer
64 views
Python string compare error
I am getting the following error when converting my binary d.type_str variable to 'bid' or 'ask'. Thanks for the help guys! I'm using python 2.7
My code:
from itertools import izip_longest
import ...
0
votes
2answers
26 views
Transforming sequence of pure characters into separate float numbers in python [closed]
I have thousands of lines with data in the form: 6580,f|S,17:42:29.3,-39:01:48,2.19,2.41,-0.22
I would like to take only the last number (in this case -0.22) so I can perform some calculations.
My ...