0

I try to build data elimination code in python. In this script I try to apply some criteria to eliminate data file and save new data file with the imput that have defined value greater than 0.003 for example. Why do I get this error in these python script ? I added errors and script together. Which point that Ive failed ?

#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import unicode_literals import csv import itertools import math def main(): # ****** f = open("deneme.csv", "r") reader = csv.reader(f) lines = itertools.islice(reader, 3, None, 5) n = open("results.csv", "wb") wr = csv.writer(n, quoting = csv.QUOTE_ALL) counter = 0 line_count = 0 for line in lines: line_count += 5 w, h = 3, 3 matrix = [[0 for x in range(w)] for y in range(h)] matrix[0][0] = float(line[1]) * (10 ** float(line[0])) matrix[0][1] = float(line[11]) * (10 ** float(line[0])) matrix[0][2] = float(line[9]) * (10 ** float(line[0])) matrix[1][0] = float(line[11]) * (10 ** float(line[0])) matrix[1][1] = float(line[3]) * (10 ** float(line[0])) matrix[1][2] = float(line[7]) * (10 ** float(line[0])) matrix[2][0] = float(line[9]) * (10 ** float(line[0])) matrix[2][1] = float(line[7]) * (10 ** float(line[0])) matrix[2][2] = float(line[5]) * (10 ** float(line[0])) trace = (matrix[0][0] + matrix[1][1] + matrix[2][2]) / 3 norm = 0 for i in range(3): for x in range(3): norm += matrix[i][x] * matrix[i][x] norm = math.sqrt(norm) if (trace / norm) > 0.0003: print(line_count - 4, line_count) # ****** f = open("deneme.csv", "r") reader = csv.reader(f) rows = itertools.islice(reader, line_count - 5, line_count, None) for i in rows: print(i) wr.writerow(i) counter += 1 print(line[0]) print(matrix) print(trace) print(norm) print('buyuk') print('____________**********____________') print(counter) if __name__ == "__main__": main() 

Any advice for this case ? Thanks in advance. I also add csv files in here Error;

 Traceback (most recent call last): File "/Users/gokceoter/PycharmProjects/CMT/cmttt.py", line 74, in <module> 21 25 ['PDEW', '39083', '00:31:45.0', '1.23', '67.12', '10', '5.1', '5.4', 'CARLSBERG', 'RIDGE', '', '', '', '', '', '', ''] main() File "/Users/gokceoter/PycharmProjects/CMT/cmttt.py", line 58, in main wr.writerow(i) TypeError: a bytes-like object is required, not 'str' 

1 Answer 1

1

You've opened the output file in binary mode

n = open("results.csv", "wb") # <---------- the `b` here wr = csv.writer(n, quoting = csv.QUOTE_ALL) 

so writes to that file expect a bytes object not a str object. You can convert between the two with str.encode and bytes.decode.

for row in rows: # why would you use `i` here? Don't. wr.writerow(row.encode()) 
Sign up to request clarification or add additional context in comments.

2 Comments

I am really new at python. Still I can not achieve, I tried to manuplate someone's code, and these errors still occur :/
@kejinatsu my code will fix the error you posted. If you're having more issues please post a new question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.