-1

I have a python file with the following code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xlrd
loc = ("/Users/arielkotch/Desktop/file/project/testFile.xlsx")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
for i in range(sheet.nrows):
    print(sheet.row_values(i))

The response I get is as follows.

I am new to python, I have tried stripping the whitespace, after cycling through each sub array, but it still appears to have white space.

trotta
  • 1,232
  • 1
  • 16
  • 23
  • 2
    Please provide what the input looks like and what you expect the output to look like after removing white space. – Nick Ellis Feb 19 '19 at 15:28
  • Possible duplicate of [Remove empty strings from a list of strings](https://stackoverflow.com/q/3845423/953482) – Kevin Feb 19 '19 at 15:29
  • Please don't post images of text. Instead, [edit] your question to include a representative (but short) sample, and the expected output. – tripleee Feb 19 '19 at 15:30
  • I don't think your output is wrong `l = [1,2,3,4,5,6,5]` `print(l)` `#[1, 2, 3, 4, 5, 6, 5]` that's just how list printing is formatted – lalam Feb 19 '19 at 15:34

1 Answers1

0

For each row, I'm assuming you want to remove excess whitespace but leave a single space where there were many. This can be achieved using the following:

for i in range(sheet.nrows):
    i = ' '.join(i.split())

This works by splitting on whitespace, then joining the split strings back together. For example:

test_string = 'hello        test'
test_string = ' '.join(test_string.split())
>>'hello test'
QuantumChris
  • 963
  • 10
  • 21