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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:39:02+00:00 2026-05-24T06:39:02+00:00

I am using python’s CSV module to iterate over the rows of a column.

  • 0

I am using python’s CSV module to iterate over the rows of a column.

What I need to do is:

  1. Get the first row for column “title”
  2. Remove any spanish characters (accents, Ñ)
  3. Remove single quotes
  4. Finally, replace spaces with dashes and convert everything to lowercase.

I got this to work with a simple test file,not a csv. I also managed to print each title in it’s own separate line.

But now I’m using this code to go over the CSV file (sorry for the VERY ugly code, I’m a newbie programmer):

import csv
import unicodedata
import ast

def strip_accents(s):
  return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))

dic_read = csv.DictReader(open("output.csv", encoding = "utf8"))

for line in dic_read:

    #print(line)     #I get each line of the csv file as a dictionary.
    #print(line["title"])  # I get only the "title" column on each line

    line = line.replace(' ', '-').lower()
    line = line.replace("´", "")
    line = strip_accents(line)
    fp=open("cleantitles.txt", "a")
    fp.write(line)
    fp.close()

I get the following error:

Traceback (most recent call last):
  File "C:/csvreader3.py", line 15, in <module> line = strip_accents(line)
 File "C:/csvreader3.py", line 7, in strip_accents
  return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
  TypeError: must be str, not dict

I also get a similar error when I try to do a .replace only. I understand now that these methods only apply to strings.

How can I get this to work? I searched around for a way to convert a dict to a string object but that didn’t work.

Also, any criticism to optimize and make my code more readable are also welcome!

  • 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-24T06:39:03+00:00Added an answer on May 24, 2026 at 6:39 am

    With the new information at hand, I think you might find this method to be simpler.

    Use the inbuilt function ‘map’. I’ll leave the explanation of what ‘map’ does to the python documentation.

    Here is what I think you should do

    Create a function that takes a line/dict and processes it to the format you want

    def strip_unwanted(line):
        title = str(line['title']).replace(' ', '-').replace("´", "")
        title = ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
        line['title'] = title
        return line
    
    with open("output.csv", encoding = "utf8") as input:
        dic_entries = csv.DictReader(input)
        # use the 'map' function
        new_entries = map(strip_unwanted, dic_entries)
    
        with open('some.csv', 'wb') as output:
            writer = csv.DictWriter(output)
            writer.writerows(new_entries)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python to append CSV file, I get data every other row. How do
Using Python module re, how to get the equivalent of the \w (which matches
Using python and wsgiref.handlers, I can get a single variable from a form with
I'm using Python's unittest module for unit testing. I'd like to be able to
Using python's optparse module I would like to add extra example lines below the
Using Python 2.5.2 and Linux Debian, I'm trying to get the content from a
Using Python regular expressions how can you get a True / False returned? All
Using python in an interactive mode one imports a module then if the module
Using Python, am finding it difficult to get filter() to work with lambda for
Using Python, is it possible to get access to the Most Recently Used (MRU)

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.