writing dictionaries to a file
Hi All, I have two dictionaries: somex:{'unit':00, type:'processing'} somey:{'comment':'fair', 'code':'aaa'} somey:{'comment':'good', 'code':bbb'} somey:{'comment':'excellent', 'code':'ccc'} now i would like to write this to a file in the following format(unit, code),,,the output should be as follows written to a file,,, 00, aaa 00, bbb 00, ccc can someone help me? Regards Kavitha - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
writing dictionary to file
Hi, I have 'n' number of dictionaries with the same name but different values ( DorC means debit or credit) some={'DorC':'D', 'amount':200,'name':'xxx'} some={'DorC':'C', 'amount':200,'name':'xxx'} some={'DorC':'D', 'amount':300,'name':'yyy'} some={'DorC':'C', 'amount':500,'name':'yyy'} some={'DorC':'D', 'amount':700,'name':zzz} some={'DorC':'C', 'amount':900,'name':zzz} and so on,,, if the credit and debit is not equal for a person, then i would like to open a file and write it in the following format: name:yyy debit:300 credit:500 name:zzz debit:700 credit:900 can anyone help me??? kavitha - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: writing dictionary to file
Hi Simon, iam till here: dorc=some['DorC'] amount=some['amount'] f=open(logfile.txt, w) if dorc =='C': a = -(amount) if dorc == 'D': b = amount sum=a + b if sum == 0: f.writelines(name:) f.writelines(%s %some['name']) f.writelines(credit:) f.writelines(%s % amount) but i see an empty file opened,,, kavitha Simon Brunning [EMAIL PROTECTED] wrote: On 3/8/07, kavitha thankaian wrote: can anyone help me??? I'm sure we can. How far have you got so far? -- Cheers, Simon B [EMAIL PROTECTED] http://www.brunningonline.net/simon/blog/ GTalk: simon.brunning MSN: small_values - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question(file-delete trailing comma)
thanks,,, i tried another way and it works,,, f.writelines(','.join([('\%s\' % some[field]) for field in field_order])) thanks a lot,,, Gabriel Genellina [EMAIL PROTECTED] wrote: En Wed, 28 Feb 2007 08:34:29 -0300, kavitha thankaian escribió: thanks,, now i have one more problem,,, the strings should be seperated in an order,,, some={1:'a', 2:7, 3:'c', 4:'d'} i need the output to be a,c,d,7 before my code was: field_order = [1,3,4,2] for field in field_order: f.writelines('\%s\,' % someprt[field] ) do you have an idea now how should it look like??? Proceed in small steps. First get the data you need to write, then, format them and build a single line, then write the new line onto the file. some = {1:'a', 2:7, 3:'c', 4:'d'} # i need the output to be a,c,d,7 field_order = [1,3,4,2] row = [] for field in field_order: row.append(some[field]) # row contains ['a', 'c', 'd', 7] # convert to string row = ['%s' % item for item in row] ### alternative: convert to string, with around each value ##row = ['%s' % item for item in row] # make a single line, using , as separator line = ','.join(row) # write to file f.write('%s\n' % line) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question(file-delete trailing comma)
ok,,, my script writes a dictionary to a file.but i need only the values from the dictionary which should be sepearted by a comma,,,so i did as following: some=getAllData()--dictionary f=open(test.txt, w) for value in some.values(): f.writelines('\%s\,' % value )strings seperated by comma when i execute the above code,my test.txt file has the following: a,b,c,d, now i need to delete the comma at the end,,,this is my problem,,, kavitha Mikael Olofsson [EMAIL PROTECTED] wrote: kavitha thankaian wrote: i get an error when i try to delete in file and rename it as out file,,the error says permission denied. Perhaps you should give us both the exact code you are running and the complete traceback of the error. That could make things easier. There can be numerous reasons for permission denied. actually i need something like following: in_file = open('in.txt','w') for line in in_file: line.strip().strip(',') but when i run the above code,i get an errorbad file descriptor Of course you do! You are opening the file for writing, but your code attempts to read the file. Probably, you think that the code would change the lines in the file itself, which it does not, even if it would be possible to read from a file opened for writing. What's wrong with the code that Mohammad posted? Note that you might need to close out_file in his code. /MiO -- http://mail.python.org/mailman/listinfo/python-list - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question(file-delete trailing comma)
Thanks Sanket,,, But still doesnt solve my problem,,, now my file contains: aa,ba,b,ca,b,c, and so on,,, the comma at the end is deleted,,,but the file contains some junk values,,, kavitha sanket kathalkar [EMAIL PROTECTED] wrote: Solution to this problem is: --- some=getAllData()--dictionary tmpstr= f=open(test.txt, w) for value in some.values(): tmpstr+= '\%s\,' % value f.writelines(tmpstr.strip(,))strings seperated by comma --- Thanks sanket kavitha thankaian wrote: ok,,, my script writes a dictionary to a file.but i need only the values from the dictionary which should be sepearted by a comma,,,so i did as following: some=getAllData()--dictionary f=open(test.txt, w) for value in some.values(): f.writelines('\%s\,' % value )strings seperated by comma when i execute the above code,my test.txt file has the following: a,b,c,d, now i need to delete the comma at the end,,,this is my problem,,, kavitha */Mikael Olofsson /* wrote: kavitha thankaian wrote: i get an error when i try to delete in file and rename it as out file,,the error says permission denied. Perhaps you should give us both the exact code you are running and the complete traceback of the error. That could make things easier. There can be numerous reasons for permission denied. actually i need something like following: in_file = open('in.txt','w') for line in in_file: line.strip().strip(',') but when i run the above code,i get an errorbad file descriptor Of course you do! You are opening the file for writing, but your code attempts to read the file. Probably, you think that the code would change the lines in the file itself, which it does not, even if it would be possible to read from a file opened for writing. What's wrong with the code that Mohammad posted? Note that you might need to close out_file in his code. /MiO -- http://mail.python.org/mailman/listinfo/python-list Heres a new way to find what you're looking for - Yahoo! Answers - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question(file-delete trailing comma)
thanks,, now i have one more problem,,, the strings should be seperated in an order,,, some={1:'a', 2:7, 3:'c', 4:'d'} i need the output to be a,c,d,7 before my code was: field_order = [1,3,4,2] for field in field_order: f.writelines('\%s\,' % someprt[field] ) do you have an idea now how should it look like??? Mikael Olofsson [EMAIL PROTECTED] wrote: kavitha thankaian wrote: my script writes a dictionary to a file.but i need only the values from the dictionary which should be sepearted by a comma,,,so i did as following: [snip code that generates the incorrect original file] when i execute the above code,my test.txt file has the following: a,b,c,d, now i need to delete the comma at the end,,,this is my problem,,, This is the first time you mention that you have control over the generation of the original file. Then I suggest that you fix the problem before generating the file. For instance, consider the following interactive session. some={1:'a',2:7,3:'c',4:'d'} some {1: 'a', 2: 7, 3: 'c', 4: 'd'} some.values() ['a', 7, 'c', 'd'] [str(x) for x in some.values()] ['a', '7', 'c', 'd'] ','.join([str(x) for x in some.values()]) 'a,7,c,d' Then write that to your file instead. /MiO -- http://mail.python.org/mailman/listinfo/python-list - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question(file-delete trailing comma)
i get an error when i try to delete in file and rename it as out file,,the error says permission denied. actually i need something like following: in_file = open('in.txt','w') for line in in_file: line.strip().strip(',') but when i run the above code,i get an errorbad file descriptor thanks,, kavitha Mohammad Tayseer [EMAIL PROTECTED] wrote: kavitha thankaian [EMAIL PROTECTED] wrote: and i need the output also in the same input.txt just add import os os.remove('in.txt') os.rename('out.txt', 'in.txt') - Don't be flakey. Get Yahoo! Mail for Mobile and always stay connected to friends.-- http://mail.python.org/mailman/listinfo/python-list - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: writing a file:newbie question
Hi All, Thanks for your reply Gabriel ,,, I just had to use a backslash character to write newline in the proper loop.So I could solve that problem.But now my question is I would like to delete the comma at the end. say for example,,i have a file test.txt and the file has the list a,b,c,d, i would like to delete the trailing comma at the end,,, if someone knows pls write to me,,, kavitha Gabriel Genellina [EMAIL PROTECTED] wrote: En Mon, 19 Feb 2007 08:02:29 -0300, kavitha thankaian escribió: Hi, i have a file test.txt and it contains a list of strings say,,, a,b,c,d,a1,b1,c1,d1,a2,b2,c2,d2, i would like to write the file as a,b,c,d a1,b1,c1,d1 a2,b2,c2,d2 and would like to delete the comma at the end. Not enough info... Does the input file contain only one line, or many lines? Always exactly 12 items? Including a trailing , ? The following may work for 12 items. Use the csv module to read the file: import csv reader = csv.reader(open(test.txt, r)) writer = csv.writer(open(output.txt, w)) for row in reader: writer.writerow(row[:4]) writer.writerow(row[4:8]) writer.writerow(row[8:]) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
newbie question(file-delete trailing comma)
hi, i have a file which has the contents as follows: a,b,c,d, a1,b1,c1,d1, a2,b2,c2,d2, i would like to delete all the trailing commas,, if someoneknows pls help me,, kavitha - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question(file-delete trailing comma)
the input file what i have is already opened with write mode. say i have a file input.txt which has a,b,c,d, and i need the output also in the same input.txt,,ie., the trailing comma in the input.txt file should be deleted,,,i dont need a file output.txt,,, is there a way?? kavitha Mohammad Tayseer [EMAIL PROTECTED] wrote: ok, it should be -- import sys in_file = open(sys.argv[1]) out_file = open(sys.argv[2], 'w') for line in in_file: print out_file, line.strip().strip(',') -- strip(',') will remove the ',' char from the beginning end of the string, not the middle. empty strip() will remove whitespaces from the beginning end of the string I hope this solves your problem kavitha thankaian [EMAIL PROTECTED] wrote:hi i would like to have the output as follows: a,b,c,d a1,b1,c1,d1 a2,b2,c2,d2 i would like to delete only the comma at the end and not the commas inbetween. thanks,,, - Any questions? Get answers on any topic at Yahoo! Answers. Try it now. - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
writing a file:newbie question
Hi, i have a file test.txt and it contains a list of strings say,,, a,b,c,d,a1,b1,c1,d1,a2,b2,c2,d2, i would like to write the file as a,b,c,d a1,b1,c1,d1 a2,b2,c2,d2 and would like to delete the comma at the end. if soneone knows pls help me,,, kavitha - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
list of strings-newline
Hi i have a list as follows list=a1,b1,c1,d1,a2,b2,c2,d2,a3,b3,c3,d3, I would like to print as a1,b1,c1,d1, a2,b2,c2,d2, a3,b3,c3,d3, and then i would like to delete the comma at the end,,say like,, a1,b1,c1,d1 a2,b2,c2,d2 a3,b3,c3,d3 (its always after the length is 4 i would like to write as the next line) if any one has any suggestions pls write to me,, - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question:connecting to a database
Hi, I could solve the problem in connecting to a database,,, while configuring the daatsourcename by default it gets connected to the master table,,,so there i must specify the database which i like to connect,,,so now i can see the animals table in my database,, Thanks Eugene,,, Kavitha Eugene Antimirov [EMAIL PROTECTED] wrote: kavitha thankaian wrote: Hi, i wrote a simple script (which follows) to insert a table in the database.i could execute this query and get the result in python shell.but when i open my sql enterprise manager i couldnt find the tableanimals.it would be so kind of you if someone could help me,,, import dbi import odbc conn=odbc.odbc(DSN=mydatabase;UID=xxx;PWD=yyy) cursor=conn.cursor() cursor.execute(Create table animals(parent char(50),child char(50))) cursor.execute(insert into animals values('lion','cub')) cursor.execute(insert into animals values('goat','lamb')) cursor.execute(select * from animals) print cursor.fetchall() Rgds Kavitha You've probably missed cursor.commit() ;) -- Sincerely, Eugene Antimirov PortaOne, Inc., SIP Support Engineer [EMAIL PROTECTED] * For further Billing and Technical information: = Please visit our website http://www.portaone.com = Please visit our forum http://forum.portaone.com * Meet us at Internet Telephony Conference Expo * Ft. Lauderdale, FL - January 24-26, 2007 - Booth 1322 * http://www.tmcnet.com/voip/conference/ -- http://mail.python.org/mailman/listinfo/python-list - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question
Hi Eugene, I tried with conn.commit() as well,,but doesnt seem to work. Also i cannot even view the existing tables via python script.For example,,,i have manually added a customer table in my database.but if i execute the query, select * from customer,,,i get the following error: mxODBC.ProgrammingError: ('S0002', 208, [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name customer., 4612) If someone knows the solution please help me,,, Kavitha Eugene Antimirov [EMAIL PROTECTED] wrote: Eugene Antimirov wrote: You've probably missed cursor.commit() ;) Sorry, my bad: conn.commit() is correct one AFAIR. -- Sincerely, Eugene Antimirov PortaOne, Inc., SIP Support Engineer [EMAIL PROTECTED] * For further Billing and Technical information: = Please visit our website http://www.portaone.com = Please visit our forum http://forum.portaone.com * Meet us at Internet Telephony Conference Expo * Ft. Lauderdale, FL - January 24-26, 2007 - Booth 1322 * http://www.tmcnet.com/voip/conference/ -- http://mail.python.org/mailman/listinfo/python-list - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list
newbie question
Hi, i wrote a simple script (which follows) to insert a table in the database.i could execute this query and get the result in python shell.but when i open my sql enterprise manager i couldnt find the tableanimals.it would be so kind of you if someone could help me,,, import dbi import odbc conn=odbc.odbc(DSN=mydatabase;UID=xxx;PWD=yyy) cursor=conn.cursor() cursor.execute(Create table animals(parent char(50),child char(50))) cursor.execute(insert into animals values('lion','cub')) cursor.execute(insert into animals values('goat','lamb')) cursor.execute(select * from animals) print cursor.fetchall() Rgds Kavitha - Heres a new way to find what you're looking for - Yahoo! Answers -- http://mail.python.org/mailman/listinfo/python-list