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 6167067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:26:54+00:00 2026-05-23T22:26:54+00:00

I know mixing text and binary is awful, but I have to do this.

  • 0

I know mixing text and binary is awful, but I have to do this.

I want to replace the binary content, which is around with “Content-Type: image” and “—-“, by string “XXXXXXXX”

So the code for test is:

# coding=utf-8
import re
raw_data = open('r_img.txt').read()
#data = re.sub(r"Content-Type: image.*?-----","Content-Type: imageXXXXXXX-----", raw_data, re.S)
data = re.sub(r"Content-Type: image[^-]*-----","Content-Type: imageXXXXXXX-----", raw_data, re.S)
print data

And the file r_img.txt would be:

Content-Disposition: form-data; name="commodity_pic1"; filename="C:\Documents and Settings\tim\My Documents\My Pictures\Pic\222A8888.jpg"

Content-Type: image/pjpeg



EEE? JFIF  H H  EEE C 

EEE C       

 EEEWhfEEE[e?EEEEEEqEEEEEEEEEEEEEEEZIOEEE(r5?-iEEEEEEEEEEEEEEE?EEE?EEEEEE
-----------------------------7db27132d0198

I had try string.replace() and re.sub, but I can’t still find the answer.

  • 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-23T22:26:54+00:00Added an answer on May 23, 2026 at 10:26 pm

    This works for me:

    data = re.sub(r"Content-Type: image.*-----","Content-Type: imageXXXXXXX-----", 
                  raw_data, 0, re.DOTALL)
    

    Essentially it matches in a greedy way all characters between Content-Type: image and -----. The 0 means “match all occurrences of this pattern”. Probably this is superfluous for you, but you can’t skip it as you also wanted to use the flag re.DOTALL that modify the meaning of “any characters” to also include newlines.

    HTH!

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

Sidebar

Related Questions

I know this might be a no-brainer, but please read on. I also know
I know, I would have thought the answer was obviously no as well, but
I want to convert function object to function. I wrote this code, but it
Know of any good libraries for this? I did some searches and didn't come
I know there is a registry key indicating the install directory, but I don't
I know I must be missing something, but in a while statement how does
I'm trying to count how many essays have been graded so I know how
I have a single columned datatable inside a single tabled dataset. I just want
We have built a Windows Service that is running on client's machines, which occasionally
I know its probably possible, but is it practical and doable to try and

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.