Function to read specified single column from a csv file in python without using libraries and it should return column name and values into list - python-3.x

Getting error "TypeError: 'list' object is not callable", I have changed the parenthesis into square bracket but still not working.
def getColumnType(value):
dataType = None;
try:
numericVal = int(value)
dataType =int;
except ValueError:
numericVal = float(value)
dataType = float;
return dataType
def extractColFromFile(fileName,colIdx):
try:
columnName = ""
datalist = []
with open(fileName) as file:
firstLine = file.readline().strip()
colCount = firstLine.count(',') + 1
contents = [x.strip() for x in file.readlines()]
floatCount = 0
if colIdx in range(0,colCount):
columnName = firstLine.split(',')[colIdx]
for line in contents:
columns = line.split(',')
for idx,data in enumerate(columns):
if (idx == colIdx):
if(getColumnType(data) == float):
floatCount += 1
datalist.append(data)
if( floatCount > 0):
classType = float
else:
classType = int
result = list(map(classType, datalist))
return columnName, result
else:
print("invalid column index value");
except OSError as e:
print(e)
x = int(input("Enter a number: "))
result = extractColFromFile("task1.csv",x)
print (result)

Related

Function to read specified column value from CSV file in python without libraries and return the column name and list of column values

creating a function to read the specified given column Range and column value from the csv file in python without using libraries. it should return the column name and list of column values map with own column types. for example, if it is float then the list should return float values. But while running the function, getting an error
TypeError: 'list' object is not callable
even i tried to change the parenthesis to Square bracket. But still not working.
def get_Col_Data_Type(val):
colType = None;
try:
intergerVal = int(val)
colType = int;
except:
floatVal = float(val)
colType = float;
return colType
def get_Col_From_Data(file_Name,column_Range):
col_Name = ""
column_Value = []
# result1 = []
with open(file_Name) as file:
first_Row = file.readline().strip()
column_Count = first_Row.count(',') + 1
row_Value = [x.strip() for x in file.readlines()]
float_Count = 0
if column_Range in range(0,column_Count):
col_Name = first_Row.split(',')[column_Range]
for line in row_Value:
data_columns = line.split(',')
for index,data in enumerate(data_columns):
if (index == column_Range):
if(get_Col_Data_Type(data) == float):
float_Count += 1
column_Value.append(data)
if( float_Count > 0):
column_Type = float
else:
column_Type = int
result = list(map(column_Type, column_Value))
# result1 = list[result]
return col_Name, result
else:
print("Invalid column Range Given. Please give the available column Range");
x = int(input("Enter a Column Range: "))
print("You have entered the column Range is:",x)
result = get_Col_From_Data("task1.csv",x)
print (result)

TypeError: cannot unpack non-iterable NoneType object while using operator packages

