Issue with writing CSV in Python - python

I am reading and XLSX file. and looping over the rows and colums to remove all the clutter from the excel.
with open("../Converted/test.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(header) # write the header
for i in range(10, max_row + 1):
full_data_row = ""
for j in range(1, max_col + 1):
cell_obj = sheet_obj.cell(row = i, column = j)
if cell_obj.value is not None:
full_data_row += str(cell_obj.value) + ','
full_data_row = full_data_row[:-1]
writer.writerow(full_data_row)
When I print full_data_row it prints the string with , so that is correct.
When i check my csv file. I get this as an ouput:
header1,header2,header3
O,p,z,e,t, ,h,o,s,t,e,d,

csvwriter.writerow() expects an iterable (e.g. a list), and will handle the formatting (placing commas between values) for you. Therefore, it parses your string as a list (of characters), printing commas between every character.
An alternative approach could be:
for i in range(10, max_row + 1):
full_data_row = []
for j in range(1, max_col + 1):
cell_obj = sheet_obj.cell(row = i, column = j)
if cell_obj.value is not None:
full_data_row.append(str(cell_obj.value))
writer.writerow(full_data_row)

Related

naive algorthim string matching

import pandas as pd
import csv
def naive_string_matching(text, pattern):
n = len(text)
m = len(pattern)
for i in range(n - m + 1):
j = 0
while j < m and text[i + j] == pattern[j]:
j += 1
if j == m:
return i
return -1
# Initialize an empty list to store the row strings
row_strings = []
# Open the CSV file for reading
with open("pcr_data.csv", 'r') as csv_file:
# Create a CSV reader object
reader = csv.reader(csv_file)
# Iterate through the rows in the CSV file
for i, row in enumerate(reader):
# Convert the row to a string
row_string = ''.join(row)
# Append the row string to the list
row_strings.append(row_string)
print('line[{}] = {}'.format(i, row_strings))
def search_in_file(text, pattern):
index = naive_string_matching(text, pattern)
if index != -1:
print("The pattern found at index: ", index)
else:
print("The pattern was not found in the file")
search_in_file(row_strings,'01001111110')
my data in cvs file
#from this [10101010,10101010] to:
#'01011111001', '11101111111', '11101111011', '11101111011', #'11011111011', '10101111001',
I have CVS file with 1740 rows and 11 col, and i store the data in list of string and convert from 001010101 to string with remove( , )
but i have problem here the orginal algothrtim doesnot work and give me this error
TypeError: object of type '_io.TextIOWrapper' has no len()
and it cannot find the match pattern.
I try use pandas for reading but also give me wrong result
please help me

I lose leading zeros when copy data from dataframe to openpyxl.workbook

I use openpyxl and pandas to fill row color with specified condition. Everything works fine but in some cells I lose leading zeros (like 0345 -> output 345), I don't want that. How can I get the exact data?
dt = pd.read_excel(file_luu, sheet_name="Sheet1")
dt = pd.DataFrame(dt)
dinhDanh = len(dt.columns) - 1
wb = load_workbook(file_luu)
print(type(wb))
ws = wb['Sheet1']
for i in range(0, dt.shape[1]):
ws.cell(row=1, column=i + 1).value = dt.columns[i]
for row in range(dt.shape[0]):
for col in range(dt.shape[1] ):
ws.cell(row + 2, col + 1).value = str(dt.iat[row, col]) if (str(dt.iat[row, col]) != "nan") else " "
if dt.iat[row, dinhDanh] == True:
ws.cell(row + 2, col + 1).fill = PatternFill(start_color='FFD970', end_color='FFD970',
fill_type="solid") # used hex code for brown color
ws.delete_cols(1)
ws.delete_cols(dinhDanh)
wb.save(file_luu)
Copy exactly all characters

How to transform a csv file into a multi-dimensional list using Python?

I started out with a 4d list, something like
tokens = [[[["a"], ["b"], ["c"]], [["d"]]], [[["e"], ["f"], ["g"]],[["h"], ["i"], ["j"], ["k"], ["l"]]]]
So I converted this to a csv file using the code
import csv
def export_to_csv(tokens):
csv_list = [["A", "B", "C", word]]
for h_index, h in enumerate(tokens):
for i_index, i in enumerate(h):
for j_index, j in enumerate(i):
csv_list.append([h_index, i_index, j_index, j])
with open('TEST.csv', 'w') as f:
# using csv.writer method from CSV package
write = csv.writer(f)
write.writerows(csv_list)
But now I want to do the reverse process, want to convert a csv file obtained in this format, back to the list format mentioned above.
Assuming you wanted your csv file to look something like this (there were a couple typos in the posted code):
A,B,C,word
0,0,0,a
0,0,1,b
0,0,2,c
...
here's one solution:
import csv
def import_from_csv(filename):
retval = []
with open(filename) as fh:
reader = csv.reader(fh)
# discard header row
next(reader)
# process data rows
for (x,y,z,word) in reader:
x = int(x)
y = int(y)
z = int(z)
retval.extend([[[]]] * (x + 1 - len(retval)))
retval[x].extend([[]] * (y + 1 - len(retval[x])))
retval[x][y].extend([0] * (z + 1 - len(retval[x][y])))
retval[x][y][z] = [word]
return retval
def import_from_csv(file):
import ast
import csv
data = []
# Read the CSV file
with open(file) as fp:
reader = csv.reader(fp)
# Skip the first line, which contains the headers
next(reader)
for line in reader:
# Read the first 3 elements of the line
a, b, c = [int(i) for i in line[:3]]
# When we read it back, everything comes in as strings. Use
# `literal_eval` to convert it to a Python list
value = ast.literal_eval(line[3])
# Extend the list to accomodate the new element
data.append([[[]]]) if len(data) < a + 1 else None
data[a].append([[]]) if len(data[a]) < b + 1 else None
data[a][b].append([]) if len(data[a][b]) < c + 1 else None
data[a][b][c] = value
return data
# Test
assert import_from_csv("TEST.csv") == tokens
First, I'd make writing this construction in a CSV format independent from dimensions:
import csv
def deep_iter(seq):
for i, val in enumerate(seq):
if type(val) is list:
for others in deep_iter(val):
yield i, *others
else:
yield i, val
with open('TEST.csv', 'w') as f:
csv.writer(f).writerows(deep_iter(tokens))
Next, we can use the lexicographic order of the indices to recreate the structure. All we have to do is sequentially move deeper into the output list according to the indices of a word. We stop at the penultimate index to get the last list, because the last index is pointing only at the place of the word in this list and doesn't matter due to the natural ordering:
with open('TEST.csv', 'r') as f:
rows = [*csv.reader(f)]
res = []
for r in rows:
index = r[:-2] # skip the last index and word
e = res
while index:
i = int(index.pop(0)) # get next part of a current index
if i < len(e):
e = e[i]
else:
e.append([]) # add new record at this level
e = e[-1]
e.append(r[-1]) # append the word to the corresponding list

Copying same range multiple times from one workbook to another

Per the attached image, I am trying to copy and paste the same data into a different format.
I have figured out the first part of the code but I need help abbreviating the 2nd half after this comment:
"Fills in the concepts per store group step by step"
Currently, this code is not efficient and I would like to have it compressed into just a couple of lines.
Image of desired result (Right hand side):
Here is the code I have cobbled together so far:
import openpyxl as xl;
filename ="c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]
# opening the destination excel file
filename1 ="c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist2.xlsx"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.worksheets[0]
# copying the cell values from source
# excel file to destination excel file
rowctsq = ws1['A1']
j = 0
while j < rowctsq.value:
j = j + 3
for i in range (3 , 6):
# reading cell value from source excel file
# Populates the store list repeatedly
c = ws1.cell(row = i, column = 1)
ws2.cell(row =i , column = 1).value = c.value
ws2.cell(row =i + j , column = 1).value = c.value
# Fills in the concepts per store group step by step
c = ws1.cell(row = i, column = 2)
ws2.cell(row =i , column = 3).value = c.value
c = ws1.cell(row = i, column = 3)
ws2.cell(row =i + 3 , column = 3).value = c.value
c = ws1.cell(row = i, column = 4)
ws2.cell(row =i + 6 , column = 3).value = c.value
c = ws1.cell(row = i, column = 5)
ws2.cell(row =i + 9 , column = 3).value = c.value
# saving the destination excel file
wb2.save('c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist2.xlsx')
Hopefully, I get extra community points for answering my own question! I worked through this and have pretty much gotten to my destination. Here's the code I came up with. Works like a charm. :)
import openpyxl as xl;
filename ="c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]
# opening the destination excel file
filename1 ="c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist2.xlsx"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.worksheets[0]
# copying the cell values from source
# excel file to destination excel file
rowctsq = ws1['A1']
j = 0
k = 0
while j < rowctsq.value and k < 6:
j = j + 3
k = k + 1
for i in range (3 , 6):
# reading cell value from source excel file
# Populates store column
c = ws1.cell(row = i, column = 1)
ws2.cell(row =i + j , column = 1).value = c.value
# Populates concept 'x' column
c = ws1.cell(row = i, column = 1 + k)
ws2.cell(row =i + j , column = 3).value = c.value
# Populates concept name column
c = ws1.cell(row = 2, column = 1 + k)
ws2.cell(row =i + j , column = 2).value = c.value
# saving the destination excel file
wb2.save('c:\\Users\kevin\Documents\Python Programs\Excel Python\Conceptlist2.xlsx')

Python , Using Astype partially on the array

Im having a problem using astype and multiplying the values by 255.
The problem is, the first column of this CSV shouldnt be changed by asType or multiplied because its a label for a class.
How can i do this conversion to 'uint8' and multipliying by 255 without changing the 0 col of the csv ?
with open(trainingFolder + "standardization_traindata.csv", 'wb') as fp:
for i in range(0, aux_data.shape[0]):
column = aux_data[i, :].tolist()
#
column = np.array(column).astype('uint8') * 255
#
column = map(lambda x: str(x) + ',', column)
column = ''.join(column)[0:-1]
fp.write(column + '\n')
I Solved by doing this :
with open(testingFolder + "zca_whitening_testdata.csv", 'wb') as fp:
writer = csv.writer(fp)
temp_list = []
temp_list.append("label")
for j in range(0,aux_data.shape[1]-1):
header = "pixel"+str(j)
temp_list.append(header)
writer.writerow(temp_list)
for i in range(0, aux_data.shape[0]):
writer.writerow(aux_data[i,:].tolist())

Categories