CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2011
    Posts
    3

    Please solve this program

    I want python to read my input.txt and generate output.txt in same folder

    input.txt
    0 m
    100 f
    180 f
    280 d
    and output.txt

    0 99 m
    100 179 f
    180 279 f
    280 blank d
    I tried this a lot but didnt get that pattern ... Please please please , somebody solve this for me

    You may notice that "second column" has "next number-1" of column first.

    thanks in advance.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Please solve this program

    What did you try?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2011
    Posts
    3

    Re: Please solve this program

    The code isn't beautiful... but it works

    import sys,os,msvcrt,re
    main_path = os.getcwd().replace('\\','/')
    output_text = ''
    try:
    os.remove(main_path+'/output.txt')
    except:
    pass
    print 'custom program for bittu123\n'
    try:
    file_input = open(main_path+'/input.txt', 'r')
    lines = file_input.readlines()
    line_number = 0
    output_list = []
    parsed_data = []
    for line in lines:
    line_number+=1
    parsed_data.append(re.split('\s', line.lstrip()))
    for i in range(line_number):
    output_list.append(i)
    output_list[i]=parsed_data[i]
    pos = 0
    for letter in parsed_data[i]:
    pos+=1
    if letter.isalpha():
    letter_position = pos-1
    data_len = len(parsed_data)
    if i<(data_len-1):
    element = i+1
    else:
    element = i
    if i!=(data_len-1):
    for a in parsed_data[element]:
    if a.isdigit():
    output_list[i][letter_position:letter_position]=[int(a)-1]
    letter_position+=1
    else:
    output_list[i][letter_position:letter_position]=['blank']
    except:
    print 'Opening the file has failed / No file found.'
    for w in output_list:
    for wr in w:
    output_text+=str(wr)+' '
    output_text+='\n'
    try:
    output_file = open('D:/Python27/project/output.txt','a')
    output_file.write(output_text)
    output_file.close()
    except:
    print 'Writing the file has failed.'
    file_open = raw_input('Open the output file? y/n\n')
    if file_open == 'y':
    try:
    os.startfile(main_path+'/output.txt')
    except:
    print 'File lounching has failed.'
    pass

    print '\nPress any key to exit'
    ch = msvcrt.getch()

  4. #4
    Join Date
    Sep 2011
    Posts
    3

    Re: Please solve this program

    here is the file
    Attached Files Attached Files

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Please solve this program

    brutalenemy666: do not give code as a free handout. In the long run, spoonfeeding is harmful. When you do show code, post it in [code][/code] bbcode tags. This is especially essential for Python where a difference in indentation can alter the meaning of the code.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Sep 2011
    Posts
    3

    Re: Please solve this program

    @brutalenemy666, Thank you very much for your great help.... It really works as i wanted.

    @Laserlight, Sorry for him, since he didn't put inside . But attached file realy helped me .By the way, whats the problem if somebody is helping.

    In fact, I don't have much knowledge of python programing. So, I took help. I am new in python world...

    Once again thanks a lot

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Please solve this program

    Quote Originally Posted by bittu123
    By the way, whats the problem if somebody is helping.

    In fact, I don't have much knowledge of python programing. So, I took help. I am new in python world...
    Read the FAQ on homework. Note that "homework" is applicable even if it was not assigned to you by a teacher in an academic environment.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Sep 2011
    Posts
    3

    Re: Please solve this program

    @Laserlight..... Got your point...But I tried a lot then I decided to get help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured