-3

I am parsing an Excel spreadsheet with this code:

for row_idx in range(2, db_sheet.nrows):
        press_id = str(db_sheet.cell(row_idx, 0).value)
            press_name = 'Inj ' + press_id[3]
            try:
                press_name = 'Inj ' + press_id[3] + press_id[4]
            except:
                pass

In the spreadsheet the names will be from 'INJ1' to 'INJ10'. How to write this code correctly, so I don't have to leave empty except?

Viktor
  • 325
  • 2
  • 10
  • It's not very clear what you want to achieve there. What can be in `press_id`? What exactly is your problem - where you need to check if string part exists? – Adam Tokarski May 24 '21 at 12:49
  • well either test before use or catch if error - but catch something meaningful - not everything. catching meaninfull is ok and fits to pythons [ask-forgiveness-not-permission-explain](https://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain) – Patrick Artner May 24 '21 at 12:49
  • How does the spreadsheet look like? Use `re.split()[0]` to get the first cell of each row. – Sim Son May 24 '21 at 12:49
  • btw: `IndentationException` and not a [mre] – Patrick Artner May 24 '21 at 12:51

1 Answers1

0

Trying to answer blindly because of the lack of details, but it looks like you need string slicing: press_name = 'Inj ' + press_id[3:]

westandskif
  • 972
  • 6
  • 9