Python Forum
Help me input the Rawdata file so that it can output DataErrorFile and ValidData.txt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me input the Rawdata file so that it can output DataErrorFile and ValidData.txt
#1
datainput=open('DataInput.txt', 'r')
Validateddata=open('ValidData.txt', 'w')
Incorrectdata=open('DataErrorFile.txt', 'w')

print("This program’s purpose is to sift through the raw data file and find the data that is valid/invalid in that file '\n'")
 
countervalid = 0
counterinvalid = 0

correctstates=['ID', 'MT', 'MO', 'WA']
random = 0


recordcounter = 0

Validateddata.write("Data List \n")


for line in datainput:
    recordcounter = recordcounter + 1
    stringrecord=str(recordcounter)
    line=line.strip()
    datalist=line.split()
    states= datalist[0]
    citys=datalist[1]
    TEMP=datalist[2]
    CODEREGION=datalist[3]
    recording=datalist[4]
    recordings = int(datalist[4])

if states in correctstates:
      countervalid = countervalid + 1
else:
        counterinvalid = counterinvalid + 1
        print("STATE")
        Incorrectdata.write("Error Detected: ")
        Incorrectdata.write(states)
        Incorrectdata.write("Record")
        Incorrectdata.write(stringrecord)
        Incorrectdata.write("\n")
try:
    float(TEMP)
    countervalid = countervalid + 1
except ValueError:
    print("TEMP")
    counterinvalid = counterinvalid + 1
    stringTEMP = str(TEMP)
    Incorrectdata.write("Error Detected: ")
    Incorrectdata.write(stringTEMP)
    Incorrectdata.write("Record")
    Incorrectdata.write(stringrecord)
    Incorrectdata.write("\n")
    try:
        int(TEMP)
        print("TEMP")
        counterinvalid=counterinvalid + 1
        stringTEMP = str(TEMP)
        Incorrectdata.write("Error Detected: ")
        Incorrectdata.write(stringTEMP)
        Incorrectdata.write("Record")
        Incorrectdata.write(stringrecord)
        Incorrectdata.write("\n")
    except ValueError:
        y = random
        try:
            float(TEMP)
            countervalid = countervalid + 1
        except ValueError:
            print("TEMP")
            counterinvalid = counterinvalid + 1
            stringTEMP = str(TEMP)
            Incorrectdata.write("Error Detected: ")
            Incorrectdata.write(stringTEMP)
            Incorrectdata.write("Record")
            Incorrectdata.write(stringrecord)
            Incorrectdata.write("\n")
            if len(CODEREGION) == 4:
                countervalid = countervalid + 1
                stringcode = CODEREGION
            else:
                counterinvalid = counterinvalid + 1
                print( "REGION")
                Incorrectdata.write("Error Detected: ")
                Incorrectdata.write(CODEREGION)
                Incorrectdata.write("Record")
                Incorrectdata.write(stringrecord)
                Incorrectdata.write("\n")

                if (stringcode[1].isdigit()):
                    countervalid=countervalid + 1
                else:
                    print("REGION")
                    counterinvalid = counterinvalid + 1
                    Incorrectdata.write(CODEREGION)
                    Incorrectdata.write("Record")
                    Incorrectdata.write(stringrecord)
                    Incorrectdata.write("\n")

if (stringcode[0].isalpha()):
    countervalid = countervalid + 1
else:
    print("REGION")
    counterinvalid = counterinvalid + 1
    Incorrectdata.write(CODEREGION)
    Incorrectdata.write("Record")
    Incorrectdata.write(stringrecord)
    Incorrectdata.write("\n")

if (stringcode[3].isdigit()):
    countervalid = countervalid + 1
else:
    print("REGION")
    counterinvalid = counterinvalid + 1
    Incorrectdata.write("Error Detected: ")
    Incorrectdata.write(CODEREGION)
    Incorrectdata.write("Record")
    Incorrectdata.write(stringrecord)
    Incorrectdata.write("\n")

if (stringcode[2].isdigit()):
    countervalid = countervalid + 1
else:
    print("REGION")
    counterinvalid = counterinvalid + 1
    Incorrectdata.write(CODEREGION)
    Incorrectdata.write("Record")
    Incorrectdata.write(stringrecord)
    Incorrectdata.write("\n")

if recordings > 0:
    countervalid = countervalid + 1
