-1

am having a QTableView and trying to get all values in the column. Am able to retrieve the values using the code below

data= []
for row in range(model.rowCount()):
    data.append([])
    for column in range(model.columnCount()):
        index = model.index(row, 0)
        data[row].append(str(model.data(index)))
for item in data:
    names = '{}'.format(item[0])
    print(names)

The problem am facing is that am getting the values as bytes for example

b'Austria'
b'Belgium'
b'Denmark'
b'England'
b'Finland'

instead of

Austria
Belgium
Denmark
England
Finland
Elsie 44
  • 31
  • 7
  • change `names = '{}'.format(item[0])` to `names = '{}'.format(item[0].decode())` – eyllanesc Mar 28 '18 at 06:52
  • Possible duplicate of [Convert bytes to a string?](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – eyllanesc Mar 28 '18 at 06:56
  • It's not a duplicate because i tried it and got the same error as am getting from using the code you've provided. The error is `AttributeError: 'str' object has no attribute 'decode'` – Elsie 44 Mar 28 '18 at 07:08
  • it's a duplicate, have you tried the answers? – eyllanesc Mar 28 '18 at 07:10

1 Answers1

1

Here is a working code to solve your problem

names = []
data = []
for row in range(model.rowCount()):
    name = model.data(model.index(row, 0))
    data.append((name))
    names.append(name)
for name in names:
    decoded_name = bytes(name).decode()
    print(decoded_name)
And3r50n 1
  • 337
  • 1
  • 5
  • 21