import operator
class Point():
def __init__(self,x,y):
self.x=x
self.y=y
def __repr__(self):
return '<{0},{1}>'.format(self.x,self.y)
def distance(a,b):
return abs((a.x-b.x)**2+(a.y-b.y)**2)**.5
def closest(points):
n = len(points)
if n<=1:
print("invalid input")
elif n==2:
return (points[0],points[1])
elif n==3:
(a,b,c)=points
ret = (a,b) if distance(a,b) < distance(b,c) else (a,c)
ret = (ret[0],ret[1]) if distance(ret[0],ret[1])<distance(b,c) else (b,c)
else:
points = sorted(points,key=operator.attrgetter('x'))
leftPoints = points[:n//2]
rightPoints = points[n//2:]
(left_a,left_b) = closest(leftPoints)
(right_a,right_b) = closest(rightPoints)
d = min(distance(left_a,left_b),distance(right_a,right_b))
mid = (points[n//2].x+points[n//2+1].x)/2
midRange = filter(lambda pt:pt.x >=mid-d and pt.x<=mid+d,points)
midRange = sorted(midRange,key=operator.attrgetter('y'))
ret = None
localMin = None
for i in range(len(midRange)):
a = midRange[i]
for j in range(i+1,len(midRange)):
b = midRange[j]
if(not ret)or (abs(a.y-b.y)<=d and distance(a,b)<localMin):
localMin= distance(a,c)
ret = (a,b)
return ret
points =[Point(1,2),Point(0,0),Point(3,6),Point(4,7),Point(5,5),Point(8,4),Point(2,9),Point(4,5),Point(8,1),Point(4,3),Point(3,3)]
print(closest(points))
. The error is mentioned in the screenshot that I have uploaded .:
The error is coming while I am trying to run it in Python 3.7.
The error is occurring when I am trying to run the code with the parenthesis.

Runtime/Resource Warning error in Python

I was trying to run this code and encountered a run time error. I am not able to debug the code. I do believe that the error lies in functions huffman_encode and huffman_decode. The error showing is the resource warning error. Here is the code:
from linked_list import *
from huffman_bits_io import HuffmanBitsWriter as writer, HuffmanBitsReader as reader
import unittest
class Leaf:
'''class that implements Leaf'''
def __init__(self, parent, value, code, frequency):
self.parent = parent
self.frequency = frequency
self.value = value
self.code = code
def __eq__(self, other):
return type(other) == Leaf and self.parent == other.parent and self.frequency ==other.frequency and self.value==other.value and self.code==other.code
def __repr__(self):
return "[ {}, frequency = {} ]".format(self.code, self.frequency)
class Node:
'''class that implements Node'''
def __init__(self, parent, code, lchild, rchild, frequency):
self.parent = parent
self.code = code
self.frequency = frequency
self.lchild = lchild
self.rchild = rchild
def __eq__(self, other):
return type(other) == Node and self.parent==other.parent and self.code == other.code and self.frequency == other.frequency and self.lchild == other.lchild and self.rchild == other.rchild
def __repr__(self):
return "{}, freq = {}\n\left = {}\n\right = {}".format(self.code, self.frequency, self.lchild.___repr__(), self.rchild.__repr__())
def strip(string, seq):
'''this function cuts sequence from beginning of string if possible and returns result '''
if len(seq) > len(string):
return string
for i in range(len(seq)):
if seq[i] != string[i]:
return string
else:
return string[len(seq):]
def find(lst, item):
'''this function finds index of first occurrence of given element in the list and returns it or raise error if there is no such element'''
for i in range(lst.length):
if get(lst, i).value[0] == item:
return i
else:
raise ValueError
def string_traverse(node):
'''this function returns string representation of tree in pre-order traversal'''
lst = empty_list()
traverse(node, lst) #calls traverse
result_string = ''
for i in range(lst.length): #accumulate string from lst list
result_string += chr(get(lst, i).value)
return result_string
def traverse(node, code):
'''this function traverse the try and return list of leaf's value(helper for string_traverse)'''
if type(node) == Leaf:
code = add(code, node.value, code.length) #if node is Leaf than terminate recursion and return character
else:
traverse(node.lchild, code) #recursive call
traverse(node.rchild, code) #recursive call
def count_occurrences(file_name):
'''this function returns list that represent occurrence of every character of given string'''
with open(file_name) as file: #reads file
data = file.read()
lst = list()
for i in range(len(data)): #creates list of integer representation of string
lst.append(ord(data[i]))
data = lst
lst = empty_list()
for char in data: #this loop calculates occurrences of characters in the string
try:
index = find(lst, char)
lst = set(lst, index, (char, get(lst, index).value[1] + 1))
except ValueError:
lst = add(lst, (char, 1), 0)
lst = sort(lst, lambda x: x.value[1], False) #sorts occurrences
return lst
def comes_before(el1, el2):
'''this function returns True if el1 leaf should come before el2 leaf in Huffman tree meaning'''
if el1[1] < el2[1] or (el1[1] == el2[1] and type(el1[0]) is int and type(el2[0]) is int and el1[0] < el2[0]):
return True
else:
return False
def build_tree(occurrences):
'''this function returns Huffman tree based on given list of occurrences'''
if occurrences.length == 1: #if we have only one character returns Leaf with this character and code '0'
return Leaf(None, get(occurrences, 0).value[0], '0', get(occurrences, 0).value[1])
while occurrences.length != 1: #algorith described in the task
el1, occurrences = remove(occurrences, 0)
el2, occurrences = remove(occurrences, 0)
el1, el2 = el1.value, el2.value
if not comes_before(el1, el2): #finds order of elements in the tree
el1, el2 = el2, el1
new = Node(None, '', None, None, el1[1] + el2[1]) #creates new node
if type(el1[0]) is Node:
el1[0].code = '0' #sets up code for node
el1[0].parent = new
new.lchild = el1[0]
else:
new.lchild = Leaf(new, el1[0], '0', el1[1]) #if el1 is character not Node we will create leaf for that character
if type(el2[0]) is Node:
el2[0].code = '1' #sets up code for node
el2[0].parent = new
new.rchild = el2[0]
else:
new.rchild = Leaf(new, el2[0], '1', el2[1]) #if el2 is character not Node we will create leaf for that character
occurrences = insert_sorted(occurrences, (new, new.frequency), comes_before) #inserts new node
return get(occurrences, 0).value[0]
def collect_code(node, code = ''):
'''this function traverse Huffman tree and collect code for each leaf and returns them as nested list(helper for create_code)'''
if type(node) == Leaf:
lst = empty_list()
return add(lst, (node.value, code + node.code), 0) #if node is Leaf terminates recursion and returns code for the leaf
else:
lst = empty_list()
lst = add(lst, collect_code(node.lchild, code + node.code), 0) #recursive call
lst = add(lst, collect_code(node.rchild, code + node.code), 0) #recursive call
return lst
def create_code(tree):
'''this function unpack result of calling collect_code and return Huffman code as a list of tuples'''
code = collect_code(tree) #calls collect code
i = 0
while i < code.length: #this loop unpacks list
if type(get(code, i).value) is not tuple:
item, code = remove(code, i)
for j in range(item.value.length):
code = add(code, get(item.value, j).value, i)
continue
i += 1
return code
def huffman_encode(input_file, output_file):
'''task describe this function'''
occurrences = count_occurrences(input_file)
tree = build_tree(occurrences)
string = empty_list()
t = traverse(tree, string)
code = create_code(tree)
with open(input_file) as file:
string = file.read()
result_string = ''
for i in range(len(string)): #this loop encodes string using code produced by create_code function
for j in range(code.length):
temp = get(code, j).value
if string[i] == chr(temp[0]):
result_string += temp[1]
break
for i in range(occurrences.length):
temp = get(occurrences, i).value
occurrences = set(occurrences, i, (chr(temp[0]), temp[1]))
occurrences = sort(occurrences, lambda x: x.value[0], False)
file = writer(output_file)
file.write_int(code.length)
for i in range(occurrences.length):
temp = get(occurrences, i).value
file.write_byte(ord(temp[0]))
file.write_int(temp[1])
file.write_code(result_string)
file.close()
return string_traverse(tree)
def huffman_decode(input_file, output_file):
'''task describe this function'''
file = reader(input_file)
number_of_codes = file.read_int()
occurrences = empty_list()
for i in range(number_of_codes):
char = file.read_byte()
number = file.read_int()
occurrences = add(occurrences, (char, number), 0)
occurrences = sort(occurrences, lambda x: x.value[1], False)
tree = build_tree(occurrences)
code = sort(create_code(tree), lambda x: x.value[0], False)
occurrences = sort(occurrences, lambda x: x.value[0], False)
quantity_of_bits = 0
for i in range(code.length):
quantity_of_bits += get(occurrences, i).value[1]*len(get(code, i).value[1])
occurrences = sort(occurrences, lambda x: x.value[1], False)
bit_string = ''
for i in range(quantity_of_bits):
bit_string = bit_string + ('1' if file.read_bit() else '0')
result_string = ''
while bit_string: #this loop decodes string using code produced by create_code function
for j in range(code.length):
temp = get(code, j).value
stripped = strip(bit_string, temp[1])
if len(stripped) < len(bit_string):
result_string += chr(temp[0])
bit_string = stripped
break
with open(output_file, 'w') as file:
file.write(result_string)
file.close()
class Test(unittest.TestCase):
def test_strip1(self):
self.assertEqual(strip('123456', '123'), '456')
def test_strip2(self):
self.assertEqual(strip('123', '4567'), '123')
def test_strip3(self):
self.assertEqual(strip('123', '456'), '123')
def test_find(self):
lst = empty_list()
lst = add(lst, (1, 'b'), 0)
lst = add(lst, (2, 'a'), 1)
self.assertEqual(find(lst, 2), 1)
def test_find_raise(self):
lst = empty_list()
lst = add(lst, (1, 'b'), 0)
lst = add(lst, (2, 'a'), 1)
self.assertRaises(ValueError, find, lst, 5)
def test_occurrences(self):
lst = empty_list()
lst = add(lst, (97, 5), 0)
lst = add(lst, (98, 3), 0)
lst = add(lst , (99, 7), 2)
self.assertEqual(str(count_occurrences(r'test2.txt')), str(lst))
def test_create_code_and_tree_build(self):
occurrences = count_occurrences(r'test2.txt')
tree = build_tree(occurrences)
code = create_code(tree)
code = sort(code, lambda x: x.value[0], False)
self.assertEqual(str(code), "[(97, '11'), (98, '10'), (99, '0')]")
def test_huffman_encode_decode(self):
string = huffman_encode(r'test1.txt', r'test_out.txt')
huffman_decode(r'test_out.txt', r'test_decode.txt')
self.assertEqual(string, 'a')
with open(r'test1.txt') as file1:
with open(r'test_decode.txt') as file2:
self.assertEqual(file1.read(), file2.read())
file2.close()
file1.close()
def test_huffman_encode_decode3(self):
string = huffman_encode(r'test2.txt', r'test2_out.txt')
huffman_decode(r'test2_out.txt', r'test2_decode.txt')
self.assertEqual(string, 'cba')
with open(r'test2.txt') as file1:
with open(r'test2_decode.txt') as file2:
self.assertEqual(file1.read(), file2.read())
file2.close()
file1.close()
def test_huffman_encode_decode2(self):
string = huffman_encode(r'test3.txt', r'test3_out.txt')
huffman_decode(r'test3_out.txt', r'test3_decode.txt')
self.assertEqual(string, 'edcba')
with open(r'test3.txt') as file1:
with open(r'test3_decode.txt') as file2:
self.assertEqual(file1.read(), file2.read())
file2.close()
file1.close()
if __name__ == '__main__':
unittest.main()
And following is the error:
...
Warning (from warnings module):
File "C:\Users\Vikas\Documents\fwdregardingprojectdevelopment\huffman.py", line 212
with open(output_file, 'w') as file:
ResourceWarning: unclosed file <_io.BufferedReader name='test_out.txt'>
.
Warning (from warnings module):
File "C:\Users\Vikas\Documents\fwdregardingprojectdevelopment\huffman.py", line 212
with open(output_file, 'w') as file:
ResourceWarning: unclosed file <_io.BufferedReader name='test3_out.txt'>
.
Warning (from warnings module):
File "C:\Users\Vikas\Documents\fwdregardingprojectdevelopment\huffman.py", line 212
with open(output_file, 'w') as file:
ResourceWarning: unclosed file <_io.BufferedReader name='test2_out.txt'>
.....
----------------------------------------------------------------------
Ran 10 tests in 0.272s
OK
it seems somewhere in your code file 'out_file' is opened and not closed
find where it is opened and close it :
out_file.close()

python script- no error but no result and output is an empty file

I am trying to use the following code to filter a txt file based on the info in 89,90,91 and 92 index.
The problem is that output file is an empty file just with headers.The code is not giving any error-so am not sure how else to go about debugging it.
Thanks for helping!!
for line in fileHandle:
if firstLineFlag == 0: #to skip first line
firstLineFlag = 1
firstLineText = line #save the first line elsewhere
continue
parts = line.strip().split('\t')
try:
column13=float(parts[13-1])
except ValueError:
column13=0
if column13 < 0.01:
if parts[92] == "./.:.:.:.:.":
Nor_info_2 = parts[92].replace("./.:.:.:.:.", "00:1,1:1:1:1,1,1")
if parts[91] == "./.:.:.:.:.":
Nor_info = parts[91].replace("./.:.:.:.:.", "00:1,1:1:1:1,1,1")
if parts[90] == "./.:.:.:.:.":
Tu_info_2 = parts[90].replace("./.:.:.:.:.", "00:1,1:1:1:1,1,1")
if parts[89] == "./.:.:.:.:.":
Tu_info = parts[89].replace("./.:.:.:.:.", "00:1,1:1:1:1,1,1")
normalSplit_2 = parts[92].split(':')
normalSplit = parts[91].split(':')
tumorSplit_2 = parts[90].split(':')
tumorSplit = parts[89].split(':')
print(Nor_info_2)
try:
TD_Tumor_2 = float(tumorSplit_2[3-1])
except ValueError:
TD_Tumor = 0
try:
TD_Tumor = float(tumorSplit[3-1])
except ValueError:
TD_Tumor = 0
try:
TD_Normal_2 = float(normalSplit_2[3-1])
except ValueError:
TD_Tumor = 0
try:
TD_Normal = float(normalSplit[3-1])
except ValueError:
TD_Tumor = 0
if TD_Tumor_2 >= TD_Tumor and TD_Tumor_2 >= 7:
tumorAD=tumorSplit_2[2-1].split(',')
normalAD=normalSplit_2[2-1].split(',')
normalratio=float(normalAD[2-1])/TD_Normal_2
else:
tumorAD=tumorSplit[2-1].split(',')
normalAD=normalSplit[2-1].split(',')
normalratio=float(normalAD[2-1])/TD_Normal
tumorratio=float(tumorAD[2-1])/TD_Tumor_2
parts.append(tumorratio)
parts.append(normalratio)
data.append(parts)
dataz1 = sorted(data, key = itemgetter(91), reverse = True)
#with open('filtered/'+currentFile+'_filtered.txt', 'w') as fileHandle: ## to write your data in proper format
with open(currentFile+'_filtered.txt', 'w') as fileHandle: ## to write your data in proper format
fileHandle.write(firstLineText)
for item in data:
convert_first_to_generator = (str(w) for w in item)
string = '\t'.join(convert_first_to_generator)
string += '\n'
#print string
fileHandle.write(string)
command = 'mv '+currentFile+'_filtered.txt filtered/' ### to move edited files into a different folder
system(command)

Python - Receiving an TypeError

So the code below:
Takes a given square, and if it is an X does nothing. If the square has an O in it, it changes the O to an X and autofills the square above, below, to the left, and to the right.
#openFile(filename): This function opens and prints the users txt file for paint
#input: none
#output: The file that needs to be painted
def openFile(filename):
file = open(filename, 'r')
for line in file:
print(line)
file.close()
#convertFile(filename): This function is used to convert the .txt file into a 2D arrary
#input: none
#output: none
def convertFile(filename):
empty = []
filename = open(filename, 'r')
for line in filename:
line = line.rstrip("\n")
empty.append(list(line))
return empty
#getCoordinates(x,y): This function is used to get the coordinates the user wants to pain from
#input: user coordinates.
#output: none
def getCoordinates(x, y):
coordinates = []
userInt = 0
user = []
try:
user = input("Please enter a square to fill , or q to exit: ")
user.split()
coordinates.append(int(user[0]))
coordinates.append(int(user[2]))
except ValueError:
print("Enter a valid input!")
user = input("Please enter a square to fill, or q to exit: ")
user.split()
coordinates.append(int(user[0]))
coordinates.append(int(user[2]))
return coordinates
def printGrid(grid):
for innerList in grid:
for item in innerList:
print(item, end = "")
print()
#autoFill(board, row, col): This is the heart of the program and the recursive program
# use to fill the board with x's
#input: none
#output: none
def autoFill(grid, rows, cols):
if grid[cols][rows] == "X":
return 0
else:
grid[cols][rows] = "X"
if rows > 0:
autoFill(grid, rows - 1, cols)
if rows < len(grid[cols]) - 1:
autoFill(grid, rows + 1, cols)
if cols > 0:
autoFill(grid, rows, cols - 1)
if cols < len(grid) - 1:
autoFill(grid, rows, cols + 1)
def main():
coordinates = []
empty = []
while True:
filename = input("Please enter a filename: ")
openFile(filename)
empty = convertFile(filename)
coordinates = getCoordinates(len(empty), len(empty[0]))
empty = autoFill(empty(coordinates[0], coordinates[1]))
for item in empty:
s = ""
s.join(item)
for x in item:
s += str(x)
print(s)
if user == "q":
return 0
main()
output should look like:
Please enter a filename: input.txt
OOOOOOXOOOO
OOOOOXOOOOO
OOOOXOOOOOO
XXOOXOOOOOO
XXXXOOOOOOO
OOOOOOOOOOO
Please enter a square to fill, or q to exit: 1, 1
XXXXXXXOOOO
XXXXXXOOOOO
XXXXXOOOOOO
XXXXXOOOOOO
XXXXOOOOOOO
OOOOOOOOOOO
But when i type in the coordinate points i get:
empty = autoFill(empty(coordinates[0], coordinates[1]))
TypeError: 'list' object is not callable
Any guidance in fixing this issue will be much appreciated
The particular error you're asking about is happening because you're trying to call empty (which is a list, as returned by convertFile) as if it were a function.

Resources