简体   繁体   中英

HOW TO MAKE ATTRIBUTE FOR “PRINT” OUTPUT

I'm just new to python and even codings. This is my Python code:

>>> import xlrd
>>> file_name = "D:/Uber/reviews"
>>> workbook = xlrd.open_workbook(file_name)
>>> sheet = workbook.sheet_by_index(0)
>>> for row in range(sheet.nrows):
print(sheet.cell_value(row, 1)

I got the output of that "print" command. However, I don't know how to make a variable for that output (I need that variable for nltk tokenize)

Thank you for your help.

You can reuse-it directly in your class:

>>> import xlrd
>>> from nltk.tokenize import TweetTokenizer
>>> tknzr = TweetTokenizer()

>>> file_name = "D:/Uber/reviews"
>>> workbook = xlrd.open_workbook(file_name)
>>> sheet = workbook.sheet_by_index(0)
>>> for row in range(sheet.nrows):
      data =sheet.cell_value(row, 1) 
      print(data)
      tknzr.tokenize(data)

I think what you want is to assign the line:

sheet.cell_value(row, 1)

to a variable. For example:

my_row_value = sheet.cell_value(row, 1)

Then you can use my_row_value with nltk tokenize as you wish. Better yet, if you want this program to grow and use it after you have exited the python interpreter, the best thing will be to have your code in a script, for example:

myscript.py

And then you can simply run it using the python interpreter or if you are on linux simply:

python myscript.py

Let me know if I was of help.

I am not sure what you are looking for, but if you want to capture the output of print, just do

for row in range(sheet.nrows):
    string = str(sheet.cell_value(row, 1))
    print (string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM