Python: Removing punctuation from read-in text using translate method

I'm trying to open and read a file, split the contents into words, and then iterate through them and remove all the punctuation. Why is this returning a TypeError: expected a character buffer object and how can I fix it?

with open(self.filename, "r") as file_opened: text = file_opened.read() words_with_punct = text.split() remove = dict.fromkeys(map(ord, '\n' + string.punctuation)) for i in words_with_punct: words = i.translate(remove) return words

--------------Solutions-------------

text = file_opened.read().translate(None,string.punctuation)

should work fine not sure what all the other stuff your doing is supposed to do

Category:python Time:2018-11-07 Views:0
Tags: python

Related post

  • Remove instances from a list by using LINQ or Lambda? 2009-07-26

    Now I come a stage to get all my data as a list in cache(objects) and my next thing I have to do is to remove some instances from the list. Normally, I would do removing like this: List<T> list; List<T2> toBeRemovedItems; // populate two

  • SQL Server: How do you remove punctuation from a field? 2009-11-30

    Any one know a good way to remove punctuation from a field in SQL Server? I'm thinking UPDATE tblMyTable SET FieldName = REPLACE(REPLACE(REPLACE(FieldName,',',''),'.',''),'''' ,'') but it seems a bit tedious when I intend on removing a large number o

  • Facebook removes parameters from url that I used to sendusing facebook send button 2011-11-10

    Facebook removes parameters from url that I used to send using facebook send button. I have tested my url http://www.logixsolution.info/grouppay/gift/?og_id=1 in http://developers.facebook.com/tools/debug there also this same problem exists. I have c

  • Remove duplicates from two large text files using unordered_map 2011-06-13

    I am new to a lot of these C++ libraries, so please forgive me if my questions comes across as naive. I have two large text files, about 160 MB each (about 700000 lines each). I need to remove from file2 all of the duplicate lines that appear in file

  • How to remove punctuation from a String in C 2009-12-03

    I'm looking to remove all punctuation from a string and make all uppercase letters lower case in C, any suggestions? --------------Solutions------------- Just a sketch of an algorithm using functions provided by ctype.h: #include <ctype.h> void

  • Python Removing Letters From a String 2011-11-19

    This is a homework assignment. I just need a nudge. I'm trying to create a loop in which the letters of a string will be removed when it is beyond a certain number. Example: Enter Your String: David Enter Your Amount: 4 Returns: Davi What I've made s

  • Removing punctuation from words that occur in a string 2012-02-06

    The code below works great. It creates a list of words occurring in the string $commentstring, ranked by number of occurrences. It omits numbers and anything in the $blacklist. It treats words ending in punctuation as unique. So american, american.,

  • Reading the text using regular expressions 2010-06-17

    I want to extract all values from the text line below using regular expressions: (Sales (Type 0 0 0 0 000 0 0 0 0) "Product" "ProductType" "" "0000" "0000") ;;ProductName 000 How to write regular expression for this? am using C#. am using the followi

  • Remove LI from UL on click using jQuery 2010-07-14

    So I have a INPUT TEXT that upon keypress=enter prepends an LI to an UL The LI contains a LINK that gives the user the option to remove it from the UL HTML (on page load) <ul id="conditionList"></ul> after ther user inputs some conditions

  • Remove style from selected span/text in div 2010-08-01

    Consider this snippet: <div> <span style="color:red;">a</span> <span style="color:blue;">a</span> <span style="color:white;">a</span> </div> How can you remove style from selected by user text? Edited:

  • Remove punctuations from NSMutableArray 2011-05-26

    How to remove punctuations like "," from an NSMutableArray? --------------Solutions------------- There are no commas in your array or your array elements. When you use NSLog() to log an array, it uses the NeXTSTEP format, which states that parenthese

  • Need Help Removing Entries from a Large Text File Based on the Contents of Another Text File 2011-10-18

    Good day. I could really use your help on this one. I have a stats text file in the following format. ID=1000000 Name=Name1 Field1=Value1 ...(Fields 2 to 25) Field26=Value26 ID=1000001 Name=Name2 Field1=Value1 ...(Fields 2 to 25) Field26=Value26 ID=1

  • PHP preg_replace: remove punctuation from beginning and end of string 2011-11-27

    What regular expression can I use in PHP to remove all punctuation from the beginning and end of a string? --------------Solutions------------- I wouldn't use a regex, probably something like... $str = trim($str, '"\''); Where the second argument is

  • how do I remove fbdownloader from internet explorer 10 using windows 8? 2012-02-06

    I am trying to find a procedure to remove fbdownloader for internet explorer 10 running with windows 8 --------------Solutions------------- Hi John, Thank you for posting your query in Microsoft Community Forums. From the issue description, I underst

  • Remove styles from internal style sheet using jQuery 2016-05-16

    <html> <head> <style> .containerTitle { background-color:silver; text-align:center; font-family:'Segoe UI'; font-size:18px; font-weight:bold; height:30px; } </style> </head> <body> <div id="a"/> </bod

  • prevent user from copying the text using jQuery? 2011-04-17

    how do i prevent the user from Copying any text in asp.net page using jQuery? --------------Solutions------------- You can disable the right click and also bind keyup event on document to detect copy command key combination "Ctl + C" and returning fa

  • Python, remove duplicates from list of tuples 2011-06-29

    I have the following list: [('mail', 167, datetime.datetime(2010, 9, 29)) , ('name', 1317, datetime.datetime(2011, 12, 12)), ('mail', 1045, datetime.datetime(2010, 8, 13)), ('name', 3, datetime.datetime(2011, 11, 3))] And I want to remove items from

  • Remove highlight added to selected text using JavaScript? 2011-11-10

    I highlighted selected text using JavaScript with the following code: var sel = window.getSelection(); if(!sel.isCollapsed) { var range = sel.getRangeAt(0); sel.removeAllRanges(); document.designMode = "on"; sel.addRange(range); document.execCommand(

  • Python removing items from list 2011-07-06

    I have a list in the given format: [['John', 'Smith'], ['Linus', 'Torvalds'], ['Bart', 'Simpson']] There are some elements like this in the list ['Linus Torvalds', ''] and I want to remove those. So why doesn't the following code remove them? for i i

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.819 (s). 13 q(s)