things can be that easy.
$string = "<TEXT>
Some text
Another text
</TEXT>";
@s=split /<\/TEXT>/, $string;
$s[0] =~ s/.*<TEXT>// ;
print $s[0];
things can be that easy.
$string = "<TEXT>
Some text
Another text
</TEXT>";
@s=split /<\/TEXT>/, $string;
$s[0] =~ s/.*<TEXT>// ;
print $s[0];
>>> a = 'nf+nfpermult+nf'
>>> a=a.split("+")
>>> for n,i in enumerate( a ):
... if i == "nf": a[n]="1.0"
...
>>> print '+'.join(a)
1.0+nfpermult+1.0
if you want to use regex, you should compile your regex with re.DOTALL and re.M for multiline match.
use a html parser for this job, such as BeautifulSoup. If you don't want to, then another way is to read the whole html, split on "</div>", go through each element in the list, check for "<div class="info-content">", if found, replace it will null. You will get your string
you do it systematically using the same way to read single level dict
d={'AAA': {'KEY1':'text1', 'KEY2':'text2', 'KEY3': 'text3', 'KEY4': 'text4'},
'BBB': {'BBB1':{'KEY1':'text1', 'KEY2':'text2', 'KEY3': 'text3', 'KEY4': 'text4'},
'BBB2':{'KEY1':'text1', 'KEY2':'text2', 'KEY3': 'text3', 'KEY4': 'text4'},
'BBB3':{'KEY1':'text1', 'KEY2':'text2', 'KEY3': 'text3', 'KEY4': 'text4'}}}
for i,j in d.iteritems():
for m,n in j.iteritems():
print m,n
do the checking of "AAA", "BBB" yourself.
Ok the issue i have worked out is when there seems to be a space on the new line on the file.
i.e.
cat apps/tech/testruniquefailures
<space is here>
batchjob1
batchjob2How do i make sure the above file does not have a space on the top of it.
don't use useless cat with while loop
while read -r line
do
#processing
done < "file"
'
anyway, here's another way to solve your problem
# more file
C
D
# more file1
A
B
C
D
E
C
D
C
D
A
# grep -f file file1 | sort | uniq -c
3 C
3 D
no need to use cat
uuencode $file $file | /usr/bin/mailx -s "TEST"
awk 'BEGIN{FS="[]].[[]|[[]|[]]"}
{
gsub(/id|\"/,"",$13)
split($3,a,"/")
gsub("tag","",$16)
print $13,a[1],$7,$16
}' file
# echo $HTML | sed 's/<html><body>\(.*\)<\/body><\/html>/\1/'
OK
it would be better to show a sample input file, your expected output as well.
to search in a file. just an example...considering i don't know what your file structure is like.
for line in open("file"):
if "john" in line:
d['john'] = line
another way
>>> for i in range(len(sentence.split()),0,-1):
... print sentence.split()[i-1],
yes, you can put those into a script. use loops to create a counter for your files.
eg psedocode
count=1
awk '{print NR SEP $0}' SEP="/ " exfile1.txt
for counter in the range 1 to the number of files:
cp exfile1.txt try$counter.txt
something like that.
awk '{print NR SEP $0}' SEP="/ " exfile1.txt > try2.txt
here is a awk scritp which copies the contents form one to anthor
it works well, but how do I change it to copy the contents in to several files ? can any one help please
you can invoke the awk script again, or just use cp,
like: cp try2.txt <newfilename>
Personally, i would use a loop for this.
num = 32
b1 = 10
b2 = 2
x = 1
while 1:
if num%(b2**x) < num:
x = x + 1
continue
else:
break
print x
I will give that a try, do I need the def=main etc as well, or can I leave that out??
Thanks you
it's up to you. however its good to modularize so main() is definitely good to have.
you can use splitext from os module
import os,shutil
def renamer(target) :
os.chdir(target)
for filename in os.listdir('.') :
print filename
newfilename = os.path.splitext(filename)[0]
print newfilename
os.rename(filename, newfilename)
print "Renamed " + filename + " to " + new_filename
shutil.copy("copyme","newcopyme"
:sad: Please could some please help me out here . I have an array. I want to count how my 'a' appear and the a must have its quotes 'a'
[(' ', ' ', [('a', 'Scott'), ('9', 'vth')]), (' ', ' ', [('a', 'Jenny'), ('9', 'vth')])]
Like this one has 'a' = 2
For your particular example, you can convert your list to a string, then find 'a'
>>> a = [(' ', ' ', [('a', 'Scott'), ('9', 'vth')]), (' ', ' ', [('a', 'Jenny'), ('9', 'vth')])]
>>> b = str(a)
>>> print b.count("'a'")
2
Is there a reliable way to 'pluralize' english words with Python?
there are some cookbook recipe in ASPN you can refer to.
here.
they may not be wat you want, but at least will give you a head start
you should read the docs more. also googling with "Python word count" will show you some links to what you are looking for.
I want to see if a certain word is in a text file, how do I go about that?
you can read in line by line
for line in open("file"):
if "word" in line:
do_something(line)
you can read in one whole chunk
data = open("file").read()
if "someword" in data:
do_something()
I need to read in a text file, replace selected words and write the changed text back out to a file. Is there an easy way with Python?
there are several ways.
1) reading with for loop
o = open("output","a") #open for append
for line in open("file"):
line = line.replace("someword","newword")
o.write(line + "\n")
o.close()
2) with while loop
f = open("file")
o = open("output","a")
while 1:
line = f.readline()
if not line: break
line = line.replace("someword","newword")
o.write(line + "\n")
o.close()
3) using regular expression and reading the whole file into memory
import re
o = open("output","w")
data = open("file").read()
o.write( re.sub("someword","newword",data) )
o.close()
If you don't want to explicitly declare file handles for writing to output file, you can use fileinput module, to modify file in place
import fileinput
for line in fileinput.FileInput("file",inplace=1):
line = line.replace("blah","blahblah")
print line
Or you could do this
import operator
a = (1,2,3,4)
b = (4,3,2,1)
print sum(map(operator.mul,a,b))
Thanks guys this helps alot, the only thing now though, is that if I have, let's say, 2.0, both of those suggested things return false (unless I'm using them wrong)
I need to find the prescense of a whole number, not if a decimal is there or not. Is there a way to do that?
Do you want to include decimals like 2.0 , 2.00000 etc as a whole number ??
This re stuff makes my head spin! I can see that it is very powerful for text processing, but also seemingly very complex! Almost another language within Python.
Most string manipulation problems can be solved with Python's string functions. Only very complex ones will need regexp. So try not to use regexp if possible. Of course if you are good at it , then by all means, but have to think of the next person reading your code and who don't understand regexp. Just my $0.02 :cheesy:
you need to put a decimal before doing the division:
>>> 6.0/4
1.5
>>> 6/4.0
1.5
or you can import future:
>>> from __future__ import division
>>> 6/4
1.5
>>>
Two nice solutions, wow! Now I have a question, what if we had a "-$245.99" to extract so it would give "-245.99"?
hi, wow, getter harder.:-)
anyway, here's a rather crude way and i am sure there are better ways (using re). I did substitution first, then do the rest
>>> data_raw = """
... header
... [23 ]
... [ 43 ]
... [4323]
... [-$23.44 ]
... [ 12.32 ]
... footer
... """
>>>
>>> re.findall(r"(-\d+\.\d+|\d+\.\d+|\d+)",re.sub(r"(-\$)","-",data_raw))
['23', '43', '4323', '-23.44', '12.32']
I still prefer not to use re though lol:)
another way is to use the "|" special character.
>>> re.findall(r'\d+\.\d+|\d+',data_raw) ['20', '35', '40', '84', '100', '245.99'] >>>
sorry somehow i can't find my edit button, but anyway
a decimal/float looks like this : 245.332 or 4.5 or 74.32
so to match them, we need one or more digits, followed by "." and followed by one or more digits again..so the expression becomes "\d+\.\d+".
another way is to use the "|" special character.
>>> re.findall(r'\d+\.\d+|\d+',data_raw)
['20', '35', '40', '84', '100', '245.99']
>>>
i think its answered in the other thread
This was quite a lively discussion!
A question to ghostDog74, can you use regular expression to simply extract the number from each dataline, no matter what the non-numeric stuff is?
hi there
sure, re.findall(r"(\d+)",data_raw)
thanks
assuming data is as first posted:
>>> import re
>>> re.findall(r"\[(.*)\]",data_raw)
['20 ', ' 35 ', '40 ', '84 ', '100 ', ' 245', ' 260', ' 300 ', ' 440 ', ' 521 ', ' 650 ']
or if they contain spaces like the other posts...
>>> [ i.strip() for i in re.findall(r"\[(.*)\]",data_raw) ]
['20', '35', '40', '84', '100', '245', '260', '300', '440', '521', '650']
can you try :
dates = [i for i in files for j in months if j in i ]
I took a mixed type list and set out to find the min and max values. The results are very surprising to me:
mixed_list = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789] mn = min(mixed_list) mx = max(mixed_list) print mn, type(mn) # 7 <type 'int'> print mx, type(mx) # Zoe <type 'str'>
How does Python handle mixed type lists to get such a result?
another way...but you have to test it in different cases...
lst = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789]
slist = sorted(lst) #sort the list
min_num , max_str = slist[0], slist[-1] #first and last element usually is minnum, max str.
for num,item in enumerate(slist):
try:
if item.isalpha(): #check for first occurence of a word.
min_str = item
max_num = slist[num-1] #get the number on the left of this word. Usually is max num
break
except: pass
print min_num ,max_num , max_str , min_str
what is your error, and what is your expected output/results?
if you don't want to change list1, you can do
list1dup = list1[:]
list3 = add_item(list1dup)
...
if you just want to find "Paul" in your example, one way to do it is to convert that list to string
astring = ','.join( str(i) for i in nested_list)
if "Paul in astring:
print "Found"
this is the problem
grade = input("Enter gradepoint for course (Enter to quit): ")
input() expects a number. when you just press enter, it gives error
maybe a try:except can solve the problem...
is there an easy way to detect if the entire contents of one list are in another?
if I have a list like this: li = [1,2]
and I have another which will have random values, I want the program to do something only if every value of li is in the second list.I need a way to program this:
x = random.randint(0,10) y = random.randint(0,10) li = [1,2] li2 = [x,y] if every value of li2 is in li: do this
if your lists comparing only numbers, you can sort them first?
if sorted(li) == sorted(li2):
do something
def en_de_code(text,flag=0):
if flag == 0:
return ''.join([chr(ord(char) + 1) for char in text ]) #encode
else:
return ''.join([chr(ord(char) - 1) for char in text ]) #decode
Thanks for the suggestion ghostdog74. I was hoping for a more straightforward approach. The debug approach seems to output the entire e-mail content, and this might involve performance loss if the attachments are very large.
Standard python sockets seem to be able to capture bytes sent/received:
while 1: data = conn.recv(1024) if not data: break
I wonder if a similar approach can be used with smtplib?
i have not done this before, but i guess you could look at the smtplib.py module source itself to see if its possible...
Is it then the keys of the dictionary that are immutable? Not the entire dict? I have read about this much and many sources claim that Python dicts "are immutable." I just did a search again about dicts, and came upon a page that claims that "lists are immutable" :rolleyes: .
I think much of the on-line info is incorrect as much of it directly contadicts other sources and seems incomplete. It is difficult to find the truth. Frustrating.
Thanks for your earlier reply.:)
Regards,
sharky_machine
In Python docs first para here http://www.python.org/doc/2.4.1/lib/typesmapping.html
"""
2.3.8 Mapping Types -- classdict
A mapping object maps immutable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. A dictionary's keys are almost arbitrary values. Only values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as 1 and 1.0) then they can be used interchangeably to index the same dictionary entry.
"""
from Tkinter import *
import math
##import sys
sys.frozen = 1
.....
do you want to comment out "import sys" and try again?
I would post this in my other thread, but I marked that as solved so I think less people would look at it. I want to say this:
if list1 is empty: do this elif list1 has any value in it: do this
How do I word that in python?
>>> a = []
>>> if a:
... print "not empty"
...
>>> b = ['a']
>>> if b:
... print "not empty"
...
not empty
>>>
or you can use len()
say you have a list:
list = [1,a,2,b,3,c,4,d,5,e,6,f,7,g,8,h,9,i,10,j,11,k,12,l,13]
What's the easiest loop set up if I want to store each number (every other slot) into another list? The above list is actually text from a file read and then split with "1 a" and such on each line of the file. After I do that, I want the other slots put into another list. Also, let's say the above letters are different and there are multiples of the same letter, I want to delete the multiples and be left with a list of all the unique letters in the original list.
Ex: if the list is [1,a,2,a,3,v,4,t,5,v,9,c], I want [1,2,3,4,5,9] and [a,c,t,v] (numerical and alphabetic order respectively)
Thanks
hmm.list comprehension?
alpha = [ c for c in list if str(c).isalpha()]
number = [ c for c in list if str(c).isdigit()]
if you want to get unique elements, use set
I thought function xrange would give me a list of integers, but I got a string instead:
x = "xrange(1, 5)" print x # xrange(1, 5) x = xrange(1, 5) print x # xrange(1, 5) same result as string above!?
if you look at the xrange() docs. it says "returns an ``xrange object'' instead of a list"
if you want to use xrange(). use a for loop
for i in xrange(1,5):.....
Greetings All!
I would like to get some feedback to all ya good python folks here at daniweb regarding some implementations that you guys doing with Python and using MySQL db. I'm looking for some mysql plugin for python. I've tried sourceforge 's mysql-python but with no luck(installing) .. maybe someone here can help me out on some alternatives.
thanks all! :mrgreen:
how about http://www.sqlite.org
it's best if you could explain what you wish to do, give sample input and output if possible
Maybe my question was a little bit wrong. I would like to reverse a nibble. I have a dodgy solution but it works...
Now I have a different issue. I would like
temp1psc :
to look like
Any ideas?
''.join()
Does all the lines you want end in "BANK"?