使用Python将数据导入excel

时间:2018-10-24 09:47:56

标签: python python-3.x python-2.7 python-requests wxpython

在这段代码中,我从名为“ emails”的excel工作表中的两个列表中导入了数据:

a。列表1

b。 list2

处理该数据并将该数据存储在最后两个列表中:

a。 final_list1

b。 final_list2

现在我想将这两个最终列表中存在的数据存储在同一Excel中的我无法完成的新工作表中,请帮助我

打印(final_list2)后代码未运行

public class TestPattern
{
  public static void main(String[] args)
  {
    try
    {
      Scanner scanner;
      String  line;
      scanner = new Scanner(System.in);
      for (;;)
      {
        System.out.print("Enter line: ");
        System.out.flush();
        line = scanner.nextLine();
        if (line.isEmpty())
          break;
        check(line);
      }
      scanner.close();
    }
    catch (Exception exception_ipar)
    {
      exception_ipar.printStackTrace();
    }

  } // main

  public static void check(String line)
  {
    // ----------------------------------
    // Determine patterns present in line
    // ----------------------------------
    List<String>     found;
    Iterator<String> it;
    String           pattern;
    found = new ArrayList<String>();
    it = patterns.iterator();
    while (it.hasNext())
    {
      pattern = it.next();
      if (line.contains(pattern))
        found.add(pattern);
    }

    // -------------------
    // Maybe nothing found
    // -------------------
    if (found.isEmpty())
    {
      System.out.println("No pattern found");
      return;
    }

    // --------------
    // Compose result
    // --------------
    StringBuilder result;
    int           counter;
    result = new StringBuilder();
    result.append(line + " | ");
    for (counter = 0; counter < found.size(); counter++)
    {
      if (counter == 0)
        result.append("this is ");
      else if (counter == (found.size() - 1))
        result.append(" and ");
      else
        result.append(", ");
      result.append(found.get(counter));
    }

    // -----------
    // Show result
    // -----------
    System.out.println(result);

  } // check

  private static Set<String> patterns;

  static
  {
    patterns = new TreeSet<String>();
    patterns.add("A");
    patterns.add("B");
    patterns.add("C");
  }

} // class TestPattern

错误是:

import xlrd
import xlwt
file_location = "E:/emails.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
list1 = [sheet.cell_value(r,0) for r in range(sheet.nrows)]
type(list1)
for r in range(sheet.nrows):
        print(list1[r])

final_list1 = [] 
for num in list1:
    if num not in final_list1:
        final_list1.append(num)
print(final_list1)

list2 = [sheet.cell_value(r,1) for r in range(sheet.nrows)]
type(list2)
for r in range(sheet.nrows):
        print(list2[r])

final_list2 = [] 
for num in list2:
    if num not in final_list2:
        final_list2.append(num)
print(final_list2)


final_list2 = [elem for elem in final_list2 if elem not in final_list1 ]
print(final_list2)




book = xlwt.Workbook(encoding="utf-8")
sh = book.add_sheet("sh")
i=1
for r in range(10):
    for col_index, item in enumerate(final_list2):
        sh.write(i, col_index, item)      
book.save("emails.xlsx")

1 个答案:

答案 0 :(得分:0)

编辑:

xlwt库确实应该仅与旧的excel文件(.xls)文件一起使用,这样可能会引起问题。参见docs

  

xlwt是一个用于向较早的Excel文件(即.xls)写入数据和格式化信息的库。


问题似乎在于工作表... for(let i=0; i<childLength; i++) { let child = container.getChildAt(i) // Determine the child if(child.typeName == 'TextView') // this is TextView else if(child.typeName == 'Image') // this is Image } ... 已经存在,因此您无法添加它。尝试为其指定一个尚不存在的工作表名称,或使用sh,它将覆盖工作表。

有关更多信息,请参见https://xlwt.readthedocs.io/en/latest/api.html?highlight=add