简体   繁体   中英

How to format concatenated list output - python

I am using pyexcel to try to filter an excel file for certain values, but I can't figure out how to format my functions output.

I need to output cell values in the form [x,y] for pyexcel to read the data, currently my output is coming out as [x, y], and the space before the y value makes it so pyexcel crashes.

Saw some questions answered using the % string format method, but this has no effect for my script.

This is what I currently have, crash location identified with <---:

import pyexcel as pe

sheet = pe.get_sheet(file_name="foo.xlsx") 

print sheet #displays excel file

#these are accepted ways to output a cell value
#sheet[1,2], "numeric" 
#sheet['B1'], "alphanumeric"

list_rows = list(range(11))   #Max Row Value +1 as upper bound
list_columns = list(range(4))    #Max Column Value +1 as upper bound

def Scroller(max_row,max_column):
  r = 0
  while r <= (len(list_rows)-1):
    c = 0
    while c <= (len(list_columns)-1):
      index = [list_rows[r],list_columns[c]]
      print "%s" % index
      print sheet[index]  <------
      c += 1
    r += 1

Scroller((len(list_rows)-1),(len(list_columns)-1))

While this iterates correctly to hit all cell values, its output has a space like:

[0, 0]
[0, 1]
[0, 2] ... etc.

I need:

[0,0]
[0,1]
[0,2] ... etc.

Thanks in advance for any help!

Caveat: I've never used pyexcel as I prefer CSV modules...

No experience with this module, but given your syntax, couple of thoughts here:

  1. sheet is some object that can be accessed via the __getitem__() method
  2. To access something in sheet its either sheet[row, col] or sheet['A#']
  3. Your saying that sheet[1, 2] will crash due to the space in [1, 2]

Given these constraints, lets examine what you've posted. First, lets start with your sample outputs:

print "%s" % index
# [0, 0]
# [0, 1]
# [0, 2] ... etc.

Well this tells me that index is of type list containing ints, which makes sense given the prior line:

index = [list_rows[r],list_columns[c]]

Then you say your program crashes at the following line:

print sheet[index]  <------

But you said sheet has to be indexed by the constraints in Item 2,lets look at what you're doing:

index = [list_rows[r],list_columns[c]]
# For example, the first one is [0, 0]
# Currently, index = [0, 0]
print "%s" % index # Which prints index as a string
print sheet[index] # You're doing sheet[[0, 0]]
# Wait, you broke Item 2 constraints!

So, give the following a try:

  1. print sheet[index[0], [index[1]] or...
  2. print sheet[*index] as its a bit cleaner

Frankly you can reduce some code:

def Scroller(max_row,max_column):
  r = 0
  while r <= (len(list_rows)-1):
    c = 0
    while c <= (len(list_columns)-1):
      print sheet[list_rows[r],list_columns[c]]
      c += 1
    r += 1

Some reading on string formatting however:

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