Function improperly marks CSV rows as corrupt - python

I have a CSV file with data in it that should be either a float or an integer, but if its not, the row is marked as corrupt.
I have this if statement that checks several of the columns in each row to see if they are not empty, and if they are a float or integer. Currently, the program marks every row in the CSV file as corrupt. How do I fix this?
def check_if_number(num) :
for i in num:
if i == "." or i == "":
pass
else:
try:
float(i)
except (ValueError, TypeError):
return False
return True
def check_if_empty(item) :
if item == "" :
return True
else:
return False
if (check_if_empty(row[5]) == False
and check_if_number(row[5]) == False
or check_if_empty(row[6]) == False
and check_if_number(row[6]) == False
or check_if_empty(row[8]) == False
and check_if_number(row[8]) == False
or check_if_empty(row[9]) == False
and check_if_number(row[9]) == False
or check_if_empty(row[10]) == False
and check_if_number(row[10]) == False):
corrupt = True

I think your second function is returning True when you want it to return False?
def check_if_empty(item) :
if item == "" :
return False
else:
return True
EDIT:
I have simplified and attached an updated working code that yields what you are looking for just to help substantiate my thought.
def check_if_number(num):
float(num)
for i in num:
if i == "." or i == "":
pass
else:
try:
float(i)
except (ValueError, TypeError):
return False
return True
def check_if_empty(item):
if item == "" :
return False
else:
return True
if check_if_empty('.4') == True and check_if_number('.4') == True:
corrupt = False
else:
corrupt = True
print(corrupt)
... False

Related

Why does one part of conditional statements in Python returns None while others are ok?

I need to write a code that prints out valid or invalid for an input of letters and numbers for a license plate. The conditions are: first two characters must be letters, characters have to be between 2 and 6, and if numbers are used, 0 should not be the first, nor should a number appear before a letter.
I put the code below on Thonny and cannot understand why len(l) == 4 part of the conditional statement for def valid_order() returns None and does not execute the next line of the code whereas others work fine. My code should return "Valid" for CSAA50, but it returns invalid. Why?
Also, is there an elegant way to write def valid_order()?
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
if s[0:2].isalpha() and 6 >= len(s) >= 2 and s.isalnum() and valid_order(s):
return True
else:
return False
def valid_order(c):
n = []
l = list(c[2:len(c)])
for i in c:
if i.isdigit():
n += i
if n and n[0] == "0":
return False
if len(l) == 2:
if l[0].isdigit() and l[1].isalpha():
return False
if len(l) == 3:
if l[0].isdigit():
if l[1].isalpha() or l[2].isalpha():
return False
else:
if l[1].isdigit() and l[2].isalpha():
return False
if len(l) == 4:
if l[0].isdigit():
if l[1].isalpha() or l[2].isalpha() or l[3].isalpha():
return False
else:
if l[1].isdigit():
if l[2].isalpha() or l[3].isalpha():
return False
else:
if l[2].isdigit() and l[3].isalpha():
return False
else:
return True
main()

Using a recursive bisection search to check if a character is in a string

My code is here:
def isIn(char, aStr):
mid = len(aStr)//2
if len(aStr)==0:
return False
elif len(aStr)==1:
if char == aStr:
return True
elif aStr[mid] == char:
return True
if mid == 0 and len(aStr) != 1:
return False
else:
if char > aStr[mid]:
return isIn(char,aStr[mid:] )
else:
return isIn(char,aStr[0:mid])
my code works for when the character is present in the string, if the test case is such that if the character that I want to search in the string is not actually present in the string, then the code goes into an infinite loop.
For example in the test case isIn('m','iloruuyz') the code goes into an infinite loop.
On the if len(aStr) == 1: condition, you only return True if the condition is met, but not False if the condition is not, that is where the infinite loop is occuring :)
def isIn(char, aStr):
mid = len(aStr)//2
if len(aStr)==0:
return False
elif len(aStr)==1:
if char == aStr:
return True
else: # Else return false to stop the infinite loop
return False
elif aStr[mid] == char:
return True
if mid == 0 and len(aStr) != 1:
return False
else:
if char > aStr[mid]:
return isIn(char,aStr[mid:] )
else:
return isIn(char,aStr[0:mid])

Python: A function to check the last string

the function takes user's input (a string). It should return True if the last character appears more than once in the string, regardless if it's upper or lower case, otherwise it returns False.
What is wrong with the code?
def last_early(word):
word.lower()
if word.count(word[-1]) > 1:
print("True")
else:
print("False")
This is what I expect:
>>> last_early("happy birthday")
True
>>> last_early("best of luck")
False
>>> last_early("Wow")
True
>>> last_early("X")
False
Try:
def last_early(word):
lower_word = word.lower() # word.lower won't change word itself
if lower_word.count(lower_word[-1]) > 1:
return True
else:
return False
you can test by running:
def testLastEarly():
print("testing last_early()...",end="")
assert(last_early("happy birthday") == True)
assert(last_early("best of luck") == False)
assert(last_early("Wow") == True)
assert(last_early("X") == False)
print("pass")
testLastEarly()
if you want to try more exercise, check out here

Blank List, Return False

I'm looking to write a function definition named has_evens that takes in sequence of numbers and returns True if there are any even numbers in the sequence and returns False otherwise.
My code is correct, except when it receives something empty, like "([])". I need to account for that. Here's my code:
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
elif num % 2 != 0:
return False
if ([]):
return False
The final part is a desperate attempt to account for blank lists. More formally, it needs to pass this assertion:
assert has_evens([]) == False
You should only return True when an even is found:
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
return False
Python has an any function to make this simpler:
def has_evens(s):
return any(num % 2 == 0 for num in s)
I answered my own question. I just needed to un-indent my lines
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
else:
return False

Python function, returning False and None

I have this function
def postData():
data = doPOst()
if data.result == "success":
return data
if data.result == "fail":
return false
Then i based on return i have to do something like
if ret is None:
pass # 1
if ret is False:
pass # 2
else:
normal
I want to know that will None and False get mixed
If you check for None and False explicitly, then no, they will not get combined.
if ret is None: # entered only on None
pass
elif ret == False: # entered only on False or 0
pass
else:
pass
If you just checked for truthy/falsey values, they would get combined:
if ret:
pass
else: # entered on None or False
pass

Categories