else:
    counterinvalid = counterinvalid + 1
    print ("RECORDING")
    strrecording = str(recordings)
    Incorrectdata.write("Error Detected: ")
    Incorrectdata.write(strrecording)
    Incorrectdata.write("Record")
    Incorrectdata.write(stringrecord)
    Incorrectdata.write("\n")

if states in correctstates:
    if recordings > 0:
        if(stringcode[0].isalpha()):
            if (stringcode[1].isdigit()):
                if (stringcode[2].isdigit()):
                    if (stringcode[3].isdigit()):
                        if (TEMP[3].isdigit()):
                            Validateddata.write(states)
                            Validateddata.write(" ")
                            Validateddata.write(citys)
                            Validateddata.write(" ")
                            Validateddata.write(TEMP)
                            Validateddata.write(" ")
                            Validateddata.write(CODEREGION)
                            Validateddata.write(" ")
                            strrecording = str(recordings)
                            Validateddata.write(strrecording)
                            Validateddata.write("\n")
print("Records")
openrecords = open('datainput.txt', 'r')
for line in openrecords:
    print(line)
print(" Data Record Validations and Errors  \n")
print("Correct Data Variables=", countervalid)
print("Incorrect Data Variables=", counterinvalid, "\n")
print("Navigate to the Correct File and Incorrect Data Files for more information.")
Validateddata.close()
Incorrectdata.close()
datainput.close()
I have this in a folder with all the .txt files I am trying to run but I get this error message:

Error:
Traceback (most recent call last): File "C:\Users\Master Race\Desktop\Program 1.EvanWinder\Evan Program 1.py", line 31, in <module> if states in correctstates: NameError: name 'states' is not defined
can anyone help?
Reply
#2
looking at the code it looks fine.
is it possible that this code is different from the one you run, e.g. you made changes and didn't save it (no auto save), or having 2 files and you running different one than you think?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(May-01-2020, 05:12 AM)buran Wrote: looking at the code it looks fine.
is it possible that this code is different from the one you run, e.g. you made changes and didn't save it (no auto save), or having 2 files and you running different one than you think?

no this is the exact program I'm running but I'm checking over the files to make sure they are exactly what should open.
Reply
#4
states is assigned on line 24, but that line is within a loop. That suggests that the loop is never run to assign the variable. That suggests datainput is empty.

Just before line 31, print out the value of recordcounter and see if it is zero.
Reply
#5
(May-01-2020, 05:26 AM)bowlofred Wrote: That suggests datainput is empty.
yep, I assumed DataInput.txt is not empty
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(May-01-2020, 05:26 AM)bowlofred Wrote: states is assigned on line 24, but that line is within a loop. That suggests that the loop is never run to assign the variable. That suggests datainput is empty.

Just before line 31, print out the value of recordcounter and see if it is zero.
So in this case take it out of to loop and place it under line 16? or is there another way I should go about this also DataInput.txt is not empty
Reply
#7
(May-01-2020, 05:36 AM)Halozeus Wrote:
(May-01-2020, 05:26 AM)bowlofred Wrote: Just before line 31, print out the value of recordcounter and see if it is zero.
So in this case take it out of to loop and place it under line 16? or is there another way I should go about this also DataInput.txt is not empty

I was just suggesting that as a diagnostic, you put something like
print(f"recordcounter is now {recordcounter}")
immediately prior to the if states... line.

If the value is zero, you need to figure out why it's not reading the file properly. If it is non-zero, then it would be really odd, because states should be assigned on the first time through.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output provide the filename along with the input file processed. arjunaram 1 926 Apr-13-2023, 08:15 PM
Last Post: menator01
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading an Input File DaveG 1 1,242 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  serial input/output barryjo 3 2,474 Dec-27-2021, 11:57 PM
Last Post: barryjo
  How to input & output parameters from command line argument shantanu97 1 2,550 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Code giving same output no matter the input. Yort 2 2,522 Dec-20-2020, 05:59 AM
Last Post: buran
  Handling multi-input/output audio in python bor1904 4 3,566 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  single input infinite output problem Chase91 2 1,931 Sep-23-2020, 10:01 PM
Last Post: Chase91
  How to assign input file name as logger name Mekala 5 2,841 Aug-05-2020, 12:54 AM
Last Post: Larz60+
  Output CSV file with filepath and file contents glittergirl 1 1,748 Aug-03-2020, 01:50 AM
Last Post: glittergirl

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020