Search code examples
pythongoogle-sheetsgspread

How to download plain-text from Google spreadsheet cell?


I'm using gspread module to login to a 1) Google spreadsheet 2) download a column of cells 3) print/save them to a text file.

I can use string filtering, but it removes some commas that I want to keep. So using string filters is not an option. The format gives plain-text when I use "val = worksheet.acell('B1').value". But it doesn't give me plain-text when I use "cell = worksheet.cell(1, value_render_option='FORMULA').value"

I'm using burnash gspread API to login and pull the data.

sheet = spreadsheet.open('Spreadsheet Name').worksheet('Worksheet Name')

col = sheet.col_values(1, value_render_option='FORMULA')

print str(col)

The problem is, it gives the values as ['[u\'Text in Row 1', u\'Text in Row 2', u\'Text in Row 3'] instead of "Text in Row 1Text in Row 2Text in Row 3" (which is what I want the output to show)


Solution

  • 'u' is the text representation of the corresponding Unicode string.

    Suppose u' occurs in a variable test, it could be removed by:

    test.encode("utf-8")

    Checkout This link for more ways.

    And for List like

    test_list = ['Text in Row 1','Text in Row 2','Text in Row 3']

    Run the following code:

    test_list = ['Text in Row 1','Text in Row 2','Text in Row 3']
    
    output_list = " ".join(str(x) for x in test_list)
    
    print(output_list)
    

    And the output would be :

    Text in Row 1 Text in Row 2 Text in Row 3