2 Attachment(s)
Please solve this program
I want python to read my input.txt and generate output.txt in same folder
input.txt
Quote:
0 m
100 f
180 f
280 d
and output.txt
Quote:
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.
Re: Please solve this program
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()
1 Attachment(s)
Re: Please solve this program
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.
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
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.
Re: Please solve this program
@Laserlight..... Got your point...But I tried a lot then I decided to get help