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

  • SEARCH
  • Home
  • 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 8287427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:02:19+00:00 2026-06-08T12:02:19+00:00

In Python, I open a text file and read it line-by-line, and strip() the

  • 0

In Python, I open a text file and read it line-by-line, and strip() the line before evaluating it.
When I evaluate the line, I have an if statement to check if the line is “random”, and then puts a random number into a variable called genRandom. I have another line in my code that looks like this:

thisLine.replace("genRANDOM",genRandom) #Replace genRANDOM with the random number

On every line, it seems to work ok. In the input text file, I have a line that looks like this:

-genRANDOM

Whenever my script evaluates that line, I get this error:

Traceback (most recent call last):
  File "D:\Mass Storage\pythonscripts\TurnByTurn\execute.py", line 37, in <module>
    thisLine.replace("genRANDOM",genRandom) #Replace genRANDOM with the random number
TypeError: expected a character buffer object

How can I fix this? Thanks in advance!

  • 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-06-08T12:02:21+00:00Added an answer on June 8, 2026 at 12:02 pm

    You’re telling thisLine.replace to replace the string "genRANDOM" with the number genRandom. That doesn’t work, because it “expects a character buffer object” (such as a string), not a number.

    Try str(genRandom) instead, to turn it into a string: "4" instead of 4.

    Also, you might want to name the variable something else; genRandom sounds like a function that generates a random number to me, not a random number that’s been generated.

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

Sidebar

Related Questions

I have two columns in a text file. I read them into Python into
I am trying to open a text file in python. In the following code:
I have written the same program (open text file and display contents) in C
I'm using python to read a text file with the segment below (can't post
I'm working on Windows with Python 2.6.1. I have a Unicode UTF-16 text file
I have a small text file that I'd like to read into a scalar
I'm having trouble reading an entire specific line of a text file using Python.
I want to be able to use Python to open a .csv file like
The following code: with open(J:\\python\\.data) as data: self.data=pickle.load(data) generated the following error: File J:\python\code.py,
I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It

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.