invalid syntax in csv files ipython - python-3.x

these are my input as a csv file but I can not run my code in ipython because of invalid syntax error but I do not know what should I do?
mandana,5,7,3,15
hamid,3,9,4,20,9,1,8,16,0,5,2,4,7,2,1
sina,0,5,20,14
soheila,13,2,5,1,3,10,12,4,13,17,7,7
ali,1,9
sarvin,0,16,16,13,19,2,17,8
def calculate_sorted_averages('C:\Users\Y A S H E L\Desktop\in.csv','C:\Users\Y A S H E L\Desktop\o.csv'):
averages = {}
with open('C:\Users\Y A S H E L\Desktop\in.csv') as csv_file:
csvfile = csv.reader(csv_file, delimiter=',')
for row in csvfile:
scores = []
for i in range(1, len(row)):
scores.append (float(row[i]))
avg = mean(scores)
averages [row[0]] = avg
averages_ord = OrderedDict (sorted (averages.items(), key=lambda x:(x[1], x[0])))
with open ('C:\Users\Y A S H E L\Desktop\o.csv', 'w') as out:
count = 0
for person in averages_ord:
count += 1
if count == 1:
out.write(person+ ","+ str(averages_ord[person]))
else:
out.write("\n"+ person+ ","+ str(averages_ord[person]))

When I copy your function to a python session I get:
def calculate_sorted_averages('C:\Users\Y A S H E L\Desktop\in.csv','C:\Users\Y A S H E L\Desktop\o.csv'):
^
SyntaxError: invalid syntax
You can define a function with
def foo(filename1, filename2):
You can not define it with a literal string, def foo('test.txt'):
A syntax error means your code is wrong at a basic Python syntax level. It doesn't even try to run your code.
This corrects that syntax error. I haven't tried to run it.
def calculate_sorted_averages(file1, file2):
averages = {}
with open(file1) as csv_file:
csvfile = csv.reader(csv_file, delimiter=",")
for row in csvfile:
scores = []
for i in range(1, len(row)):
scores.append(float(row[i]))
avg = mean(scores)
averages[row[0]] = avg
averages_ord = OrderedDict(sorted(averages.items(), key=lambda x: (x[1], x[0])))
with open(file2, "w") as out:
count = 0
for person in averages_ord:
count += 1
if count == 1:
out.write(person + "," + str(averages_ord[person]))
else:
out.write("\n" + person + "," + str(averages_ord[person]))

Related

Do not process already present data

