![]() |
| ||
| python newbie help i am newbie and have a problem. i have a text file(rawfile.txt) like below. NAME:XXXXXXXXXXXX SURNAME:XXXXXXXXXXXX DATE:23.09.2006 A B C D E F G H (column names) 40 250 300 01.01.2006 13:43:21 250 12345678 KENTBANK 31 123 455 02.02.2006 11:22:43 450 CAPITALBANK . . . . PAGE 1 40 150 240 01.11.2006 17:41:21 50 12346678 XBANK 31 123 455 02.02.2006 11:22:43 654474151 YBANK . . . . PAGE 2 . PAGE 3 . . PAGE 4 . . NOTES:XXXXXXX XXXXXXX XXXXXXXXXXXXXXXXXXXXXXX i want to convert format below(tab separeted) and save (resultfile.txt). A B C D E F G H 40 250 300 01.01.2005 13:43:21 250 12345678 KENTBANK 31 123 455 02.02.2005 11:22:43 450 tab CAPITALBANK 40 150 240 01.11.2005 17:41:21 50 12346678 CITYBANK 31 123 455 02.02.2005 11:22:43 tab 654474151 CITYBANK how can i do this? Thanks. |
| ||
| Re: python newbie help Does all the lines you want end in "BANK"? |
| ||
| Re: python newbie help If the lines you want start with number, other than header, here is easy solution: # this would be rawfile.txt |
| ||
| Re: python newbie help thanks guys...it's good for me. if some lines are empty, how can i set them default valus? for example: ID A1 A2 Date Time Sec.SID Bank 40 250 300 01.01.2005 13:43:21 250 12345678 KENTBANK 31 123 455 02.02.2005 11:22:43 450 XXX CAPITALBANK 40 150 240 01.11.2005 17:41:21 50 12346678 YYY 31 123 455 02.02.2005 11:22:43 ZZZ 654474151 CITYBANK XXX,YYY,ZZZ means empty (not indicated). SID(default):111111 Bank(default):NA Sec(default):0 and all fields should be tab separeted, not space. |
| ||
| Re: python newbie help Simply replace this part of present code: # process the list of linesThe thing with xxx, yyy, zzz and default values you have to explain better. For instance, is xxx contained in rawfile.txt and you want it replaced with 111111? You could do that with additional line.replace(what, with) statements. |
| ||
| Re: python newbie help ok...assume that column6-7-8 are variables. ID A1 A2 Date Time Sec.SID Bank 40 250 300 01.01.2005 13:43:21 250 12345678 KENTBANK 31 123 455 02.02.2005 11:22:43 450 column7 CAPITALBANK 40 150 240 01.11.2005 17:41:21 50 12346678 column8 31 123 455 02.02.2005 11:22:43 column6 654474151 CITYBANK column6(default):0 column7(default):11111111 column8(default):NA and all lines should be separeted with tab, not only line names. |
| ||
| Re: python newbie help additionally, if A1 or A2 columns start with "2", insert 555. A1:250 then A1:555240 ID A1 A2 Date Time Sec SID Bank 40 555250 300 01.01.2005 13:43:21 250 12345678 KENTBANK ***************************************************** if A1 or A2 columns start with "4", insert 666. ID A1 A2 Date Time Sec. SID Bank 31 123 666452 02.02.2005 11:22:43 450 column7 CAPITALBANK 31 123 666455 02.07.2005 14:22:43 column6 654474151 CITYBANK ********************************************************* to be continued.... note:i have decided to learn Python. Python,great! (i wonder python & database applications) |
| ||
| Re: python newbie help Got to hurry! The waitress at the internet bistro wants to serve my meal. Here is some more code, hope that satisfies your needs: # this would be rawfile.txtLuckily, Python makes it easy. So far it has been a brain-teaser, when it gets to be work I will stop! |
| ||
| Re: python newbie help 1. i have tried codes. it runs vey well. how can i use files argument? my python file bank.py i call file using arguments bank.py <filename> 2. line.replace(" ","\t") doesnt work. i am trying separete all lines/columns with tab but i couldnt. 3. and bon appetite :) |
| ||
| Re: python newbie help Function line.replace(" ","\t") works very well, but it may depend on how many spaces your tab is set at in your editor, as you look at the result. You could use double tabs like line.replace(" ","\t\t"). To add a commandline argument, change this part of the code ... # read in raw_file.txt as list of lines/stringsto this ... # use commandline argument for filenameOn your question about Python and databases, there are many modules available to make Python interface with the most common databases. You just have to google for them. |
| All times are GMT -4. The time now is 10:56 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC