Tagged Questions
0
votes
3answers
31 views
Finding and combining acronyms in Python
I am trying to process some tweets, and I am trying to split up certain terms in the tweets as follows :
word : DailyRX
final_word : Daily R X
word : AdeleOfficial
final_word : Adele Official
word ...
0
votes
5answers
77 views
Add http://xxx.xx/ to a string using python
How can I add "http://test.url/" to the result link.get('href') below, but only if it doesn't contain "http"
import urllib2
from bs4 import BeautifulSoup
url1 = ...
2
votes
3answers
41 views
Joining dictionary terms to form string
So, I have a dictionary of terms where each key is a word from a text file, and the value is a list of the next two words in that text file.
def dict(txt, n):
txt = txt.split()
output = {}
...
2
votes
1answer
51 views
Looking for a good way to split a string on all-capital words
For example I have an arbitrary string:
var = 'I have a string I want GE and APPLES but nothing else'
What's the best way to split the string in python so that I can obtain just 'GE' and 'APPLES'. ...
1
vote
1answer
75 views
how can i search for a specific word in python?
Is there a way to search a specific word in python, like 'or'.
I did this:
word = raw_input("what do you want to search? ")
for filee in open('text.txt'):
if word in filee:
print "found"
...
0
votes
0answers
12 views
Mutagen 1.22 Encoding Issue
I am having an issue with character encoding with Mutagen.
I casted the dict[key] to Unicode, bu all I receive are errors. The character in question is U+00E9 or é, but what I prints is ├⌐. I am ...
0
votes
4answers
33 views
Python List Index Comprehension
I have a class that returns a list of varying length and structure. A sample output is:
['Lead Attorneys', 'Defendant', 'Creighton, David Walter', '3661 Braeden Ct', 'Middleburg, FL 32068', ...
-1
votes
1answer
46 views
Function to create tree of words
I want to create a function that takes a string. From the string provided, I want to take one letter from the string and add it to the string in every position from the first position to the last.
I ...
0
votes
1answer
26 views
str.format() option is not working
This code was taken from the tutorial:
def main():
stri = "Hello, {person}"
stri.format(person="James")
print(stri) #prints "Hello, {person}"
Why the format() is not working?
0
votes
1answer
46 views
'str' object does not support item assignment python
XP = 0
nmin = 50
nmax = 2000
getxp = []
f = open("users/" + user.name.lower() + ".txt", 'r')
xpg = f.readline().split("X Points = ",1)[1]
f.close
elif used_prefix ...
3
votes
2answers
37 views
In Python (2.7.3) how do I write a function that answers if any characters in str(x) are in str(y) (or str(y) are in str(x))?
def char_check(x,y):
if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
return True
else:
return False
print "You will enter two words that you ...
0
votes
2answers
52 views
Is it possible to split a string on multiple delimiters in order?
I know how to split a string based on multiple separators using re as in this question: Python strings split with multiple separators. But I'm wondering how to split a string using the order given in ...
-1
votes
5answers
77 views
why do i get “Attribute Error: 'int' object has no attribute 'lower'”?
i need x to be an integer so my next part of code works, but as soon as i remove quotation marks around 0,1 or 2 where it says "making input readable for computer" i get this error message.
from ...
2
votes
4answers
40 views
Python - handling multiple str.replace calls better? [duplicate]
I often find myself wandering through large sets of text, extracting terms or otherwise cleaning things so I re-use a string as a filename or such like.
In a recent task, I grabbed a few hundred pdf ...
-1
votes
1answer
40 views
String Django Format Template
I have a problem, thats probably very simple,
I have a list of strings, that I pass as variable to a template called s_name
If I print the list elements on the view side I see them ok, but on the ...
0
votes
0answers
50 views
Input text file of strings and integers
I have a data file (ordered_data.txt) composed of 8 columns of integers and the last column is a string. It is 2912 rows long. I am inputting the data like so:
data = ...
0
votes
1answer
38 views
Single dictionary of fewer keys from a multi level dictionary
I am looking for a way to get values from variable depth in a multi level dictionary. I have a list of keys that are to be looked upon in the multi level dictionary. This key and value is to be put ...
0
votes
0answers
26 views
Handling the locations info of Github users
I am doing some analysis on Github users. In the analysis, I need to know where some person comes from(exact to city) via his personal info(I have got his personal info via Github API).
The trouble ...
1
vote
2answers
60 views
Can't convert 'list' object to str implicitly python
elif used_prefix and cmd == "xp":
if self.getAccess(user) >= 1:
f = open("users/" + user.name.lower() + ".txt", 'r')
word = f.readline().split("X Points ...
2
votes
3answers
72 views
Python, How to check all strings in a list?
website = str(raw_input('Website: '))
palavras_chaves = ['joomla', 'Joomla']
palavras_chaves2 = ['wordpress', 'Wordpress', 'WordPress', 'wp']
abrindo = urllib2.urlopen(website)
abrindo = ...
2
votes
1answer
28 views
Searching for a string within a csv file using python
I've read the official python documentation and as many posts about the topic as possible, but I think I need steering in the right direction. Maybe Python isn't even the right solution for what I'm ...
1
vote
1answer
38 views
Python Test for Full List
I have a list in Python. The list has 5 elements, which can either be "None" or any other string. I want to test if each element is not "None" (that is if all the elements are set).
I've written the ...
0
votes
3answers
92 views
What is the fastest way to convert string to array in python?
This is a line I read from a text file:
[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]
I used readline() to read it in as a string. Now what is the fastest way to convert it ...
0
votes
4answers
50 views
python strip() seems doesn't working as expected
I do my apologizes for the dummy question, but i'm experiencing a weird problem with a simple script that seems correct but doesnt' works as expected
#!/usr/bin/python
import json,sys
...
3
votes
2answers
55 views
Python raw_input messing up string concatenation
I am trying to do something relatively simple in Python and am surprised at how badly this isn't working for how simple it should be.
I'm here just trying to concatenate three simple strings. The ...
2
votes
1answer
43 views
Writing a small piece of code to sum up digits in a string, through using a try-except block
Background: One of the "Finger Exercises" in my introductory textbook has me try to write such a program to teach me how to use a try-except block. The textbook is designed to accompany the '6.00x' ...
1
vote
1answer
36 views
Why does function behavior used within pandas apply change?
I cannot figure out why a simple function:
def to_integer(value):
if value == "":
return None
return int(value)
changes values from str to int only if there's no empty string "" ...
-3
votes
2answers
35 views
Check if any part of a string matches a given string in python
How would one check if any part of a string matches a given string.
For example, if I had the string "tyghfjsf" and wanted to check if the specific substring "ghf" was in the string, how would I do ...
0
votes
2answers
61 views
Create a list from file in python
I have a .txt file like this:
John 26
Mary 48
Nick 34
I want import them and put them in a list so that I can find specific elements. For example age[1] would have the value 48, name[1] the value ...
0
votes
3answers
37 views
How to reference a regex match as a variable in python
So here's my problem:
I need to separate these punctuation items '], [, ?, !, (, ), ", ;, {, }' from whatever character they touch with a space. For example,
"Did he eat it (the bug)?" becomes: " ...
0
votes
1answer
56 views
Python if in string multiple times
Here's my script of sorting males and females one after another into the variable 'group':
males = ['John Smith 1 : M', 'John Smith 2 : M', 'John Smith 3 : M', 'John Smith 4 : M', 'John Smith 5 : M', ...
-1
votes
2answers
42 views
Add elements of string in python [on hold]
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
34 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
47 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
38 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 ...
4
votes
2answers
107 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
25 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
40 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
50 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
44 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
44 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
66 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
71 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
1answer
63 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
67 views
Formating Python strings with \n [closed]
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
41 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
55 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 ...