I have a folder in which there are some video files. I want to extract frames from the videos but only those videos should be processed whose names are not present in the csv. It should check for the present video file names in the csv before processing the videos
def extractFrames(m):
global vid_name
vid_files=glob(m)
print(vid_files)
complete_videos = get_completed_videos()
print(complete_videos)
new_vid_files = [x for x in vid_files if get_vid_name(x) not in complete_videos]
for vid in new_vid_files:
print("path of video========>>>>.",vid)
v1=os.path.basename(vid)
try:
vid_name = get_vid_name(vid)
vidcap = cv2.VideoCapture(vid)
except cv2.error as e:
print(e)
except:
print('error')
#condition
fsize=os.stat(vid)
print('=============size of video ===================:' , fsize.st_size)
try:
if (fsize.st_size > 1000):
fps = vidcap.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frameCount/fps
minutes = int(duration/60)
print('fps = ' + str(fps))
print('number of frames = ' + str(frameCount))
print('duration (S) = ' + str(duration))
if (duration > 1):
success,image = vidcap.read()
count=0
success=True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count % 10 == 0 or count ==0:
target_non_target(img_name, image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
except:
print("error")
print('finished processing video ',vid)
with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv', 'a') as csv_file:
fieldnames = ['Video_Name','Process']
file_is_empty = os.stat("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv').st_size == 0
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if file_is_empty:
writer.writeheader()
writer.writerow({'Video_Name':vid_name,'Process':'done'})
def get_vid_name(vid):
return os.path.splitext(os.path.basename(vid))[0]
def get_completed_videos():
completed_videos = []
with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\video_info.csv") as csv_file:
for row in csv.reader(csv_file):
for col in range(0,len(row)):
try:
completed_videos.append(row[col])
except Exception as e:
print(str(e))
print(completed_videos[0])
return completed_videos
Suppose there are 3 videos in a folder. Code is successfully run for those 3 videos and their names are written in the csv. Now if i paste video number 4 in the folder then it should process only the 4th video after checking for the video names present in the csv. Currently it is repeatedly processing all the video files everytime the script is run.
First off, inside the for loop
v1=os.path.basename(vid_files[v_f])
Should be
v1=os.path.basename(new_vid_files[v_f])
Since you are looping over the new_vid_files range. Using those indices on the original list will give you unexpected items. Better yet, you can directly use a for-each loop (since you don't seem to be using v_f for anything other than list access) as follows:
for vid in new_vid_files:
And this vid would replace all instances of new_vid_files[v_f].
Next, you are using vid_name to write to the csv, so you need to perform the same operation for each item from vid_files before matching against complete_videos while creating the new_vid_files list.
If you create a method for getting the video name as follows:
def get_vid_name(vid_file):
return os.path.splitext(os.path.basename(vid_file))[0]
Then you can change the list comprehension to be
new_vid_files = [x for x in vid_files if get_vid_name(x) not in complete_videos]
Edit: As mentioned in the comments to the other answer, the output for complete_videos indicates it isn't being parsed properly. It is appending both the column headers and other unneeded columns. This code will work despite that, but it needs to be fixed. I am not solving it because it is a relatively simple change, and I want the OP to understand what they're doing wrong.
def extractFrames(m):
global vid_name
vid_files=glob(m)
print(vid_files)
complete_videos = get_completed_videos()
new_vid_files = [x for x in vid_files if get_vid_name(x) not in complete_videos]
for vid in new_vid_files:
print("path of video========>>>>.",vid)
v1=os.path.basename(vid)
try:
vid_name = get_vid_name(vid)
vidcap = cv2.VideoCapture(vid)
except cv2.error as e:
print(e)
except:
print('error')
#condition
fsize=os.stat(vid)
print('=============size of video ===================:' , fsize.st_size)
try:
if (fsize.st_size > 1000):
fps = vidcap.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frameCount/fps
minutes = int(duration/60)
print('fps = ' + str(fps))
print('number of frames = ' + str(frameCount))
print('duration (S) = ' + str(duration))
if (duration > 1):
success,image = vidcap.read()
count=0
success=True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count % 10 == 0 or count ==0:
target_non_target(img_name, image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
except:
print("error")
print('finished processing video ',vid)
with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv', 'a') as csv_file:
fieldnames = ['Video_Name','Process']
file_is_empty = os.stat("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv').st_size == 0
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if file_is_empty:
writer.writeheader()
writer.writerow({'Video_Name':vid,'Process':'done'})
def get_vid_name(vid):
return os.path.splitext(os.path.basename(vid))[0]
def get_completed_videos():
completed_videos = []
with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\video_info.csv") as csv_file:
for row in csv.reader(csv_file):
for col in range(0,len(row)):
try:
completed_videos.append(row[col])
except Exception as e:
print(str(e))
print(completed_videos[0])
return completed_videos

Separate and write unique files from delimited text

I am following this tutorial here to separate and write out a delimited text file, but only get one file on output. Is this a python2 -> 3 issue? Help please.
filename = ('file path')
with open(filename) as Input:
op = ''
start = 0
count = 1
for x in Input.read().split("\n"):
if (x == 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'):
if (start == 1):
with open(str(count) + '.txt', 'w') as Output:
Output.write(op)
Output.close()
op = ''
count = + 1
else:
start = 1
Input.close()
You have allways count = 1.
Change this line:
count = + 1
to
count += 1

generating random strings and matching them in python

this is a python program to generate a random string and to match it with a user given output and to get a return on the amount of attempts by the computer but i cant get the try count
import random
class txt:
def __init__(self):
self.txt = None
trycount = 0
def maketxt(self,txt):
txt = ""
a = []
a.append(txt.split())
# return a
# def match(self):
tokenlist = ["a", "b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
matchlist =[]
while (len(matchlist) <=24):
x =random.choice(tokenlist)
matchlist.append(x)
if matchlist == a:
print(trycount)
else :
trycount += 1
match()
t = txt()
t.maketxt("hagjkrshgujrahg")
I keep getting the error
File "C:/Users/#####/AppData/Local/Programs/Python/Python36/test1.py", line 25, in maketxt
trycount += 1
UnboundLocalError: local variable 'trycount' referenced before assignment

Python 3 script for exporting a csv

Hi I have a csv file where there are two columns, one with numbers and one with letters in the following format:
1234 k
343 o
5687 uiuuo
All I want to do is to fill the blank rows with the previous values. I have written that code which saves my work in a new csv but I get an error that says:
b = w[1]
IndexError: list index out of range
This is my code
import csv
with open('col.csv', 'r') as f:
reader = csv.reader(f)
my_list = list(reader)
#print my_list[1]
#x = my_list[1]
#print x[0]
x = 0
for count in my_list:
w = my_list[x]
a = w[0]
b = w[1]
print (a, b)
#print 'a', a , 'b', b
if a == '' and b == '' and x < 3044:
h = x - 1
my_list[x] = my_list[h]
#print 'my_list[x]', my_list[x]
x = x + 1
#print my_list[x]
elif a != '' and b != '' and x < 3044:
my_list[x] = (a,b)
x = x + 1
# print my_list[x]
writer = csv.writer(open('C:/Users/user/Desktop/col2.csv', 'wb'))
#for count in my_list:
data = my_list
for row in data:
writer.writerow(row)
#print count
When you say
blank lines with previous values
I'm assuming that you want to turn:
1234 k
343 o
5687 uiuuo
Into
1234 k
1234 k
343 o
343 o
5687 uiuuo
You have quite a lot of problems with your code:
import csv
with open('col.csv', 'r') as f:
reader = csv.reader(f)
my_list = list(reader)
If you've commented it out you don't need to include it in your question
#print my_list[1]
#x = my_list[1]
#print x[0]
x = 0
for count in my_list:
You do know that your list doesn't contain counts, right? This is just code that lies. Don't do that. Also, if you want to enumerate over a list and get the index along with the value, that's what enumerate is for. It should be for x, value in enumerate(my_list)
w = my_list[x]
a = w[0]
b = w[1]
Your second row doesn't actually have two elements in it. That's why your code fails. Oops.
print (a, b)
#print 'a', a , 'b', b
This code here is a hot mess. Why are you limiting to x < 3044? h is some random variable name that has no meaning. Don't do that either.
if a == '' and b == '' and x < 3044:
h = x - 1
my_list[x] = my_list[h]
#print 'my_list[x]', my_list[x]
x = x + 1
#print my_list[x]
elif a != '' and b != '' and x < 3044:
my_list[x] = (a,b)
x = x + 1
# print my_list[x]
Don't open files like this, it's possible that they'll never get flushed to disk. Or the entire file won't in any case. Always use a with block!
writer = csv.writer(open('C:/Users/user/Desktop/col2.csv', 'wb'))
#for count in my_list:
data = my_list
for row in data:
writer.writerow(row)
#print count
So... there's an interesting assumption here - that your first row must not be empty. I mean, I guess it could, but then you're going to be writing empty rows, and maybe you don't want that. Also your provided input doesn't seem to match what you're doing, since you're not using a \t delimiter.
If you think about what you want to do you can come up with the steps pretty easily:
for each row in the input file
write out that row to the output file
if it's blank/empty, write out the previous row
So that's pretty straight forward then.
import csv
with open('input.csv') as infile, open('output.csv', 'w') as outfile:
reader = csv.reader(infile, delimiter='\t')
writer = csv.writer(outfile)
for row in reader:
writer.writerow(row)
This works - but it doesn't write the previous row if we've got a blank row. Hm. So how can we do that? Well, why not store the previous row? And if the current row is empty, we can write the previous one instead.
previous_row = [] # If the first row is empty we need an empty list
# or whatever you want.
for row in reader:
if not row:
writer.writerow(previous_row)
else:
writer.writerow(row)
previous_row = row
If you want to treat ['', ''] as an empty row, too you just have to tweak the code:
if not row and not all(row):
...
Now if the row is empty, or the row contains false-y items it will skip that one as well.
Try not to index elements of an empty list or assign them to variables.
Most easy way in your case would be simply clone a complete row.
import csv
with open('col.csv', 'r') as f:
reader = csv.reader(f)
my_list = list(reader)
for i in range(0,len(my_list)):
currentLine = my_list[i]
#Make sure it's not the first line and it's empty, else continue
if not currentLine and i > 0:
my_list[i] =my_list[i-1]
with open('C:/Users/user/Desktop/col2.csv','wb') as f:
writer = csv.writer(f)
for row in my_list:
writer.writerow(row)

Find the Mode of a List

L = [98,75,92,87,89,90,92,87]
def mode(L):
shows = []
modeList = []
L.sort()
length = len(L)
for num in L:
count = L.count(num)
shows.append(count)
print 'List = ', L
maxI = shows.index(max(shows))
for i in shows:
if i == maxI:
if modeList == []:
mode = L[i]
modeList.append(mode)
print 'Mode = ', mode
elif mode not in modeList:
mode = L[i]
modeList.append(mode)
print 'Mode = ', mode
return mode
mode(L)
I can't seem to iterate through my list properly...
I can successfully get the first Mode to return >>>(Mode = 87) using the 2nd for-loop however, I can't get it to search the rest of the list so that it will also return >>>(Mode = 92)
I've deleted my attempts at Mode = 92, can you help fill in the blanks?
Thanks
the same idea with collections.Counter
from collections import Counter
L = [98,75,92,87,89,90,92,87]
def mode(L):
# count the occurring frequencies
count = Counter(L)
# find the max
mx = max(count.values())
# collect the frequencies that occur the most
modes = [f for f, m in count.items() if m == mx]
return modes
print(mode(L))
# [87, 92]
Good job on the code. I rewrote it a little. See below code:
L = [98,75,92,87,89,90,92,87]
def mode(L):
# Create a frequency table
freq = {}
for num in L:
if not num in freq:
freq[num] = 1
else:
freq[num] += 1
# Gets maximal occurence
maxoccurence = 0
for f in freq:
maxoccurence = max(maxoccurence, freq[f])
# Put all the numbers with the occurence in a list
modes = []
for f in freq:
if freq[f] == maxoccurence:
modes += [f]
# Returns the list
return modes
print(mode(L))

Resources