CSV reader putting /n after each row - django

I have generated a CSV file from excel.
I am trying to read this CSV file using python CSV. However after each row I get /n. How to remove this /n.
Here is my code:
with open('/users/ps/downloads/test.csv','rU') as csvfile
spamreader = csv.reader(csvfile,dialect=csv.excel_tab)
a = []
for row in csvfile:
a.append(row)
print a
I get result like this:
['HEADER\n', 'a\n', 'b\n', 'c\n', 'd\n', 'e']
I want to have results like this:
['HEADER', 'a', 'b', 'c', 'd', 'e']

you could try a replace
a.replace('\n','')
edit:
working verison- a.append(row.replace('\n',''))

You can use strip
x = ['HEADER\n', 'a\n', 'b\n', 'c\n', 'd\n', 'e']
In [6]: def f(word):
...: return word.strip()
...:
In [7]: map(f, x)
Out[7]: ['HEADER', 'a', 'b', 'c', 'd', 'e']
In [8]:

Related

TypeError: slice indices must be integers or None or have an __index__ method in python 2.7

for file in zip(frames_list[-round(0.2 * len(frames_list)):], masks_list[-round(0.2 * len(masks_list)):]):
# Convert tensors to numpy arrays
frame = frame_batches.next().numpy().astype(np.uint8)
mask = mask_batches.next().numpy().astype(np.uint8)
# Convert numpy arrays to images
frame = Image.fromarray(frame)
mask = Image.fromarray(mask)
# Save frames and masks to correct directories
frame.save(DATA_PATH + '{}_frames/{}'.format(dir_name, dir_name) + '/' + file[0])
mask.save(DATA_PATH + '{}_masks/{}'.format(dir_name, dir_name) + '/' + file[1])
print("Saved {} frames to directory {}".format(len(frames_list), DATA_PATH))
print("Saved {} masks to directory {}".format(len(masks_list), DATA_PATH))
Traceback
Traceback (most recent call last):
File "/home/khawar/Desktop/Khawar_Seg/main.py", line 190, in <module>
generate_image_folder_structure(frame_tensors, masks_tensors, frames_list, masks_list)
File "/home/khawar/Desktop/Khawar_Seg/main.py", line 173, in generate_image_folder_structure
for file in zip(frames_list[-round(0.2 * len(frames_list)):], masks_list[-round(0.2 * len(masks_list)):]):
TypeError: slice indices must be integers or None or have an __index__ method
The round function in python 2.7 returns a float type, but the sequence slice is expecting an int as the argument.
>>> type(round(2.0))
<type 'float'>
>>> items = [0, 1, 2, 3, 4]
>>> items[round(2.0):]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method
# If we cast the rounded index to an int, it will work
>>> index = int(round(2.0))
>>> type(index)
<type 'int'>
>>> items[int(round(2.0)):]
[2, 3, 4]
So, for your example code, you need to cast the indices to integers before using them in the slice -- the [<start>:<end>] part of your for loop.
frames_index = -int(round(0.2 * len(frames_list)))
masks_index = -int(round(0.2 * len(masks_list)))
for file in zip(frames_list[frames_index:], masks_list[masks_index:]):
...
To make things easier to read, I suggest that you use a function to make your index numbers:
def get_list_index(list_size): # list_size is the len(<list>)
float_value = round(0.2 * list_size)
return -int(float_value)
frames_index = get_list_index(len(frames_list))
masks_index = get_list_index(len(masks_list))
for file in zip(frames_list[frames_index:], masks_list[masks_index:]):
...
Edit:
To answer the question in your comment:
what is the meaning of : in for file in zip(frames_list[-round(0.2 * len(frames_list)):]?
The : separates the start index from the end index in python slice notation.
For instance, if you have the list ['a', 'b', 'c', 'd', 'e'] and you wanted to get only the portion from 'b' through 'd', you would use a slice starting from 1 and ending on 4 -- _1 more than 'd''s index.
>>> ['a', 'b', 'c', 'd', 'e'][1:4]
['b', 'c', 'd']
Python lets you use negative indexing, so you can count back from the right side. We could write the same slice using -1 instead of 3:
>>> ['a', 'b', 'c', 'd', 'e'][1:-1]
['b', 'c', 'd']
If we wanted to have all of the items in the list, starting at 'b' and going through the end, we can change our right index to None or just leave it out:
>>> ['a', 'b', 'c', 'd', 'e'][1:None]
['b', 'c', 'd', 'e']
>>> ['a', 'b', 'c', 'd', 'e'][1:]
['b', 'c', 'd', 'e']

How merge dictionary with key values but which contains several different list values?

Someone, asked how my input looks like:
The input is an ouput from preceeding function.
And when I do
print(H1_dict)
The following information is printed to the screen:
defaultdict(<class 'list'>, {2480: ['A', 'C', 'C'], 2651: ['T', 'A', 'G']})
which means the data type is defaultdict with (keys, values) as (class, list)
So something like this:
H1dict = {2480: ['A', 'C', 'C'], 2651: ['T', 'A', 'G'].....}
H2dict = {2480: ['C', 'T', 'T'], 2651: ['C', 'C', 'A'].....}
H1_p1_values = {2480: ['0.25', '0.1', '0.083'], 2651: ['0.43', '0.11', '0.23']....}
H1_p2_values = {2480: ['0.15', '0.15', '0.6'], 2651: ['0.26', '0.083', '0.23']....}
H2_p1_values = {2480: ['0.3', '0.19', '0.5'], 2651: ['0.43', '0.17', '0.083']....}
H2_p2_values = {2480: ['0.3', '0.3', '0.1'], 2651: ['0.39', '0.26', '0.21']....}
I want to merge this dictionaries as:
merged_dict (class, list) or (key, values)= {2480: h1['A', 'C', 'C'], h2 ['C', 'T', 'T'], h1_p1['0.25', '0.1', '0.083'], h1_p2['0.15', '0.15', '0.6'], h2_p1['0.3', '0.19', '0.5'], h2_p2['0.3', '0.3', '0.1'], 2651: h1['T', 'A', 'G'], h2['C', 'C', 'A']....}
So, I want to merge several dictionaries using key values but maintain the order in which different dictionary are supplied.
For merging the dictionary I am able to do it partially using:
merged = [haplotype_A, haplotype_B, hapA_freq_My, hapB_freq_My....]
merged_dict = {}
for k in haplotype_A.__iter__():
merged_dict[k] = tuple(merged_dict[k] for merged_dict in merged)
But, I want to add next level of keys infront of each list, so I can access specific items in a large file when needed.
Downstream I want to access the values inside this merged dictionary using keys each time with for-loop. Something like:
for k, v in merged_dict:
h1_p1sum = sum(float(x) for float in v[index] or v[h1_p1])
h1_p1_prod = mul(float(x) for float in v[index] or v[h1_p1])
h1_string = "-".join(str(x) for x in v[h1_index_level]
and the ability to print or write it to the file line by line
print (h1_string)
print (h1_p1_sum)
I am read several examples from defaultdict and other dict but not able to wrap my head around the process. I have been able to do simple operation but something like this seems a little complicated. I would really appreciate any explanation that you may add to the each step of the process.
Thank you in advance !
If I understand you correctly, you want this:
merged = {'h1': haplotype_A, 'h2': haplotype_B, 'h3': hapA_freq_My, ...}
merged_dict = defaultdict(dict)
for var_name in merged:
for k in merged[var_name]:
merged_dict[k][var_name] = merged[var_name][k]
This should give you an output of:
>>>merged_dict
{'2480': {'h1': ['A', 'C', 'C'], 'h2': ['C', 'T', 'T'], ..}, '2651': {...}}
given of course, the variables are the same as your example data given.
You can access them via nested for loops:
for k in merged_dict:
for sub_key in merged_dict[k]:
print(merged_dict[k][sub_key]) # print entire list
for item in merged[k][sub_key]:
print(item) # prints item in list

Dictionary Key Error

I am trying to construct a dictionary with values from a csv file.Say 10 columns there and i want to set the first column as key and the remaining as Values.
If setting as a for loop the dictionary has to have only one value. Kindly Suggest me a way.
import csv
import numpy
aname = {}
#loading the file in numpy
result=numpy.array(list(csv.reader(open('somefile',"rb"),delimiter=','))).astype('string')
#devolop a dict\
r = {aname[rows[0]]: rows[1:] for rows in result}
print r[0]
Error as follows.
r = {aname[rows[0]]: rows[1:] for rows in result}
KeyError: '2a9ac84c-3315-5576-4dfd-8bc34072360d|11937055'
I'm not entirely sure what you mean to do here, but does this help:
>>> result = [[1, 'a', 'b'], [2, 'c', 'd']]
>>> dict([(row[0], row[1:]) for row in result])
{1: ['a', 'b'], 2: ['c', 'd']}

How to write the retrieved DB from MS SQL server into new CSV File with headers using python 2.7.6

I am trying to view the database retrieved from ms SQL server in CSV file using python with headers(column names)and without any braces and quotes. My code is as follows:
import csv
import pyodbc
outpath="path\\test1.csv"
output = open(outpath, "w")
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=SIPLDT0115;DATABASE=First_eg;UID=sa;PWD=wisdom')
cursor = cnxn.cursor()
sql = "select * from First1"
cursor.execute(sql)
rows = cursor.fetchall()
desc = cursor.description
header = (desc[0][0], desc[1][0], desc[2][0], desc[3][0], desc[4][0])
print "%s %3s %s %3s %3s" % header
for row in rows:
print row
value = str(row).strip('(')
output.write(str(value.replace(')','\n')))
output.close()
f = open("path\\test1.csv").read()
print f
OUTPUT:
F_Name L_Name S_ID Branch Course
('jash', 'u', 123, 'C', 'B')
('jash', 'u', 123, 'C', 'B')
('jash', 'u', 123, 'C', 'B')
'jash', 'u', 123, 'C', 'B'
'jash', 'u', 123, 'C', 'B'
'jash', 'u', 123, 'C', 'B'
In the csv file, it was coming without headers.
I want to view the database as like a table in csv file with header. How? Is it possible? please reply yar!!

how to change one element's value base on its previous one in a loop

In Python 3, how do I change the element between 'b' into Capitalize form.
from
ls = ['a','b','c','d','b','f']
to
ls = ['a','b','C','D','b','f']
is there a way to control the position of iterator?
Using slice:
>>> ls = ['a','b','c','d','b','f']
>>> i = ls.index('b')
>>> j = ls.index('b', i+1)
>>> ls[i+1:j] = map(str.upper, ls[i+1:j])
>>> ls
['a', 'b', 'C', 'D', 'b', 'f']
Personally, I would go with the slicing syntax that has already been suggested. But here's another solution, in case you're interested (same runtime complexity, better space complexity):
>>> ls = ['a','b','c','d','b','f']
>>> mode = False
>>> for i,char in enumerate(ls):
... if char == 'b':
... mode = not mode
... continue
... if mode:
... ls[i] = char.upper()
...
>>> ls
['a', 'b', 'C', 'D', 'b', 'f']