Click here to Skip to main content
15,886,689 members

Replace line with new data

BinshaS asked:

Open original thread
I have my input as

ROLLDIST_STAGE_NO
 1 0 0 0

If my script file in csv, where 27th row and first 4 columns contain any data other than 0,then the 0 in input should be 1 as output otherwise input data should be same as output. For eg: If 27th row contains data as 0,100,0,200 then my output should be

ROLLDIST_STAGE_NO
 1 1 0 1

Please help to solve this. Code help is expecting.


What I have tried:

fileName=input("Enter Input File Name: ")
   f1=open(fileName,'r')
   OutName=input("Enter Output File Name: ")
   f4=open(OutName,"w") 
   scriptName=input("Enter Script File Name(csv): ")
   r=csv.reader((open(scriptName)))
   rownum=0
   a=[]
   for row in r:                #Reading script data
      a.append(row)
      rownum+=1
   class Calculate:
      rollFlag=0
      rollCount=0
      roll=int(a[25][0])
      rtFlag=0
      def write(self,string):
             f4.write(string)
     obj=Calculate()
     for line in f1.readlines():     
     string=line
     elif re.match(r'ROLLDIST_STAGE_NO',string):
         f4.write(string)
         obj.rollFlag=1
     elif(obj.rollFlag==1 and obj.roll==1):
         if obj.rollCount<=1:
             strplit=string.split()
             if(int(a[27][0])!=0):
                st1=1
              else:

               st1=int(strplit[0])
            if int(a[27][1]!=0):
              st2=1
             else:

                st2=int(strplit[1])
             if int(a[27][2]!=0):
                 st3=1
             else:
                 print(strplit[2])
                 st3=int(strplit[2])
             if int(a[27][3]!=0):
                 st4=1
            else:
                st4=int(strplit[3])

             string=str(st1)+"\t"+str(st2)+"\t"+str(st3)+"\t"+str(st4)+"\n"
             obj.write(string)
             obj.rollFlag=0
             obj.rollCount+=1

    else:
         f4.write(string)
Tags: Python (Python 2.3, Python3.6), String, File

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900