I'm iterating through a bunch of SDE's, then iterating through those and producing feature class titles, and it's in a list. I then perform a list.replace() and turn it into a comma delimited string.
So, I want to take that delimited string such as:
SDE_name1,thing1,thing2,thing3,thing4
SDE_name2,thing1,thing2,thing3,thing4
SDE_name3,thing1,thing2,thing3,thing4
and insert it into excel using XLWT
I can get it to write the entire length into one cell, with or without the commas,
but I'd like it to write each item into a new column...
So column A would have SDE_name1
Column B would have thing1
columb C would have thing2 etc etc etc
So far I've tried:
listrow=2
listcol=0
for row in list:
worksheet.write(listrow,listcol,row,style)
listrow+=1
wbk.save(bookname)
and
listrow=2
listcol=0
list=something.split(anotherlist,delimiter=",")
for row in list:
worksheet.write(listrow,listcol,row,style)
listrow+=1
wbk.save(bookname)
So both with and without a delimiter. Either way, both write everything to one column left to right. It will write each item in the list to a new row...but I need it to write each item after the comma to a new column.
Any idea?
You are not iterating through the columns. As it's not clear exactly what the variables in your example refer to, assume you start with the following
data = ["SDE_name1,thing1,thing2,thing3,thing4",
"SDE_name2,thing1,thing2,thing3,thing4",
"SDE_name3,thing1,thing2,thing3,thing4"]
The code to write the Excel file becomes:
rowCount = 2
for row in data: # row = "SDE_name1,thing1,thing2,thing3,thing4"
colCount = 0
for column in row.split(","): # column = ["SDE_name1", "thing1", "thing2", "thing3", "thing4"]
worksheet.write(rowCount, colCount, column, style)
colCount += 1
rowCount += 1
wbk.save(bookname)
Related
I have a dataframe test. My goal is to search in the column t1 for specific strings, and if it matches exactly a specific string, put that string in the next column over called t1_selected. Only thing is, I can't get iterrows() to go over the entire dataframe, and to report results in respective rows.
for index, row in test.iterrows():
if any(['ABCD_T1w_MPR_vNav_passive' in row['t1']]):
#x = ast.literal_eval(row['t1'])
test.loc[i, 't1_selected'] = str(['ABCD_T1w_MPR_vNav_passive'])
I am only trying to get ABCD_T1w_MPR_vNav_passive to be in the 4th row under the t1_selected, while all the other rows will have not found. The first entry in t1_selected is from the last row under t1 which I didn't include in the screenshot because the dataframe has over 200 rows.
I tried to initialize an empty list to append output of
import ast
x = ast.literal_eval(row['t1'])
to see if I can put x in there, but the same issue occurred.
Is there anything I am missing?
for index, row in test.iterrows():
if any(['ABCD_T1w_MPR_vNav_passive' in row['t1']]):
#x = ast.literal_eval(row['t1'])
test.loc[index, 't1_selected'] = str(['ABCD_T1w_MPR_vNav_passive'])
Where index is the row its written to. With i it was not changing
I want my program to read 2 columns (the first and the second one) and add them to an array. They are dependent on eachother - so they need to be written alongside eachother, as in the first row (both columns) next to eachother, and then the second row and so on.
I have managed to write the first column (containing the names) to the array, however have not managed to write the second column to the array.
rownum=1
array=[]
for row in reader:
if row[1] != '' and row[1] != 'Score':
array.append(row[1])
rownum=rownum+1
if rownum==11:
break
I attempted to append more than one row however it returns the error message 'only accepts one argument'.
Any ideas how I can do this so i can reference the score for each name from the csv file
Try using a dictionary.
d = {} #curly braces denote an empty dictionary
for row in reader:
d[row[0]] = row[1]
d, in this case, would be a dictionary with the first column of your csv file as the keys and the second column as the corresponding values.
You can access it very similar to how you access a list. Say you had Brian,80 as one of the entries in your csv file, d["Brian"] would return 80.
EDIT
OP has requested (in the comments) for a more complete version of the code. Assuming OP's code already works, I'll modify that code so it works with a dictionary:
rownum=1
d={} #denotes an empty dictionary
for row in reader:
if row[1] != '' and row[1] != 'Score':
d[row[0]]=row[1] #first column is the key/index, second column is the value
rownum=rownum+1
if rownum==11:
break
I am new to Python especially when it comes to using it with Excel. I need to write code to search for the string “Mac”, “Asus”, “AlienWare”, “Sony”, or “Gigabit” within a longer string for each cell in column A. Depending on which of these strings it finds within the entire entry in column A’s cell, it should write one of these 5 strings to the corresponding row in column C’s cell. Else if it doesn’t find any of the five, it would write “Other” to the corresponding row in column C. For example, if Column A2’s cell contained the string “ProLiant Asus DL980 G7, the correct code would write “Asus” to column C2’s cell. It should do this for every single cell in column A, writing the appropriate string to the corresponding cell in column C. Every cell in column A will have one of the five strings Mac, Asus, AlienWare, Sony, or Gigabit within it. If it doesn’t contain one of those strings, I want the corresponding cell in column 3 to have the string “Other” written to it. So far, this is the code that I have (not much at all):
import openpyxl
wb = openpyxl.load_workbook(path)
sheet = wb.active
for i in range (sheet.max_row):
cell1 = sheet.cell (row = i, column = 1)
cell2 = sheet.cell (row = I, column = 3)
# missing code here
wb.save(path)
You haven't tried writing any code to solve the problem. You might want to first get openpyxl to write to the excel workbook and verify that is working - even if it's dummy data. This page looks helpful - here
Once that is working all you'd need is a simple function that takes in a string as an argument.
def get_column_c_value(string_from_column_a):
if "Lenovo" in string_from_column_a:
return "Lenovo"
else if "HP" in string_from_column_a:
return "HP"
# strings you need to check for here in the same format as above
else return "other"
Try out those and if you have any issues let me know where you're getting stuck.
I have not worked much with openpyxl, but it sounds like you are trying to do a simple string search.
You can access individual cells by using
cell1.internal_value
Then, your if/else statement would look something like
if "HP" in str(cell1.internal_value):
Data can be assigned directly to a cell so you could have
ws['C' + str(i)] = "HP"
You could do this for all of the data in your cells
I am writing data from xls file column by column. Now i want to write that data in csv same as column by column.
Problem is i am not getting how to switch the pointer to next column.
Currently i am getting O/P as below from my code:
abc
pqr
,
def
ghi
What i want is
abc,def
pqr,ghi
My sample code:
for k in col1,col2:
for i in range(2,10):
test = (sheet.cell(row=i, column=k).value)
c.writerow([test])
c.writerow(",") #switch to next column.. Not working
Please help...
The problem is that you are writing the row out on every element, you should probably put each column element into a list, cast it as a tuple (because that's what writerow expects, and write the entire row out at once.
You are also using rows on the inner loop, where you should likely be using columns. The way you had it you were going down each row then column, when it should be the other way around. Like reading a book, you tackle each word individually (column item in inner loop) and you move down to the next line (row item in outer loop) when you are done with the current line.
# Rows 2-9
for i in range(2,10):
row = []
for k in col1,col2:
row.append(unicode(sheet.cell(row=i, column=k).value))
c.writerow(tuple(row)) #switch to next column.. Not working
How do you create a tabular data structure form this list of sublists using GSpread or XLWT in which every item at index 0 is in the first column, every item at index 1 is in the second column, etc.?
For example, I'd like to put all the a's in the following list into column1, all the b's into column2, etc. In other words, I only want one value to one cell, so I'd like 'a' in the first cell in the first column, 'aa1' in the second cell of the first column, 'aa2' in the third cell of the first column, etc.
lst = [[['a','b','c'],['aa1','bb1','cc1'],['aaa2','bbb2','ccc2']],[['a','b','c'],['aa1','bb1','cc1'],['aaa2','bbb2','ccc2']]]
This is what I have, which is using a for-loop, but I'm wondering if there is a different method where I could create one for loop that way I wouldn't have to manually create a for loop for every extra column.
gc = gspread.login('username', 'password')
sheet = gc.open("Curalate").sheet1
row = 1
for subsublist in lst[0]:
sheet.update_cell(1,row,subsublist[0])
row = row + 1
row = 1
for subsublist in lst[0]:
sheet.update_cell(2,row,subsublist[1])
row = row + 1
row = 1
for subsublist in lst[0]:
sheet.update_cell(,row,subsublist[2])
row = row + 13
also, if this were xlwt, it's exactly the same except sheet.udpate_cell would be replaced with sheet.write and it's organized sheet.write(row,column,datapoint) instead of (column, row, datapoint).
Based on the comment: "each sublist with have its own sheet", I think you can do this quite simply with a nested loop. I'm familiar with xlwt but not gspread, so I'll demonstrate with xlwt syntax:
# assume worksheet is an xlwt worksheet object
lst = [['a','b','c'],['aa1','bb1','cc1'],['aaa2','bbb2','ccc2']]
for (rownum, rowlist) in enumerate(lst):
for (colnum, value) in enumerate(rowlist):
worksheet.write(rownum, colnum, value)
Both enumerate and xlwt are 0-indexed (at least in the Python interface) so you don't need to change that. If gspread is 1-indexed (as the Excel human interface is), adding 1 to rownum and/or colnum as needed should correct that.
It sounds like you'll have more than one worksheet, depending on the number of two-dimensional sublists in your main list. Again, you can nest for that:
wb = xlwt.Workbook()
for (sheet_number, sublist) in enumerate(lst):
ws = wb.add_sheet('sheet_%s', sheet_number + 1)
for (rownum, rowlist) in enumerate(sublist):
for (colnum, value) in enumerate(rowlist):
ws.write(rownum, colnum, value)