Re: delete from pattern to pattern if it contains match
harirammano...@gmail.com writes: > Hi Jussi, > > i have seen you have written a definition to fulfill the requirement, > can we do this same thing using xml parser, as i have failed to > implement the thing using xml parser of python if the file is having > the content as below... > > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" > "http://java.sun.com/dtd/web-app_2_3.dtd";> > > > > and entire thing works if it has as below: > > > what i observe is xml tree parsing is not working if http tags are > there in between web-app... Do you get an error message? My guess is that the parser needs the DTD but cannot access it. There appears to be a DTD at that address, http://java.sun.com/... (it redirects to Oracle, who bought Sun a while ago), but something might prevent the parser from accessing it by default. If so, the details depend on what parser you are trying to use. It may be possible to save that DTD as a local file and point the parser to that. Your problem is morphing rather wildly. A previous version had namespace declarations but no DTD or XSD if I remember right. The initial version wasn't XML at all. If you post (1) an actual, minimal document, (2) the actual Python commands that fail to parse it, and (3) the error message you get, someone will be able to help you. The content of the document need not be more than "hello, world" level. The DOCTYPE declaration and the outermost tags with all their attributes and namespace declarations, if any, are important. -- https://mail.python.org/mailman/listinfo/python-list
Writing different sections into a file
Hi, I need to write different sections into a file. At any point of time, content can be added to any section. I don't want keep each section into a temporary file. What is the better way to store the contents of each section and write them into a file at the end? What is the better datatype to achieve this? Thanks and Regards, Palpandi -- https://mail.python.org/mailman/listinfo/python-list
Re: Optimizing Memory Allocation in a Simple, but Long Function
Derek Klinge wrote: Also, it seems to me if the goal is to use the smallest value of n to get a particular level of accuracy, changing your guess of N by doubling seems to have a high chance of overshoot. If you want to find the exact n required, once you overshoot you could use a binary search to narrow it down. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Optimizing Memory Allocation in a Simple, but Long Function
Derek Klinge wrote: > I found that the pattern of an additional digit of accuracy corresponding > to 10*n did not hold as strongly for that value (I can post data if > desired). I also got some results that seem to contradict the mathematical > definition. For example try EulersNumber(10**15).LimitMethod(), the > definition places this limit at e, and yet the (python) answer is >3.035. > Please let me know if I've fouled up the implementation somehow. There's nothing wrong with the formula, but floating point numbers have limited precision. At some point your calculation will become mostly a rounding error. You'll see the surprising >>> n = 10**14 >>> (1+1/n)**n 2.716110034087023 >>> n = 10**15 >>> (1+1/n)**n 3.035035206549262 on most modern computers with most programming langagues. https://docs.python.org/3.5/tutorial/floatingpoint.html has some introductory information. -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing different sections into a file
Palpandi wrote: > I need to write different sections into a file. > At any point of time, content can be added to any section. > > I don't want keep each section into a temporary file. > What is the better way to store the contents of each section and write > them into a file at the end? What is the better datatype to achieve this? If the data easily fits into memory just keep it in one list per section header = [...] body = [...] footer = [...] ... body.append("something more\n") footer[:] = ["new footer\n"] ... with open(filename, "w") as f: for section in (header, body, footer): f.writelines(section) If the sections are not known in advance put them into a dictionary or a list. If you are free to choose the file format you may use https://docs.python.org/dev/library/configparser.html -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
On Monday, April 25, 2016 at 12:47:14 PM UTC+5:30, Jussi Piitulainen wrote: > harirammano...@gmail.com writes: > > > Hi Jussi, > > > > i have seen you have written a definition to fulfill the requirement, > > can we do this same thing using xml parser, as i have failed to > > implement the thing using xml parser of python if the file is having > > the content as below... > > > > > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" > > "http://java.sun.com/dtd/web-app_2_3.dtd";> > > > > > > > > and entire thing works if it has as below: > > > > > > > what i observe is xml tree parsing is not working if http tags are > > there in between web-app... > > Do you get an error message? > > My guess is that the parser needs the DTD but cannot access it. There > appears to be a DTD at that address, http://java.sun.com/... (it > redirects to Oracle, who bought Sun a while ago), but something might > prevent the parser from accessing it by default. If so, the details > depend on what parser you are trying to use. It may be possible to save > that DTD as a local file and point the parser to that. > > Your problem is morphing rather wildly. A previous version had namespace > declarations but no DTD or XSD if I remember right. The initial version > wasn't XML at all. > > If you post (1) an actual, minimal document, (2) the actual Python > commands that fail to parse it, and (3) the error message you get, > someone will be able to help you. The content of the document need not > be more than "hello, world" level. The DOCTYPE declaration and the > outermost tags with all their attributes and namespace declarations, if > any, are important. Hi Jussi, Here is an input file...sample.xml http://xmlns.jcp.org/xml/ns/javaee"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"; version="3.1"> controller com.mycompany.mypackage.ControllerServlet listOrders com.mycompany.myactions.ListOrdersAction saveCustomer com.mycompany.myactions.SaveCustomerAction 5 graph /graph 30 Here is the code: import xml.etree.ElementTree as ET ET.register_namespace("", "http://xmlns.jcp.org/xml/ns/javaee";) tree = ET.parse('sample.xml') root = tree.getroot() for servlet in root.findall('servlet'): servletname = servlet.find('servlet-name').text if servletname == "controller": root.remove(servlet) tree.write('output.xml') This will work if doesnt have below... xmlns="http://xmlns.jcp.org/xml/ns/javaee"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"; -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
On Monday, April 25, 2016 at 3:19:15 PM UTC+5:30, hariram...@gmail.com wrote: > On Monday, April 25, 2016 at 12:47:14 PM UTC+5:30, Jussi Piitulainen wrote: > > harirammano...@gmail.com writes: > > > > > Hi Jussi, > > > > > > i have seen you have written a definition to fulfill the requirement, > > > can we do this same thing using xml parser, as i have failed to > > > implement the thing using xml parser of python if the file is having > > > the content as below... > > > > > > > > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" > > > "http://java.sun.com/dtd/web-app_2_3.dtd";> > > > > > > > > > > > > and entire thing works if it has as below: > > > > > > > > > > > what i observe is xml tree parsing is not working if http tags are > > > there in between web-app... > > > > Do you get an error message? > > > > My guess is that the parser needs the DTD but cannot access it. There > > appears to be a DTD at that address, http://java.sun.com/... (it > > redirects to Oracle, who bought Sun a while ago), but something might > > prevent the parser from accessing it by default. If so, the details > > depend on what parser you are trying to use. It may be possible to save > > that DTD as a local file and point the parser to that. > > > > Your problem is morphing rather wildly. A previous version had namespace > > declarations but no DTD or XSD if I remember right. The initial version > > wasn't XML at all. > > > > If you post (1) an actual, minimal document, (2) the actual Python > > commands that fail to parse it, and (3) the error message you get, > > someone will be able to help you. The content of the document need not > > be more than "hello, world" level. The DOCTYPE declaration and the > > outermost tags with all their attributes and namespace declarations, if > > any, are important. > > Hi Jussi, > > Here is an input file...sample.xml > > > http://xmlns.jcp.org/xml/ns/javaee"; > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; > xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee > http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"; > version="3.1"> > > controller > com.mycompany.mypackage.ControllerServlet > > listOrders > com.mycompany.myactions.ListOrdersAction > > > saveCustomer > com.mycompany.myactions.SaveCustomerAction > > 5 > > > > > graph > /graph > > > > > 30 > > > > > Here is the code: > > import xml.etree.ElementTree as ET > ET.register_namespace("", "http://xmlns.jcp.org/xml/ns/javaee";) > tree = ET.parse('sample.xml') > root = tree.getroot() > > for servlet in root.findall('servlet'): > servletname = servlet.find('servlet-name').text > if servletname == "controller": > root.remove(servlet) > > tree.write('output.xml') > > This will work if doesnt have below... > > xmlns="http://xmlns.jcp.org/xml/ns/javaee"; > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; > xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee > http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"; By the way i didnt get any error message and i am using version 3.4.3 -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing different sections into a file
On Monday, April 25, 2016 at 1:01:12 PM UTC+5:30, Palpandi wrote: > Hi, > > I need to write different sections into a file. > At any point of time, content can be added to any section. > > I don't want keep each section into a temporary file. > What is the better way to store the contents of each section and write them > into a file at the end? > What is the better datatype to achieve this? > > > Thanks and Regards, > Palpandi use ConfigParser.. -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing different sections into a file
On 25/04/2016 09:30, Palpandi wrote: Hi, I need to write different sections into a file. At any point of time, content can be added to any section. I don't want keep each section into a temporary file. What is the better way to store the contents of each section and write them into a file at the end? What is the better datatype to achieve this? Thanks and Regards, Palpandi Use Stringio: - from cStringIO import StringIO content = StringIO() # Header content.write('; Header\n') content.write('; Body'\n) content.write('; Footer\n') open('my_file', 'wb').write(content.getvalue()) - Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
harirammano...@gmail.com wrote: > Here is the code: Finally ;) > import xml.etree.ElementTree as ET > ET.register_namespace("", "http://xmlns.jcp.org/xml/ns/javaee";) I don't know what this does, but probably not what you expected. > tree = ET.parse('sample.xml') > root = tree.getroot() > > for servlet in root.findall('servlet'): > servletname = servlet.find('servlet-name').text I think you have to specify the namespace: for servlet in root.findall('{http://xmlns.jcp.org/xml/ns/javaee}servlet'): servletname = servlet.find( '{http://xmlns.jcp.org/xml/ns/javaee}servlet-name').text > if servletname == "controller": You could have added a print statement to verify that the line below is executed. > root.remove(servlet) > > tree.write('output.xml') > > This will work if doesnt have below... > > xmlns="http://xmlns.jcp.org/xml/ns/javaee"; > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; > xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee > http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"; -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
harirammano...@gmail.com writes: > On Monday, April 25, 2016 at 12:47:14 PM UTC+5:30, Jussi Piitulainen wrote: >> harirammano...@gmail.com writes: >> >> > Hi Jussi, >> > >> > i have seen you have written a definition to fulfill the requirement, >> > can we do this same thing using xml parser, as i have failed to >> > implement the thing using xml parser of python if the file is having >> > the content as below... >> > >> > > > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" >> > "http://java.sun.com/dtd/web-app_2_3.dtd";> >> > >> > >> > >> > and entire thing works if it has as below: >> > > > >> > >> > what i observe is xml tree parsing is not working if http tags are >> > there in between web-app... >> >> Do you get an error message? >> >> My guess is that the parser needs the DTD but cannot access it. There >> appears to be a DTD at that address, http://java.sun.com/... (it >> redirects to Oracle, who bought Sun a while ago), but something might >> prevent the parser from accessing it by default. If so, the details >> depend on what parser you are trying to use. It may be possible to save >> that DTD as a local file and point the parser to that. >> >> Your problem is morphing rather wildly. A previous version had namespace >> declarations but no DTD or XSD if I remember right. The initial version >> wasn't XML at all. >> >> If you post (1) an actual, minimal document, (2) the actual Python >> commands that fail to parse it, and (3) the error message you get, >> someone will be able to help you. The content of the document need not >> be more than "hello, world" level. The DOCTYPE declaration and the >> outermost tags with all their attributes and namespace declarations, if >> any, are important. > > Hi Jussi, > > Here is an input file...sample.xml > > > http://xmlns.jcp.org/xml/ns/javaee"; > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; > xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee > http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"; > version="3.1"> > > controller > com.mycompany.mypackage.ControllerServlet > > listOrders > com.mycompany.myactions.ListOrdersAction > > > saveCustomer > com.mycompany.myactions.SaveCustomerAction > > 5 > > > > > graph > /graph > > > > > 30 > > > > > Here is the code: > > import xml.etree.ElementTree as ET > ET.register_namespace("", "http://xmlns.jcp.org/xml/ns/javaee";) > tree = ET.parse('sample.xml') > root = tree.getroot() > > for servlet in root.findall('servlet'): > servletname = servlet.find('servlet-name').text > if servletname == "controller": > root.remove(servlet) > > tree.write('output.xml') > > This will work if doesnt have below... > > xmlns="http://xmlns.jcp.org/xml/ns/javaee"; > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; > xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee > http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"; It's a namespace issue, and your method of registering a default namespace isn't working. It's a frustrating failure mode: no error message, no nothing :) Try defining a namespace prefix in your method calls, and using that prefix in element names: ns = { 'x' : "http://xmlns.jcp.org/xml/ns/javaee"; } for servlet in root.findall('x:servlet', ns): servletname = servlet.find('x:servlet-name', ns).text I got this from here: https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces Note that the namespace prefix - I chose to use 'x' - has no meaning. It's the association of the prefix that you use to the URI that is the name of the namespace that does the job. -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
Peter Otten writes: > harirammano...@gmail.com wrote: > >> Here is the code: > > Finally ;) :) -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
harirammano...@gmail.com writes: > On Monday, April 25, 2016 at 3:19:15 PM UTC+5:30, hariram...@gmail.com wrote: [- -] >> Here is the code: >> >> import xml.etree.ElementTree as ET >> ET.register_namespace("", "http://xmlns.jcp.org/xml/ns/javaee";) >> tree = ET.parse('sample.xml') >> root = tree.getroot() >> >> for servlet in root.findall('servlet'): >> servletname = servlet.find('servlet-name').text >> if servletname == "controller": >> root.remove(servlet) >> >> tree.write('output.xml') [- -] > By the way i didnt get any error message and i am using version 3.4.3 Right. The parsing succeeds but no 'servlet' elements are found and the loop simply has no effect. I may be missing some technical detail, but I think the 'servlet' elements in the document are in the default namespace (because one was declared) while your .findall and .find calls are looking for a 'servlet' element that is in no namespace at all. I seem to remember that there is such a distinction in XML. -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
On Monday, April 25, 2016 at 4:09:26 PM UTC+5:30, Jussi Piitulainen wrote: > Peter Otten writes: > > > harirammano...@gmail.com wrote: > > > >> Here is the code: > > > > Finally ;) > > :) name space issue can be resolved registering name space i have no issue with that, only concern is xml parser has no effect when http things are added... -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
harirammano...@gmail.com writes: > On Monday, April 25, 2016 at 4:09:26 PM UTC+5:30, Jussi Piitulainen wrote: >> Peter Otten writes: >> >> > harirammano...@gmail.com wrote: >> > >> >> Here is the code: >> > >> > Finally ;) >> >> :) > > name space issue can be resolved registering name space i have no > issue with that, only concern is xml parser has no effect when http > things are added... No, the parser works fine. Your attempt to register a default namespace didn't work. Those "http things" *are* the namespace issue! The following version of your code works. *Try it.* It finds the servlet element in the document object, removes it, and writes out XML text without the servlet element. (It seems to invent another namespace prefix. That doesn't change the meaning of the document.) import xml.etree.ElementTree as ET ns = { 'x' : "http://xmlns.jcp.org/xml/ns/javaee"; } tree = ET.parse('sample.xml') root = tree.getroot() for servlet in root.findall('x:servlet', ns): servletname = servlet.find('x:servlet-name', ns).text if servletname == "controller": root.remove(servlet) tree.write('output.xml') -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
On Monday, April 25, 2016 at 4:58:15 PM UTC+5:30, Jussi Piitulainen wrote: > harirammano...@gmail.com writes: > > > On Monday, April 25, 2016 at 4:09:26 PM UTC+5:30, Jussi Piitulainen wrote: > >> Peter Otten writes: > >> > >> > harirammano...@gmail.com wrote: > >> > > >> >> Here is the code: > >> > > >> > Finally ;) > >> > >> :) > > > > name space issue can be resolved registering name space i have no > > issue with that, only concern is xml parser has no effect when http > > things are added... > > No, the parser works fine. Your attempt to register a default namespace > didn't work. Those "http things" *are* the namespace issue! > > The following version of your code works. *Try it.* It finds the servlet > element in the document object, removes it, and writes out XML text > without the servlet element. (It seems to invent another namespace > prefix. That doesn't change the meaning of the document.) > > import xml.etree.ElementTree as ET > > ns = { 'x' : "http://xmlns.jcp.org/xml/ns/javaee"; } > > tree = ET.parse('sample.xml') > root = tree.getroot() > > for servlet in root.findall('x:servlet', ns): > servletname = servlet.find('x:servlet-name', ns).text > if servletname == "controller": > root.remove(servlet) > > tree.write('output.xml') yup its working well if i include register namespace, else i am getting ns:0 in every line of output.xml. But its removing top line -- https://mail.python.org/mailman/listinfo/python-list
RE: delete from pattern to pattern if it contains match
I put some code I did before for the xmlns: xml_root = ET.ElementTree(ET.fromstring(xml_decoded)).getroot() for elem in xml_root.getiterator(): if('{http://request.messagepush.interfaces.comviva.com/xsd}shortCode'==elem.tag): shortCode = (elem.text).rstrip() if('{http://request.messagepush.interfaces.comviva.com/xsd}text'==elem.tag): send_text = (elem.text).rstrip() if('{http://request.messagepush.interfaces.comviva.com/xsd}item'==elem.tag): subscribers = (elem.text).rstrip() result_sms = send_sms(subscribers,shortCode,send_text) Reuse it. -Original Message- From: Python-list [mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf Of Peter Otten Sent: 25 April 2016 11:14 To: python-list@python.org Subject: Re: delete from pattern to pattern if it contains match harirammano...@gmail.com wrote: > Here is the code: Finally ;) > import xml.etree.ElementTree as ET > ET.register_namespace("", "http://xmlns.jcp.org/xml/ns/javaee";) I don't know what this does, but probably not what you expected. > tree = ET.parse('sample.xml') > root = tree.getroot() > > for servlet in root.findall('servlet'): > servletname = servlet.find('servlet-name').text I think you have to specify the namespace: for servlet in root.findall('{http://xmlns.jcp.org/xml/ns/javaee}servlet'): servletname = servlet.find( '{http://xmlns.jcp.org/xml/ns/javaee}servlet-name').text > if servletname == "controller": You could have added a print statement to verify that the line below is executed. > root.remove(servlet) > > tree.write('output.xml') > > This will work if doesnt have below... > > xmlns="http://xmlns.jcp.org/xml/ns/javaee"; > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; > xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee > http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"; -- https://mail.python.org/mailman/listinfo/python-list This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
harirammano...@gmail.com writes: > On Monday, April 25, 2016 at 4:58:15 PM UTC+5:30, Jussi Piitulainen wrote: >> harirammano...@gmail.com writes: >> >> > On Monday, April 25, 2016 at 4:09:26 PM UTC+5:30, Jussi Piitulainen wrote: >> >> Peter Otten writes: >> >> >> >> > harirammano...@gmail.com wrote: >> >> > >> >> >> Here is the code: >> >> > >> >> > Finally ;) >> >> >> >> :) >> > >> > name space issue can be resolved registering name space i have no >> > issue with that, only concern is xml parser has no effect when http >> > things are added... >> >> No, the parser works fine. Your attempt to register a default namespace >> didn't work. Those "http things" *are* the namespace issue! >> >> The following version of your code works. *Try it.* It finds the servlet >> element in the document object, removes it, and writes out XML text >> without the servlet element. (It seems to invent another namespace >> prefix. That doesn't change the meaning of the document.) >> >> import xml.etree.ElementTree as ET >> >> ns = { 'x' : "http://xmlns.jcp.org/xml/ns/javaee"; } >> >> tree = ET.parse('sample.xml') >> root = tree.getroot() >> >> for servlet in root.findall('x:servlet', ns): >> servletname = servlet.find('x:servlet-name', ns).text >> if servletname == "controller": >> root.remove(servlet) >> >> tree.write('output.xml') > > yup its working well if i include register namespace, else i am > getting ns:0 in every line of output.xml. That's a namespace prefix for each element name that is in the default namespace. If the ET.register_namespace has the effect of making that the default namespace in the output, fine, you can use it. The important thing is that you can read your output.xml back in, using the XML parser, and it has the intended meaning. > But its removing top line > Not a problem. You can still read your output.xml back in, using the XML parser, and it will have the same meaning as it would have had with this declaration. -- https://mail.python.org/mailman/listinfo/python-list
Re: delete from pattern to pattern if it contains match
harirammano...@gmail.com wrote: >> tree.write('output.xml') > > yup its working well if i include register namespace, else i am getting > ns:0 in every line of output.xml. > > But its removing top line > The write() method allows you to specify an encoding and/or require an xml declaration: https://docs.python.org/dev/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.write -- https://mail.python.org/mailman/listinfo/python-list
Re: Optimizing Memory Allocation in a Simple, but Long Function
On 25 April 2016 at 08:39, Gregory Ewing wrote: > Derek Klinge wrote: >> >> Also, it seems to me if the goal is to use the smallest value of n to get >> a >> particular level of accuracy, changing your guess of N by doubling seems >> to >> have a high chance of overshoot. > > > If you want to find the exact n required, once you overshoot > you could use a binary search to narrow it down. Also you can calculate the truncation error for Euler's method. Since f(t) = f(t0) + f'(t0)*(t - t0) + (1/2)f''(t0)*(t - t0)**2 + O((t - t0)**3) Euler's method just uses the first two terms so x[n+1] = x[n] + dt*f(x[n]) the next term would be (1/2)*f'(x[n])*dt**2 Since in your case f'(x) = x and dt = 1/N that's (1/2)*x[n]*(1/N)**2 As a relative error (divide by x[n]) that's (1/2)*(1/N)**2 Let's add the relative error from N steps to get N*(1/2)*(1/N)**2 = 1/(2*N) So the relative error integrating from 0 to 1 with N steps is 1/(2*N). If we want a relative error of epsilon then the number of steps needed is 1/(2*epsilon). That is to say that for a relative error of 1e-4 we need N = 1/(2*1e-4) = 1e4/2 = 5e3 = 5000. >>> import math >>> N = 5000 >>> error = math.e - (1 + 1.0/N)**N >>> relative_error = error / math.e >>> relative_error 9.998167027596845e-05 Which is approximately 1e-4 as required. -- Oscar -- https://mail.python.org/mailman/listinfo/python-list
what is the difference between one-line-operation and 2-line-operation
for a simple code [code] vexList = [1, 2, 3] print('vexList', list(vexList)) vexList=map(lambda e: e+1, vexList) print('vexList', list(vexList)) vexList = list(vexList) print('vexList', list(vexList)) vexList=map(lambda e: e*2,vexList) print('vexList', list(vexList)) [/code] py27 says [quote] ('vexList', [1, 2, 3]) ('vexList', [2, 3, 4]) ('vexList', [2, 3, 4]) ('vexList', [4, 6, 8]) [/quote] but py34 says [quote] vexList [1, 2, 3] vexList [2, 3, 4] vexList [] vexList [] [/quote] if I change the above code in to one line [code] vexList = [1, 2, 3] print('vexList', list(vexList)) vexList=list(map(lambda e: e+1, vexList)) print('vexList', list(vexList)) vexList=map(lambda e: e*2,vexList) print('vexList', list(vexList)) [/code] then py27 and py34 get same verList [quote] ('vexList', [1, 2, 3]) ('vexList', [2, 3, 4]) ('vexList', [4, 6, 8]) [/quote] I found 'filter' function behaves likely I found type(map(lambda e: e, vexList)) is in py2 type(map(lambda e: e, vexList)) is in py3 so, what produces this difference between py2 and py3 in nature? is there more examples? where can I find the text abiut his difference? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Optimizing Memory Allocation in a Simple, but Long Function
A couple thoughts. I think my original approach would be faster than binary search for finding the minimum value of N needed to get a decimal level of absolute accuracy from Euler's number. Here is my reasoning: EulerlersNumber(13).LimitMethod() - math.e < .1 and EulersNumber(135).LimitMethod - math.e < .01. N increased by a little more than a factor of 10. My method would use 130, 131, 132, 133, 134, and 135 as guesses after 13. Using the double + binary search the guesses would be 26, 52, 104, 208, 156, 130, 143, 137, 134, 136, and then 136 after using the information that n=13 gives an tolerance of < .1. If the trend holds, then to get the next decimal of accuracy, you would need to do less than 10 searches each time. When I was using my linear approximation method I found that I never had to do more than 10 additional guesses of N to get the next digit of accuracy. When I was using EulersMethod() I found this trend held for as long as I would allow my computer to do the calculations (though accuracy to 10**-10). What I find very interesting is that although the limit method is mathematically equivalent to the linear approximation method, they give different results, given sufficiently high values of N (at least in Python). The limit method does not follow my predictions as accurately, which leads to the question of whether or not the phenomenon I observed was an artifact of rounding error or not. Also, my explicit goal is to be able to handle large numbers of N, and to reduce rounding error to a minimum. Using the fractions module to perform the limit method with rational numbers rather than binary represented decimals and got an error that the integer was too long to convert to float and even when using smaller values of n (10**14) I seem to get values that are not the same as when using decimals. It seems to me (I'm just thinking about this at a low level and haven't written out any math to justify it yet) that because the linear approximation method only multiplies and adds the rounding error is smaller than in the limit method where numbers are being taken to exponents that have rounding errors. Although I see the value of relative error, I am just as interested in absolute error (though admittedly they are directly related values). Are there modules or libraries I can/should use to minimize rounding error and use very large values of N and get an accurate answer? How does the math module calculate the vale of e? Thanks, Derek On Mon, Apr 25, 2016 at 6:49 AM Oscar Benjamin wrote: > On 25 April 2016 at 08:39, Gregory Ewing > wrote: > > Derek Klinge wrote: > >> > >> Also, it seems to me if the goal is to use the smallest value of n to > get > >> a > >> particular level of accuracy, changing your guess of N by doubling seems > >> to > >> have a high chance of overshoot. > > > > > > If you want to find the exact n required, once you overshoot > > you could use a binary search to narrow it down. > > Also you can calculate the truncation error for Euler's method. Since > >f(t) = f(t0) + f'(t0)*(t - t0) + (1/2)f''(t0)*(t - t0)**2 + O((t - > t0)**3) > > Euler's method just uses the first two terms so > > x[n+1] = x[n] + dt*f(x[n]) > > the next term would be > >(1/2)*f'(x[n])*dt**2 > > Since in your case f'(x) = x and dt = 1/N that's > > (1/2)*x[n]*(1/N)**2 > > As a relative error (divide by x[n]) that's > > (1/2)*(1/N)**2 > > Let's add the relative error from N steps to get > > N*(1/2)*(1/N)**2 = 1/(2*N) > > So the relative error integrating from 0 to 1 with N steps is 1/(2*N). > If we want a relative error of epsilon then the number of steps needed > is 1/(2*epsilon). > > That is to say that for a relative error of 1e-4 we need N = > 1/(2*1e-4) = 1e4/2 = 5e3 = 5000. > > >>> import math > >>> N = 5000 > >>> error = math.e - (1 + 1.0/N)**N > >>> relative_error = error / math.e > >>> relative_error > 9.998167027596845e-05 > > Which is approximately 1e-4 as required. > > -- > Oscar > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Scraping email to make invoice
On 2016-04-24, Michael Torrie wrote: > On 04/24/2016 12:58 PM, CM wrote: > >> 1. INPUT: What's the best way to scrape an email like this? The >>email is to a Gmail account, and the content shows up in the >>email as a series of basically 6x7 tables (HTML?), one table per >>PO number/task. I know if the freelancer were to copy and paste >>the whole set of tables into a text file and save it as plain >>text, Python could easily scrape that file, but I'd much prefer >>to save the user those steps. Is there a relatively easy way to >>go from the Gmail email to generating the invoice directly? (I >>know there is, but wasn't sure what is state of the art these >>days). > > I would configure Gmail to allow IMAP access (you'll have to set up a > special password for this most likely), Your normal gmail password is used for IMAP. > and then use an imap library from Python to directly find the > relevant messages and access the email message body. If the body is > HTML-formatted (sounds like it is) I would use either BeautifulSoup > or lxml to parse it and get out the relevant information. Warning: don't use the basic imaplib. IMAP is a miserable protocol, and imap lib is too thin a wrapper. It'll make you bleed from the ears and wish you were dead. Use imapclient or imaplib2. I've used both (with Gmail's IMAP server), and IMO both are pretty good. Either one is miles ahead of plain imaplib. -- Grant Edwards grant.b.edwardsYow! But they went to MARS at around 1953!! gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: what is the difference between one-line-operation and 2-line-operation
oyster writes: - - > I found > type(map(lambda e: e, vexList)) is in py2 > type(map(lambda e: e, vexList)) is in py3 > > so, what produces this difference between py2 and py3 in nature? is > there more examples? where can I find the text abiut his difference? Yes, there are more ways obtain objects that behave like the map and filter objects on Python 3. One similar to map and filter is enumerate. Try iter('foo'), iter((1,2,4)), ..., iter(range(3)). This is used implicitly by certain Python constructions. Note that range objects themselves are different. Open a file for reading: open('example.txt'). You get lines out. A generator expression: ( "{}".format(x) for x in (1,2,3) ), where the outer parentheses are for grouping and not always needed. Define and call a generator function: def foo(o): yield '(' yield o yield ')' x = foo(31) '(-31-)' == '-'.join(foo('31')) You can ask for the next element from any of these, or collect their remaining elements in a list or tuple, or otherwise iterate or map over them. They will be consumed when you do so, otherwise they just wait. They can be huge, even infinite. Best not to collect their contents in a data structure if they are huge. They trade space for time. -- https://mail.python.org/mailman/listinfo/python-list
Re: what is the difference between one-line-operation and 2-line-operation
On 04/25/2016 08:13 AM, oyster wrote: > so, what produces this difference between py2 and py3 in nature? is > there more examples? where can I find the text abiut his difference? One thing I see is that both your py2 and py3 examples are treating print as a function. It's only a function in Py3. By default in Py2 it's a statement. You passed it a tuple, and that's what it prints. If you add: from __future__ import print_function then py2 and py3 will behave the same in regards to print. -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing different sections into a file
On 2016-04-25, Palpandi wrote: > Hi, > > I need to write different sections into a file. At any point of > time, content can be added to any section. > > I don't want keep each section into a temporary file. What is the > better way to store the contents of each section and write them into > a file at the end? What makes you think a temporary file isn't best? > What is the better datatype to achieve this? When in doubt, try the simplest approach first: Use a string (or byte-string if it's not text) for each of the header, body, and footer. Append data to each as desired and then write them out to a file when you're done. If that's not workable, explain why, and we can tell you what to try next (probably a stringio for each section, or a list of strings or byte-strings for each section, or temporary files). -- Grant Edwards grant.b.edwardsYow! Wait ... is this a FUN at THING or the END of LIFE in gmail.comPetticoat Junction?? -- https://mail.python.org/mailman/listinfo/python-list
Question on List processing
Dear Group, I have a list of tuples, as follows, list1=[u"('koteeswaram/BHPERSN engaged/NA himself/NA in/NA various/NA philanthropic/NA activities/NA ','class1')", u"('koteeswaram/BHPERSN is/NA a/NA very/NA nice/NA person/NA ','class1')", u"('koteeswaram/BHPERSN came/NA to/NA mumbai/LOC but/NA could/NA not/NA attend/NA the/ARTDEF board/NA meeting/NA ','class1')", u"('the/ARTDEF people/NA of/NA the/ARTDEF company ABCOMP did/NA not/NA vote/NA for/NA koteeswaram/LOC ','class2')", u"('the/ARTDEF director AHT of/NA the/ARTDEF company,/NA koteeswaram/BHPERSN had/NA been/NA advised/NA to/NA take/NA rest/NA for/NA a/NA while/NA ','class2')", u"('animesh/BHPERSN chauhan/BHPERSN arrived/NA by/NA his/PRNM3PAS private/NA aircraft/NA in/NA mumbai/LOC ','class2')", u"('animesh/BHPERSN chauhan/BHPERSN met/NA the/ARTDEF prime/HPLPERST minister/AHT of/NA india/LOCC over/NA some/NA issues/NA ','class2')", u"('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA to/NA set/NA up/NA a/NA plant/NA in/NA uk/LOCC ','class3')", u"('animesh/BHPERSN chauh an/BHPERSN is/NA trying/NA to/NA launch/NA a/NA new/ABCOMP office/AHT in/NA burdwan/LOC ','class3')", u"('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA to/NA work/NA out/NA the/ARTDEF launch/NA of/NA a/NA new/ABCOMP product/NA in/NA india/LOCC ','class3')"] I want to make it like, [('koteeswaram/BHPERSN engaged/NA himself/NA in/NA various/NA philanthropic/NA activities/NA','class1'), ('koteeswaram/BHPERSN is/NA a/NA very/NA nice/NA person/NA ','class1'), ('koteeswaram/BHPERSN came/NA to/NA mumbai/LOC but/NA could/NA not/NA attend/NA the/ARTDEF board/NA meeting/NA','class1'), ('the/ARTDEF people/NA of/NA the/ARTDEF company ABCOMP did/NA not/NA vote/NA for/NA koteeswaram/LOC ','class2'), ('the/ARTDEF director AHT of/NA the/ARTDEF company,/NA koteeswaram/BHPERSN had/NA been/NA advised/NA to/NA take/NA rest/NA for/NA a/NA while/NA ','class2'), ('animesh/BHPERSN chauhan/BHPERSN arrived/NA by/NA his/PRNM3PAS private/NA aircraft/NA in/NA mumbai/LOC','class2'), ('animesh/BHPERSN chauhan/BHPERSN met/NA the/ARTDEF prime/HPLPERST minister/AHT of/NA india/LOCC over/NA some/NA issues/NA','class2'), ('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA to/NA set/NA up/NA a/NA plant/NA in/NA uk/LOCC','class3'), ('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA to/NA launch/NA a/NA new/ABCOMP office/AHT in/NA burdwan/LOC','class3'), ('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA to/NA work/NA out/NA the/ARTDEF launch/NA of/NA a/NA new/ABCOMP product/NA in/NA india/LOCC','class3')] I tried to make it as follows, list2=[] for i in train_sents: a1=unicodedata.normalize('NFKD', i).encode('ascii','ignore') a2=a1.replace('"',"") list2.append(a2) and, for i in list1: a3=i[1:-1] list2.append(a3) but not helping. If any one may kindly suggest how may I approach it? Thanks in Advance, Regards, Subhabrata Banerjee. -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing different sections into a file
On Mon, Apr 25, 2016 at 3:04 AM, Karim wrote: > > > On 25/04/2016 09:30, Palpandi wrote: > >> Hi, >> >> I need to write different sections into a file. >> At any point of time, content can be added to any section. >> >> I don't want keep each section into a temporary file. >> What is the better way to store the contents of each section and write >> them into a file at the end? >> What is the better datatype to achieve this? >> >> >> Thanks and Regards, >> Palpandi >> > > Use Stringio: > - > > from cStringIO import StringIO > > content = StringIO() > > # Header > content.write('; Header\n') > content.write('; Body'\n) > content.write('; Footer\n') > > open('my_file', 'wb').write(content.getvalue()) > > - > Karim > > > -- > https://mail.python.org/mailman/listinfo/python-list > All of the other answers are great too. I was thinking that you could format the text file to have dividers for each section. For instance it may look something like this: Header Lorem ipsum <---> Body Lorem ipsum... <---> Footer Lorem ipsum.. Then, you could create a list of sections like so: file = open('file.txt', 'r').read() section_list = file.split('<--->') print(section_list[0]) >>> Header Lorem ipsum... Say you wanted to get really fancy, you could even format the file to have named sections like so: Lorem ipsum... Lorem ipsum... Lorem ipsum... Then you can use the xmlparser library to parse the file into an xml object. Alternatively, you could also use JSON formatting: { sections: { header: { title: "header", content: "Lorem ipsum..." }, body: { title: "Body", content: "Lorem ipsum..." }, footer: { title: "Footer", content: "Lorem ipsum..." } } } I hope this helps. -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing different sections into a file
On 25/04/2016 17:00, justin walters wrote: On Mon, Apr 25, 2016 at 3:04 AM, Karim wrote: On 25/04/2016 09:30, Palpandi wrote: Hi, I need to write different sections into a file. At any point of time, content can be added to any section. I don't want keep each section into a temporary file. What is the better way to store the contents of each section and write them into a file at the end? What is the better datatype to achieve this? Thanks and Regards, Palpandi Use Stringio: - from cStringIO import StringIO content = StringIO() # Header content.write('; Header\n') content.write('; Body'\n) content.write('; Footer\n') open('my_file', 'wb').write(content.getvalue()) - Karim -- https://mail.python.org/mailman/listinfo/python-list All of the other answers are great too. I was thinking that you could format the text file to have dividers for each section. For instance it may look something like this: Header Lorem ipsum <---> Body Lorem ipsum... <---> Footer Lorem ipsum.. Then, you could create a list of sections like so: file = open('file.txt', 'r').read() section_list = file.split('<--->') print(section_list[0]) Header Lorem ipsum... Say you wanted to get really fancy, you could even format the file to have named sections like so: Lorem ipsum... Lorem ipsum... Lorem ipsum... Then you can use the xmlparser library to parse the file into an xml object. Alternatively, you could also use JSON formatting: { sections: { header: { title: "header", content: "Lorem ipsum..." }, body: { title: "Body", content: "Lorem ipsum..." }, footer: { title: "Footer", content: "Lorem ipsum..." } } } I hope this helps. Great ideas! -- https://mail.python.org/mailman/listinfo/python-list
encoding issue help
hi: i want to decompress the string "\x1F\x8B\x08\x00\x00\x00\x00\x00\x00\x00UP]k\xC3 \x14\xFD+\xC3\xE7\xCD\xA8\xF9X\xE2\xEBX\xA1\x0CF\x1F\xBA\xEE%\x10\xAC\xB1\xAD\xC4h\x88f%\x8C\xFD\xF7]\x1B\xDA\xAD\xF8\xE29\xE7z\xEE9~#\xE7\x11G\xAF\xBB\x1C=\x22\xDFv_j\x04H+@\xBAW\x1A\xEEe\x91>SF\x18+i\x9Ef\x04\x84\xA1;\x02/]\x8F\xA5Q\xC2\xF6\xC2\x075\xE2\xFE8\x89\xB1m\xA4\x85\x89V\xFB\xC1\x88\x19\xA6\xDE\xB6\x1Fe\xB6y\x08\xCA\x87\xA7N\xCD\x1E\xC4^H\x10\xF6\x19'\x19/\x14g\x92K\xC1[\x06\xCA\xB2\x9Ca\x82K@\x07m\x8F\x17B\x98\xC1\xD7\xC9a2\xA6\xD9W\xA4j\xBAI\x9E\x84\xAB\x93\x7F\x80g\x18N\x9D,\xEB\xEA\x84fyJIAI\xCE'\xAF\xC6:\xB9\x0B\xE0\xF6\xDA\xA8\x95qg0\x8FE\x87Ke\x86iQbZU\x98\x924\xD6\x1C];\xC9\xB0n\xA3Jhd\x8C\x08\xB7\xCF\x1AN\xCE\xAA-|R\x94\xB3\x82\xA6\xE0\x902v\x19\xB4*l\xE7!*\x9F\xEB\xD5\x1A\x88\xB3>\xE8\xBF\x85\xC1u\xCA\x22n\xA1\x11\xA4\x99wj|\x17\x8B\x0F\x86\xF2\x8D\x8C\xE5\x85\x0Cn\x9Co\xDBt\xEF\xF5\xF2X\x1A\xADlx9\x09k\x95\xB9\x9A\xC8+DtI\xB0\xD116\xFA\xF9\x05\xBAs\xAET\xE0\x01\x00\x00" this string is compress gzip if i save this string in the file and read file to decompress that is not work ,otherwise i use the python interpreter mode and paste the string in the command line the code is work i suppose this is the character-set issue but i can't fix it !so i hope someone give me a solution! my cods blow ,and the work environment is linux import zlib file_obj=open("compress_data.log","r+") for gz_data in file_obj.readlines(): print gz_data decompressed_data = zlib.decompress(gz_data, 16+zlib.MAX_WBITS) print decompressed_data -- https://mail.python.org/mailman/listinfo/python-list
how to set nth bit of large binary file.
I use aria2c to download files, aria2c has this feature of allocating the memory to file before downloading the file and then it will download using multiple connections so filling the data into this file concurrently. So i wonder how to do it. I found a way to do that from here http://stackoverflow.com/questions/3407505/writing-binary-data-to-middle-of-a-sparse-file but it only supports if you are constructing the data in file from scratch and aria2c can resume the download too i.e not from scratch. -- Regards Srinivas Devaki Junior (3rd yr) student at Indian School of Mines,(IIT Dhanbad) Computer Science and Engineering Department ph: +91 9491 383 249 telegram_id: @eightnoteight -- https://mail.python.org/mailman/listinfo/python-list
Re: what is the difference between one-line-operation and 2-line-operation
On 04/25/2016 07:13 AM, oyster wrote: for a simple code [code] vexList = [1, 2, 3] print('vexList', list(vexList)) vexList=map(lambda e: e+1, vexList) print('vexList', list(vexList)) vexList = list(vexList) print('vexList', list(vexList)) vexList=map(lambda e: e*2,vexList) print('vexList', list(vexList)) [/code] py27 says [quote] ('vexList', [1, 2, 3]) ('vexList', [2, 3, 4]) ('vexList', [2, 3, 4]) ('vexList', [4, 6, 8]) [/quote] but py34 says [quote] vexList [1, 2, 3] vexList [2, 3, 4] vexList [] vexList [] [/quote] The difference in behaviour between Python2 and Python3 is the map function. In P2 it returned a list, while in P3 it returns an iterator. Your code runs through that iterator twice with the list() function. The first time it gets the elements of the list as expected, but the second time, the iterator is exhausted and returns no objects. A simpler example: "b" is a map object (iterator), then list(b) is run twice. >>> a = [1,2,3] >>> b = map(lambda e: e+1, a) >>> b >>> list(b) [2, 3, 4] >>> list(b) [] I hope that helps. Gary Herron -- Dr. Gary Herron Professor of Computer Science DigiPen Institute of Technology (425) 895-4418 -- https://mail.python.org/mailman/listinfo/python-list
HOT REQ Closable Position :Websphere message broker at Edison, NJ
Hello Associates, Please go through the below job description and let me know your interest. Please revert me: soura...@itscient.com or sourav524.itsci...@gmail.com. Position: Websphere Message Broker Location:Edison , NJ Duration: 6+ MONTHS Requisition Details: Engineer with experience on working with the new generation messaging platforms such as RabbitMQ, ActiveMQ, JMS,AMQP. Cloudscale Runtime Engineering team is responsible for the Engineering and Delivery of Next Generation Light weight Open Standards-based Platforms for Application Development/Deployment, Caching, Messaging, and Integration Services in client Private Cloud. The responsibilities of the team include delivery and adoption of the Next Generation Runtime Platforms for strategic programs across client Business Lines. Key Responsisbilites: · Engineer efficient Service Provisioning and Management of the Platforms in client Private cloud environment · Develop Integrated monitoring and management of Cloud Messaging Platform consistent with the Cloud Platform Frameworks · Enable On-demand scalability and Capacity Management of the services · Engineer Platform Security Guidelines consistent with client Global Security Policies · Provide key SME to enable rapid Application development and deployment leveraging the CloudScale messaging platforms · Work closely with application development teams to handle escalations and provide timely issue resolutions · This CloudScale Engineer position requires Evaluation and Assessments of Next Generation Application Messaging Platforms and develop usage Patterns and Recommendations for early adopter use-cases Must have skills · About 2-3 years of hands-on working experience with messaging middleware platforms such as RabbitMQ, ActiveMQ TIBCO EMS, IBM MQ or JMS based systems. · Experience in Engineering Messaging Platforms for efficient service provisioning, management and monitoring as well as Continuity of Business/Disaster recovery solutions · Working experience on projects involving multiple services/component integration · Experience in engineering platform security and integration with Security/Authentication/Authorization services · Strong Exposure to Messaging Paradigms, Network Protocols and Resilient services · Exposure to Application Programming Languages such as Ruby, Java, C/C++, C#. · Exposure to REST/JSON and WebServices services · Experience using either WebService or REST for managing application/messaging tasks. · Working experience with scripting languages such as Python, Perl, PHP, JavaScript, Shell · Excellent Oral and Written Communication Skills Nice to have Skills · Strong Exposure to Cloud environments such as AWS/CloudFoundry/Google/Azune · Hands on experience with Messaging Platforms in the Cloud such as RabbitMQ/ActiveMQ · Exposure to API based Application Services Integration Frameworks · Exposure to automation framework components such as Chef, Puppet · Experience with service and application monitoring frameworks such as Graphite, Wily, RTView · Log management in a distributed environment using utilities such as Logstash/Splunk · Exposure to Middleware components such as WebServer and AppServer Thanks & Regards Sourav Paul | Technical Recruiter IT-SCIENT LLC, Fremont, CA, USA Email: sourav524.itsci...@gmail.com Phone: 510-972-8633 | Fax: 877-701-5240 |web: www.itscient.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on List processing
On Tue, 26 Apr 2016 12:56 am, subhabangal...@gmail.com wrote: > Dear Group, > > I have a list of tuples, as follows, > > list1=[u"('koteeswaram/BHPERSN engaged/NA himself/NA in/NA various/NA [... 17 more lines of data ...] Hi Subhabrata, and thanks for the question. Please remember that we are offering help for free, in our own time. If you want help from us, you should help us to help you. It is very unlikely that many people will spend the time to study your data in close enough detail to understand your requirements. Please give a *simplified* example. Instead of 17 lines of repetitive data, use a "toy" example that matches the format but without all the complicated details. And format it so that it is easy to read: input = [u"('a/b/ ','A')", u"('z/x/ ','B')", u"('b/d/ ','C')", ] output = > I tried to make it as follows, [...] > but not helping. What do you mean, "not helping"? What happens when you try? Please show a *simple* example, with no more than four or five lines of *short, easy to read* text. Remember, we are giving you advice and consulting for free. We are not paid to do this. If your questions are too difficult, boring, tedious, or unpleasant, we will just ignore them, so please help us to help you by simplifying them as much as possible. Thank you. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Scraping email to make invoice
On 04/25/2016 08:39 AM, Grant Edwards wrote: > Your normal gmail password is used for IMAP. Actually, no, unless you explicitly tell Google to allow "less-secure" authentication. Otherwise you are required to set up a special, application-specific password. https://support.google.com/accounts/answer/185833?hl=en > Warning: don't use the basic imaplib. IMAP is a miserable protocol, > and imap lib is too thin a wrapper. It'll make you bleed from the ears > and wish you were dead. Use imapclient or imaplib2. I've used both > (with Gmail's IMAP server), and IMO both are pretty good. Either one > is miles ahead of plain imaplib. -- https://mail.python.org/mailman/listinfo/python-list
Re: Scraping email to make invoice
On 2016-04-25, Michael Torrie wrote: > On 04/25/2016 08:39 AM, Grant Edwards wrote: >> Your normal gmail password is used for IMAP. > > Actually, no, unless you explicitly tell Google to allow "less-secure" > authentication. Otherwise you are required to set up a special, > application-specific password. > > https://support.google.com/accounts/answer/185833?hl=en You're right. I should have said your normal gmail password _can_be_ used for IMAP. -- Grant Edwards grant.b.edwardsYow! TAILFINS!! ... click at ... gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head wrote: I am going to forget using a directory path. I would like to take the file win.txt and append a space and the * symbol. f = open('win.txt', 'r+') for line in f: f.read(line) f.write(line+" *") This doesn't work. Would someone fix it please? It is for a task I am trying to accomplish just for a home task. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Seymore4Head wrote: > On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head > wrote: > > I am going to forget using a directory path. > I would like to take the file win.txt and append a space and the * > symbol. > > f = open('win.txt', 'r+') > for line in f: > f.read(line) > f.write(line+" *") > > This doesn't work. Would someone fix it please? It is for a task I > am trying to accomplish just for a home task. "for line in f:" already means "make the variable line equal to each line in f sequentially". f.read is both superfluous and also doesn't do that. Leave it out entirely. The next problem you'll have is that iterating over the lines of the file leaves the newline at the end of line, so your * will end up on the wrong line. Do yourself a favor: https://docs.python.org/3/tutorial/inputoutput.html isn't very long. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi wrote: >Seymore4Head wrote: > >> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >> wrote: >> >> I am going to forget using a directory path. >> I would like to take the file win.txt and append a space and the * >> symbol. >> >> f = open('win.txt', 'r+') >> for line in f: >> f.read(line) >> f.write(line+" *") >> >> This doesn't work. Would someone fix it please? It is for a task I >> am trying to accomplish just for a home task. > >"for line in f:" already means "make the variable line equal to each >line in f sequentially". f.read is both superfluous and also doesn't do >that. Leave it out entirely. > >The next problem you'll have is that iterating over the lines of the >file leaves the newline at the end of line, so your * will end up on the >wrong line. > >Do yourself a favor: https://docs.python.org/3/tutorial/inputoutput.html >isn't very long. I was reading that. I have read it before. I don't use python enough to even remember the simple stuff. Then when I try to use if for something simple I forget how. f = open('wout.txt', 'r+') for line in f: line=line.strip() f.write(line+" *") f.close() Still broke. How about just telling me where I missed? Please? -- https://mail.python.org/mailman/listinfo/python-list
RE: Python path and append
Strip() = white spaces. Description The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters). Use to remove return carriage--> line[:-1] -Original Message- From: Python-list [mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf Of Seymore4Head Sent: 25 April 2016 20:01 To: python-list@python.org Subject: Re: Python path and append On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi wrote: >Seymore4Head wrote: > >> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >> wrote: >> >> I am going to forget using a directory path. >> I would like to take the file win.txt and append a space and the * >> symbol. >> >> f = open('win.txt', 'r+') >> for line in f: >> f.read(line) >> f.write(line+" *") >> >> This doesn't work. Would someone fix it please? It is for a task I >> am trying to accomplish just for a home task. > >"for line in f:" already means "make the variable line equal to each >line in f sequentially". f.read is both superfluous and also doesn't >do that. Leave it out entirely. > >The next problem you'll have is that iterating over the lines of the >file leaves the newline at the end of line, so your * will end up on >the wrong line. > >Do yourself a favor: >https://docs.python.org/3/tutorial/inputoutput.html >isn't very long. I was reading that. I have read it before. I don't use python enough to even remember the simple stuff. Then when I try to use if for something simple I forget how. f = open('wout.txt', 'r+') for line in f: line=line.strip() f.write(line+" *") f.close() Still broke. How about just telling me where I missed? Please? -- https://mail.python.org/mailman/listinfo/python-list This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Seymore4Head wrote: > On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi > wrote: > >>Seymore4Head wrote: >> >>> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >>> wrote: >>> >>> I am going to forget using a directory path. >>> I would like to take the file win.txt and append a space and the * >>> symbol. >>> >>> f = open('win.txt', 'r+') >>> for line in f: >>> f.read(line) >>> f.write(line+" *") >>> >>> This doesn't work. Would someone fix it please? It is for a task I >>> am trying to accomplish just for a home task. >> >>"for line in f:" already means "make the variable line equal to each >>line in f sequentially". f.read is both superfluous and also doesn't do >>that. Leave it out entirely. >> >>The next problem you'll have is that iterating over the lines of the >>file leaves the newline at the end of line, so your * will end up on the >>wrong line. >> >>Do yourself a favor: https://docs.python.org/3/tutorial/inputoutput.html >>isn't very long. > > I was reading that. I have read it before. I don't use python enough > to even remember the simple stuff. Then when I try to use if for > something simple I forget how. > > f = open('wout.txt', 'r+') > for line in f: > line=line.strip() > f.write(line+" *") > f.close() > > Still broke. How about just telling me where I missed? Please? Depends on what "broke" means. I'm going to go out on a limb and guess that the problem now is that you get no newlines at all, because they've been stripped off of the line you read and .write doesn't put it back on, in which case you should be adding " *\n" instead. If that's not it, then reply hazy, please concentrate and ask again. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On 2016-04-25 20:08, Joaquin Alzola wrote: Strip() = white spaces. Description The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters). Use to remove return carriage--> line[:-1] 1. In the file it might be a linefeed, or a carriage return, or a carriage return followed by a linefeed, depending on the operating system. Python translates it to a linefeed "\n" (or 'newline') on reading. 2. It's possible that the last line doesn't end have a line ending, so line[:-1] could be removing some other character. It's safer to use line.rstrip("\n"). -Original Message- From: Python-list [mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf Of Seymore4Head Sent: 25 April 2016 20:01 To: python-list@python.org Subject: Re: Python path and append On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi wrote: Seymore4Head wrote: On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head wrote: I am going to forget using a directory path. I would like to take the file win.txt and append a space and the * symbol. f = open('win.txt', 'r+') for line in f: f.read(line) f.write(line+" *") This doesn't work. Would someone fix it please? It is for a task I am trying to accomplish just for a home task. "for line in f:" already means "make the variable line equal to each line in f sequentially". f.read is both superfluous and also doesn't do that. Leave it out entirely. The next problem you'll have is that iterating over the lines of the file leaves the newline at the end of line, so your * will end up on the wrong line. Do yourself a favor: https://docs.python.org/3/tutorial/inputoutput.html isn't very long. I was reading that. I have read it before. I don't use python enough to even remember the simple stuff. Then when I try to use if for something simple I forget how. f = open('wout.txt', 'r+') for line in f: line=line.strip() f.write(line+" *") f.close() Still broke. How about just telling me where I missed? Please? -- https://mail.python.org/mailman/listinfo/python-list This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Thanks for the tip. Still broke. :( f = open('wout.txt', 'r+') for line in f: if line=="": exit line=line[:-1] line=line+" *" f.write(line) print line f.close() I did notice that it wrote the 3 lines of test file but it didn't append the * after the third entry and it starts printing garbage after that. On Mon, 25 Apr 2016 19:08:56 +, Joaquin Alzola wrote: >Strip() = white spaces. >Description >The method strip() returns a copy of the string in which all chars have been >stripped from the beginning and the end of the string (default whitespace >characters). > >Use to remove return carriage--> line[:-1] > >-Original Message- >From: Python-list >[mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf Of >Seymore4Head >Sent: 25 April 2016 20:01 >To: python-list@python.org >Subject: Re: Python path and append > >On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi > wrote: > >>Seymore4Head wrote: >> >>> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >>> wrote: >>> >>> I am going to forget using a directory path. >>> I would like to take the file win.txt and append a space and the * >>> symbol. >>> >>> f = open('win.txt', 'r+') >>> for line in f: >>> f.read(line) >>> f.write(line+" *") >>> >>> This doesn't work. Would someone fix it please? It is for a task I >>> am trying to accomplish just for a home task. >> >>"for line in f:" already means "make the variable line equal to each >>line in f sequentially". f.read is both superfluous and also doesn't >>do that. Leave it out entirely. >> >>The next problem you'll have is that iterating over the lines of the >>file leaves the newline at the end of line, so your * will end up on >>the wrong line. >> >>Do yourself a favor: >>https://docs.python.org/3/tutorial/inputoutput.html >>isn't very long. > >I was reading that. I have read it before. I don't use python enough to even >remember the simple stuff. Then when I try to use if for something simple I >forget how. > >f = open('wout.txt', 'r+') >for line in f: >line=line.strip() >f.write(line+" *") >f.close() > >Still broke. How about just telling me where I missed? Please? -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Mon, Apr 25, 2016, at 16:15, Seymore4Head wrote: > Thanks for the tip. > > Still broke. :( > > f = open('wout.txt', 'r+') > for line in f: > if line=="": > exit > line=line[:-1] > line=line+" *" > f.write(line) > print line > f.close() Your problem is that after you read the first line, your file "cursor" is positioned after the end of that line. So when you write the modified version of the line, it ends up after that. And then when you write it, the cursor is wherever the end of that is. So if you start with this: AAA BBB CCC You'll end up with this: AAA AAA* [this overwrites "BBB_C" with "AAA*_" if _ is the line break] CC CC* There's no good way around this. You can either read the whole file into memory at once into a list, then rewind (look at the seek function) and write the lines out of the list, or you can write to a *different* file than the one you're reading. -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on List processing
On Mon, 25 Apr 2016 15:56 , wrote: > Dear Group, > > I have a list of tuples, as follows, > > list1=[u"('koteeswaram/BHPERSN engaged/NA himself/NA in/NA various/NA > philanthropic/NA activities/NA ','class1')", u"('koteeswaram/BHPERSN is/NA > a/NA very/NA nice/NA person/NA ','class1')", u"('koteeswaram/BHPERSN > came/NA to/NA mumbai/LOC but/NA could/NA not/NA attend/NA the/ARTDEF > board/NA meeting/NA ','class1')", u"('the/ARTDEF people/NA of/NA > the/ARTDEF company ABCOMP did/NA not/NA vote/NA for/NA koteeswaram/LOC > ','class2')", u"('the/ARTDEF director AHT of/NA the/ARTDEF company,/NA > koteeswaram/BHPERSN had/NA been/NA advised/NA to/NA take/NA rest/NA for/NA > a/NA while/NA ','class2')", u"('animesh/BHPERSN chauhan/BHPERSN arrived/NA > by/NA his/PRNM3PAS private/NA aircraft/NA in/NA mumbai/LOC ','class2')", > u"('animesh/BHPERSN chauhan/BHPERSN met/NA the/ARTDEF prime/HPLPERST > minister/AHT of/NA india/LOCC over/NA some/NA issues/NA ','class2')", > u"('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA to/NA set/NA up/NA a/NA > plant/NA in/NA uk/LOCC ','class3')", u"('animesh/BHPERSN chauh > an/BHPERSN is/NA trying/NA to/NA launch/NA a/NA new/ABCOMP office/AHT > in/NA burdwan/LOC ','class3')", u"('animesh/BHPERSN chauhan/BHPERSN is/NA > trying/NA to/NA work/NA out/NA the/ARTDEF launch/NA of/NA a/NA new/ABCOMP > product/NA in/NA india/LOCC ','class3')"] > What you have is a list of strings, not tuples. > > I want to make it like, > > [('koteeswaram/BHPERSN engaged/NA himself/NA in/NA various/NA > philanthropic/NA activities/NA','class1'), > ('koteeswaram/BHPERSN is/NA a/NA very/NA nice/NA person/NA ','class1'), > ('koteeswaram/BHPERSN came/NA to/NA mumbai/LOC but/NA could/NA not/NA > attend/NA the/ARTDEF board/NA meeting/NA','class1'), ('the/ARTDEF people/NA > of/NA the/ARTDEF company ABCOMP did/NA not/NA vote/NA for/NA > koteeswaram/LOC ','class2'), ('the/ARTDEF director AHT of/NA > the/ARTDEF company,/NA koteeswaram/BHPERSN had/NA been/NA advised/NA to/NA > take/NA rest/NA for/NA a/NA while/NA ','class2'), ('animesh/BHPERSN > chauhan/BHPERSN arrived/NA by/NA his/PRNM3PAS private/NA aircraft/NA in/NA > mumbai/LOC','class2'), ('animesh/BHPERSN chauhan/BHPERSN met/NA the/ARTDEF > prime/HPLPERST minister/AHT of/NA india/LOCC over/NA some/NA > issues/NA','class2'), ('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA > to/NA set/NA up/NA a/NA plant/NA in/NA uk/LOCC','class3'), > ('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA to/NA launch/NA a/NA > new/ABCOMP office/AHT in/NA burdwan/LOC','class3'), > ('animesh/BHPERSN chauhan/BHPERSN is/NA trying/NA to/NA work/NA out/NA > the/ARTDEF launch/NA of/NA a/NA new/ABCOMP product/NA in/NA > india/LOCC','class3')] > > I tried to make it as follows, > list2=[] > for i in train_sents: > a1=unicodedata.normalize('NFKD', i).encode('ascii','ignore') > a2=a1.replace('"',"") > list2.append(a2) > and, > > for i in list1: > a3=i[1:-1] > list2.append(a3) > In both of these you seem to be trying to remove the double quote marks from the strings, but they aren't part of the strings in the first place, just delimiters. > > > but not helping. > If any one may kindly suggest how may I approach it? > Check out the documentation for ast.literal_eval > -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
I am using a test file that is only 3 lines: Punjabi .Mp3 Big Lake (DVD) SWV.avi Blue Balloon.AHC.RH.mkv The program correctly appends an * to the end of the line, but then it goes into a loop printing random looking stuff. f = open('wout.txt', 'r+') for line in f: if line=="": exit line=line[:-1] line=line+" *" f.write(line) print line f.close() On Mon, 25 Apr 2016 20:44:50 +0100, MRAB wrote: >On 2016-04-25 20:08, Joaquin Alzola wrote: >> Strip() = white spaces. >> Description >> The method strip() returns a copy of the string in which all chars have been >> stripped from the beginning and the end of the string (default whitespace >> characters). >> >> Use to remove return carriage--> line[:-1] >> >1. In the file it might be a linefeed, or a carriage return, or a >carriage return followed by a linefeed, depending on the operating >system. Python translates it to a linefeed "\n" (or 'newline') on >reading. > >2. It's possible that the last line doesn't end have a line ending, so >line[:-1] could be removing some other character. It's safer to use >line.rstrip("\n"). > >> -Original Message- >> From: Python-list >> [mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf >> Of Seymore4Head >> Sent: 25 April 2016 20:01 >> To: python-list@python.org >> Subject: Re: Python path and append >> >> On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi >> wrote: >> >>>Seymore4Head wrote: >>> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head wrote: I am going to forget using a directory path. I would like to take the file win.txt and append a space and the * symbol. f = open('win.txt', 'r+') for line in f: f.read(line) f.write(line+" *") This doesn't work. Would someone fix it please? It is for a task I am trying to accomplish just for a home task. >>> >>>"for line in f:" already means "make the variable line equal to each >>>line in f sequentially". f.read is both superfluous and also doesn't >>>do that. Leave it out entirely. >>> >>>The next problem you'll have is that iterating over the lines of the >>>file leaves the newline at the end of line, so your * will end up on >>>the wrong line. >>> >>>Do yourself a favor: >>>https://docs.python.org/3/tutorial/inputoutput.html >>>isn't very long. >> >> I was reading that. I have read it before. I don't use python enough to >> even remember the simple stuff. Then when I try to use if for something >> simple I forget how. >> >> f = open('wout.txt', 'r+') >> for line in f: >> line=line.strip() >> f.write(line+" *") >> f.close() >> >> Still broke. How about just telling me where I missed? Please? >> -- >> https://mail.python.org/mailman/listinfo/python-list >> This email is confidential and may be subject to privilege. If you are not >> the intended recipient, please do not copy or disclose its content but >> contact the sender immediately upon receipt. >> -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
In <27nshbp40p1llr231dqm31p754tvurk...@4ax.com> Seymore4Head writes: > On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head > wrote: > I am going to forget using a directory path. > I would like to take the file win.txt and append a space and the * > symbol. > f = open('win.txt', 'r+') > for line in f: > f.read(line) > f.write(line+" *") > This doesn't work. Would someone fix it please? It is for a task I > am trying to accomplish just for a home task. It's much easier to create a new file and then rename it afterwards, instead of rewriting the original file. import os f_in = open('win.txt', 'r') f_out = open('win_new.txt', 'w') for line in f_in.read().splitlines(): f_out.write(line + " *\n") f_in.close() f_out.close() os.rename('win.txt', 'win_old.txt') os.rename('win_new.txt', 'win.txt') -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Random832 wrote: > On Mon, Apr 25, 2016, at 16:15, Seymore4Head wrote: >> Thanks for the tip. >> >> Still broke. :( >> >> f = open('wout.txt', 'r+') >> for line in f: >> if line=="": >> exit >> line=line[:-1] >> line=line+" *" >> f.write(line) >> print line >> f.close() > > Your problem is that after you read the first line, your file "cursor" > is positioned after the end of that line. So when you write the modified > version of the line, it ends up after that. And then when you write it, > the cursor is wherever the end of that is. > > So if you start with this: > AAA > BBB > CCC > > You'll end up with this: > AAA > AAA* [this overwrites "BBB_C" with "AAA*_" if _ is the line break] > CC > CC* > > There's no good way around this. You can either read the whole file into > memory at once into a list, then rewind (look at the seek function) and > write the lines out of the list, or you can write to a *different* file > than the one you're reading. You can leave the details to python though: $ cat sample.txt alpha beta gamma $ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import fileinput >>> for line in fileinput.input("sample.txt", inplace=True): ... print line.rstrip("\n"), "*" ... >>> $ cat sample.txt alpha * beta * gamma * Too much magic for my taste, but the OP might like it. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Mon, 25 Apr 2016 21:26:34 + (UTC), John Gordon wrote: >In <27nshbp40p1llr231dqm31p754tvurk...@4ax.com> Seymore4Head > writes: > >> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >> wrote: > >> I am going to forget using a directory path. >> I would like to take the file win.txt and append a space and the * >> symbol. > >> f = open('win.txt', 'r+') >> for line in f: >> f.read(line) >> f.write(line+" *") > >> This doesn't work. Would someone fix it please? It is for a task I >> am trying to accomplish just for a home task. > >It's much easier to create a new file and then rename it afterwards, >instead of rewriting the original file. > >import os > >f_in = open('win.txt', 'r') >f_out = open('win_new.txt', 'w') > >for line in f_in.read().splitlines(): >f_out.write(line + " *\n") > >f_in.close() >f_out.close() > >os.rename('win.txt', 'win_old.txt') >os.rename('win_new.txt', 'win.txt') That is 100% spoon fed code and that is what I wanted. Thanks I learned enough python to complete an online course from Dr Chuck but I don't write enough code to remember how except for a simple task that is usually simpler to do manually than to remember how to code. http://www.dr-chuck.com/ Thanks to everyone else too. BTW I was trying to use a line like yours that used an output file that didn't exist and was getting an error. I assume that import os fixes that. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, Apr 26, 2016 at 7:26 AM, John Gordon wrote: > It's much easier to create a new file and then rename it afterwards, > instead of rewriting the original file. And more importantly, it's safer. If anything happens to your process while it's doing its work, you'll have a junk file sitting around, but you won't have lost everything. If you overwrite the existing file, you (often) depend on completely writing out the content you have in memory. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 26 Apr 2016 05:00 am, Seymore4Head wrote: > I was reading that. I have read it before. I don't use python enough > to even remember the simple stuff. Then when I try to use if for > something simple I forget how. It is perfectly fine to forget things that you read weeks or months before. The point is, having forgotten it, you should go back and refresh your memory when you need to. > f = open('wout.txt', 'r+') > for line in f: > line=line.strip() > f.write(line+" *") > f.close() > > Still broke. How about just telling me where I missed? Please? The contents of files don't just magically get moved out of the way when you write to them. There is no "insert mode" for files -- they are always set to "overwrite" mode. (Possible a few very old operating systems on supercomputers from the 1970s or 80s may have supported inserting... I seem to recall that VMS may have allowed that... but don't quote me.) So you can append to the *end* of a file without disrupting the content, but you cannot insert to the middle or beginning of a file without overwriting. The basic way to insert data into a file is: * read the entire file into memory; * insert the new data into the memory; * write the entire file out again. with open("myfile.txt") as f: lines = f.readlines() for i, line in enumerate(lines): lines[i] = line.strip() + " *\n" # space, asterisk, newline with open("myfile.txt", "w") as f: f.writelines(lines) That's good enough for DIY or hobby use. But for professional use, it starts getting *very* complex quickly. What if the power goes off or your computer crashes half-way through writing the file? You've lost all your data. See here for the *start* of a more professional approach: http://code.activestate.com/recipes/579097-safely-and-atomically-write-to-a-file/ -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 26 Apr 2016 08:04 am, Seymore4Head wrote: > BTW I was trying to use a line like yours that used an output file > that didn't exist and was getting an error. I assume that import os > fixes that. Why would you assume that? "Doctor, I have a problem with my arm, but I won't tell you what. I assume that if I take cough drops that will fix it." -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 26 Apr 2016 11:51:23 +1000, Steven D'Aprano wrote: > ... (Possible a few very old operating systems on supercomputers from > the 1970s or 80s may have supported inserting... I seem to recall that > VMS may have allowed that... but don't quote me.) Some [non-supercomputer] OSes/filesystems/languages support(ed) the notion of fixed-length records, where a file acted as if it were an array of records. You could, e.g., replace Record Number 3 without disturbing Record Number 2 or Record Number 4. In the systems I remember like that, the records were simply ad hoc collections of whatever you wrote, not unlike the arguments to print, and it was up to your application to read and write the same structure. The limitation, of course, was that you had to declare the length up front, and that you ended up with wasted space somewhere if a lot of your records were "short." -- https://mail.python.org/mailman/listinfo/python-list
Python email issues
Hi, I am trying send email through smtplib import smtplib import os from libs import send_mail_status send_mail_status('test', False, 'ABCD', os.getcwd()) libs.py has a function as below: def send_status_mail(name, success, id, dir): #SERVER = "localhost" FROM = "localhost" TO = ["a...@nus.edu.sg"] SUBJECT = "test" body = "test" message = """\ From: %s To: %s Subject: %s %s """ % (FROM, ", ".join(TO), SUBJECT, body) But I receive an email as below: All the contents are in the email body: From: localhost To: a...@nus.edu.sg Subject: test test Why all the ocntents are written in the body where as email body should have just a string "test" Kindly clarify. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
def __init__(self):
Hi All, Pls let me why " def __init__(self): " declaration required, what's the use of this one.Pls explain me in details. Thanks in advance. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Steven D'Aprano wrote: (Possible a few very old operating systems on supercomputers from the 1970s or 80s may have supported inserting... I seem to recall that VMS may have allowed that... but don't quote me.) I wouldn't be surprised if VMS provided some sort of indexed random-access file structure that supported inserting records. OSes of that era tended to be big on things like that, since they were all about Serious Data Processing. But it probably wouldn't have let you insert data into the middle of a sequential file without explicitly moving everything that came after it. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: def __init__(self):
San writes: > Pls let me why […] declaration required, what's the use of this one. Welcome to Python! Congratulations on beginning to learn this language. > Pls explain me in details. You should participate in our collaborative tutoring forum, ‘tutor’ https://mail.python.org/mailman/listinfo/tutor> which is specially focussed on helping Python beginners. -- \ “Giving every man a vote has no more made men wise and free | `\ than Christianity has made them good.” —Henry L. Mencken | _o__) | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list