【问题标题】:Exporting python lists into excel columns将 python 列表导出到 excel 列中
【发布时间】:2020-01-29 10:40:14
【问题描述】:
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. 如果您一直运行代码直到#CODE WORKS up to here,您将获得以下示例输出:
690205-->['AAH690205', 'AHH690205', 'LI690205', 'TDX690205']
690206-->['AAH690206', 'AHH690206', 'LI690206', 'TAHH690206', 'THH690206', 'TI690206', 'TNHH690206']

我现在要做的是将这些数据打印到这样的 excel 表中:

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

我意识到代码写得不好(第一次编码),但有人可以帮我在#CODE WORKS UP TO HERE 之后完成部分

【问题讨论】:

    标签: python excel list loops


    【解决方案1】:

    为此,我使用了字典来组织信息。

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

    箭头前的数字在字典的每个成员中用作key

    69020
    690206
    

    每个列表中的数字存储为字典中每个keyvalue。为了解释这一点,这就是我的字典的样子:

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

    为了将每个key 写在适当的位置,我使用了一个变量,该变量将设置为前一个keyvalue 的长度。 values 更容易编写,因为我只需将它们存储在 list 中并遍历 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')
    

    输出:

    【讨论】:

      猜你喜欢
      • 2012-07-30
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多