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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:22:36+00:00 2026-05-17T16:22:36+00:00

Related to a previous question , I’m trying to do replacements over a number

  • 0

Related to a previous question, I’m trying to do replacements over a number of large CSV files.

The column order (and contents) change between files, but for each file there are about 10 columns that I want and can identify by the column header names. I also have 1-2 dictionaries for each column I want. So for the columns I want, I want to use only the correct dictionaries and want to implement them sequentially.

An example of how I’ve tried to solve this:

# -*- coding: utf-8 -*-
import re

# imaginary csv file. pretend that we do not know the column order.
Header = [u'col1', u'col2']
Line1 = [u'A',u'X']
Line2 = [u'B',u'Y']
fileLines = [Line1,Line2]

# dicts to translate lines
D1a = {u'A':u'a'}
D1b = {u'B':u'b'}
D2 = {u'X':u'x',u'Y':u'y'}

# dict to correspond header names with the correct dictionary.
# i would like the dictionaries to be read sequentially in col1.
refD = {u'col1':[D1a,D1b],u'col2':[D2]}

# clunky replace function
def freplace(str, dict):
    rc = re.compile('|'.join(re.escape(k) for k in dict))
    def trans(m):
        return dict[m.group(0)]
    return rc.sub(trans, str)

# get correspondence between dictionary and column
C = []
for i in range(len(Header)):
    if Header[i] in refD:
        C.append([refD[Header[i]],i])

# loop through lines and make replacements
for line in fileLines:
    for i in range(len(line)):
        for j in range(len(C)):
            if C[j][1] == i:
                for dict in C[j][0]:
                    line[i] = freplace(line[i], dict)

My problem is that this code is quite slow, and I can’t figure out how to speed it up. I’m a beginner, and my guess was that my freplace function is largely what is slowing things down, because it has to compile for each column in each row. I would like to take the line rc = re.compile('|'.join(re.escape(k) for k in dict)) out of that function, but don’t know how to do that and still preserve what the rest of my code is doing.

  • 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-17T16:22:37+00:00Added an answer on May 17, 2026 at 4:22 pm

    You don’t need re:

    # -*- coding: utf-8 -*-
    
    # imaginary csv file. pretend that we do not know the column order.
    Header = [u'col1', u'col2']
    Line1 = [u'A',u'X']
    Line2 = [u'B',u'Y']
    fileLines = [Line1,Line2]
    
    # dicts to translate lines
    D1a = {u'A':u'a'}
    D1b = {u'B':u'b'}
    D2 = {u'X':u'x',u'Y':u'y'}
    
    # dict to correspond header names with the correct dictionary
    refD = {u'col1':[D1a,D1b],u'col2':[D2]}
    
    # now let's have some fun...
    
    for line in fileLines:
        for i, (param, word) in enumerate(zip(Header, line)):
            for minitranslator in refD[param]:
                if word in minitranslator:
                    line[i] = minitranslator[word]
    

    returns:

    [[u'a', u'x'], [u'b', u'y']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Related to my previous question: Preventing Memory issues when handling large amounts of text
This is related to a previous question . What I'm trying to understand now
this is related to my previous question. I'm trying to build Phone application from
This is related to my previous question . I'm trying, in Excel, to update
My question is related to a previous question in the forum - Number of
Related to my previous question , I have a string on the following format:
This is related to my previous question I'm solving UVA's Edit Step Ladders and
This question is related to a previous question of mine That's my current code
This is related to my previous question More than 1 Left joins in MSAccess
Very much related to my previous question , but I found this to be

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.