简体   繁体   中英

Exporting python lists into excel columns

from collections import Counter
import pandas as pd
import string
import xlwt
from xlwt import Workbook
wb=Workbook()
sheet2=wb.add_sheet('Sheet2')
sheet2 = wb.add_sheet("Sheet 2", cell_overwrite_ok=True)
sheet2.title="FINAL RESULTS"
df=pd.read_excel("Book2.xlsx", sheet_name=0)
df=df.astype('object')
df.info()
df_c1=df['Signal']
df_c2=df['DCS number']
list1_with_letters=list(df_c1)
list2_with_letters=list(df_c2)
new_list1=[]
new_list2=[]
def duplicates(lst, item):
        return [i for i, x in enumerate(lst) if x == item]
#stripping the characters for COMOS list
for x in list1_with_letters:
        x=str(x)
        new_x=''.join(filter(str.isdigit, x))
        new_list1.append(new_x)
#stripping the characters for DCS list
for y in list2_with_letters:
        y=str(y)
        new_y=''.join(filter(str.isdigit, y))
        new_list2.append(new_y)

new_list1 = list(filter(None, new_list1))
seen = set()
#we take out the duplicates of the COMOS list 
new_list1_in_order= []
for item in new_list1:
    if item not in seen:
        seen.add(item)
        new_list1_in_order.append(item)

for elem1 in new_list1_in_order: #loop through COMOS list
    index_duplicates_DCS=duplicates(new_list2,elem1)
    matched= [list2_with_letters[i] for i in index_duplicates_DCS]
    matched=str(matched)
    elem1_str=str(elem1) #convert the found element from new_list 2 into a string type
    print(elem1_str+ "-->"+ matched)
   #CODE WORKS UP TO HERE
size_matched=len(matched)
size_new_list1_in_order=len(new_list1_in_order)
for x in range(size_new_list1_in_order):
        for y in range(size_matched):
                sheet2.write(x,y,matched[y])


wb.save('sample_book.xls')
  1. If you run the code up until #CODE WORKS up to here, you will get this sample output:
690205-->['AAH690205', 'AHH690205', 'LI690205', 'TDX690205']
690206-->['AAH690206', 'AHH690206', 'LI690206', 'TAHH690206', 'THH690206', 'TI690206', 'TNHH690206']

What I'm trying to do now is to print this data to an excel sheet like this:

Column1 Column 2
690205  AAH690205
        AHH690205
        LI690205 
        TDX690205
690206  AAH690206
        LI690206
        TAHH690206
        THH690206
        TI690206
and so on and so forth

I realize that the code is poorly written (first time coding), but can someone help me achieve the part after #CODE WORKS UP TO HERE

For this, I used a dictionary to organize the information.

690205-->['AAH690205', 'AHH690205', 'LI690205', 'TDX690205']
690206-->['AAH690206', 'AHH690206', 'LI690206', 'TAHH690206', 'THH690206', 'TI690206', 'TNHH690206']

The numbers before the arrows are used as the key in each member of the dictionary:

69020
690206

The numbers in each list are stored as the value of each key in the dictionary. To explain this, this is what my dictionary looks like:

columns = {
    690205 : ['AAH690205', 'AHH690205', 'LI690205', 'TDX690205'],
    690206 : ['AAH690206', 'AHH690206', 'LI690206', 'TAHH690206', 'THH690206', 'TI690206', 'TNHH690206'],
    }

To write each key in their proper spot, I used a variable that would be set to the length of the value of the previous key . The values were much easier to write as I just had to store them in a list and iterate over that list .

import xlwt 
from xlwt import Workbook 

wb = Workbook() 

sheet = wb.add_sheet('Sheet 1', cell_overwrite_ok=True) 

# write columns that will always be there
sheet.write(0, 0, 'Column 1') 
sheet.write(0, 1, 'Column 2')

columns = {
    690205 : ['AAH690205', 'AHH690205', 'LI690205', 'TDX690205'],
    690206 : ['AAH690206', 'AHH690206', 'LI690206', 'TAHH690206', 'THH690206', 'TI690206', 'TNHH690206'],
    }


# key_list is used to store each key in order
key_list = []

for key in columns:
    key_list.append(key)


# key_index needs to start at one to prevent overriding of the column names (Column 1 and Column 2)
# key_index will be used to place each key in their correct spot
key_index = 1

for key in key_list:
    # writes the key at the correct key_index
    sheet.write(key_index, 0, key)
    # gets the length of the value for the key
    key_value_length = len(columns[key])
    # adds key_value_length to key_index to put the next key at the correct place
    key_index += key_value_length


# values_list is used to store all of the values of each key in order
value_list = []

for values in columns.values():
    for value in values:
        value_list.append(value)
        # getting index number of the value in the value_list
        index = value_list.index(value)
        # have to add one to the index because the indexes for the values will start at 1, not 0. This prevents overriding of the cell 'Column 2'
        sheet.write(index+1 , 1 ,value)

wb.save('examplesheet.xls')

Output: 电子表格输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM