Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7704205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:38:01+00:00 2026-05-31T23:38:01+00:00

First of all, I am new to python/nltk so my apologies if the question

  • 0

First of all, I am new to python/nltk so my apologies if the question is too basic. I have a large file that I am trying to tokenize; I get memory errors.

One solution I’ve read about is to read the file one line at a time, which makes sense, however, when doing that, I get the error cannot concatenate 'str' and 'list' objects. I am not sure why that error is displayed since (after reading the file, I check its type and it is in fact a string.

I have tried to split the 7MB files into 4 smaller ones, and when running that, I get:
error: failed to write data to stream.

Finally, when trying a very small sample of the file (100KB or less), and running the modified code, I am able to tokenize the file.

Any insights into what’s happening? Thank you.

# tokenizing large file one line at a time
import nltk
filename=open("X:\MyFile.txt","r").read()
type(raw) #str
tokens = '' 
for line in filename
        tokens+=nltk.word_tokenize(filename)
#cannot concatenate 'str' and 'list' objects

The following works with small file:

import nltk
filename=open("X:\MyFile.txt","r").read()
type(raw)
tokens = nltk.word.tokenize(filename)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:38:02+00:00Added an answer on May 31, 2026 at 11:38 pm

    Problem n°1: You are iterating the file char by char like that. If you want to read every line efficiently simply open the file (don’t read it) and iterate over file.readlines() as follows.

    Problem n°2: The word_tokenize function returns a list of tokens, so you were trying to sum a str to a list of tokens. You first have to transform the list into a string and then you can sum it to another string. I’m going to use the join function to do that. Replace the comma in my code with the char you want to use as glue/separator.

    import nltk
    filename=open("X:\MyFile.txt","r")
    type(raw) #str
    tokens = '' 
    for line in filename.readlines():
        tokens+=",".join(nltk.word_tokenize(line))
    

    If instead you need the tokens in a list simply do:

    import nltk
    filename=open("X:\MyFile.txt","r")
    type(raw) #str
    tokens = []
    for line in filename.readlines():
        tokens+=nltk.word_tokenize(line)
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python and have been trying to figure this out all day.
Hi there First of all just let me say that I'm new in Python
first of all I'm totally new to FAST but I already have a couple
First of all I have to say I am new to iOS development. My
First of all, I'm fairly new to Java, so sorry if this question is
First of all I need to say that I'm new to jQuery. When I
I'm new to PHP first of all .. My question is once I got
I'm new to python, django and google app engine. All great tools and have
First of all I am new to python and a bit rusty on .NET
First of all I'm a python newbie. I'm playing with Django and I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.