1

I'm trying to create a table of basically a list of words. I prefer the table format as eventually it's going to go into excel. However, with my code i keep getting an introduction of the letter 'b' before my string. Any help would be appreciated.

I even tried printing at various stages to see where and why they get introduced. Code:

from xlwt import Workbook
import numpy as np

wb = Workbook()
Number_Of_Days = int(input('How many days do you want?'))
All_Sheets = np.chararray((Number_Of_Days,1))
All_Sheets = np.chararray(All_Sheets.shape, itemsize = 10)
All_Sheets[:] = ''

print(All_Sheets)

count = 0
while count < Number_Of_Days:
    Sheet_Name = input(print('Enter the name of day', count+1))
    All_Sheets [count,0] = Sheet_Name
    count = count + 1

print(All_Sheets)

Code and output

Barmar
  • 741,623
  • 53
  • 500
  • 612
newbieintown
  • 33
  • 1
  • 4
  • This indicates that you are looking at a byte literal, not a string: https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal – Jeremy Aug 08 '17 at 16:29
  • @Jeremy Thank you for your quick response. Is there anyway i can make my input a string and note a byte literal in that case? And will excel show the b '...' when i do put it in? Or will it be ignored? – newbieintown Aug 08 '17 at 16:35
  • This will help with conversion: https://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string As for what will happen with Excel, I'm honestly not sure. – Jeremy Aug 08 '17 at 16:38

1 Answers1

2

Note the chararray documentation:

The chararray class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of dtype object_, string_ or unicode_, and use the free functions in the numpy.char module for fast vectorized string operations.

In [191]: np.chararray((2,1))
Out[191]: 
chararray([[''],
           [b'\x01']],
          dtype='|S1')

The '|S1' dtype means a one character bytestring. This is the default type in Py2, but in Py3 it is flagged with the b character.

A better way of initializing a character array is:

In [192]: np.zeros((2,1), dtype='U10')
Out[192]: 
array([[''],
       ['']],
      dtype='<U10')

In [194]: np.zeros((2,1), dtype='S10')
Out[194]: 
array([[b''],
       [b'']],
      dtype='|S10')

or even better with a list of strings:

In [195]: np.array(['one','two','three'])
Out[195]: 
array(['one', 'two', 'three'],
      dtype='<U5')
hpaulj
  • 221,503
  • 14
  • 230
  • 353