Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 7th, 2013, 12:32 AM
CastorTroy CastorTroy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 CastorTroy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 5 sec
Reputation Power: 0
Python is Reading My .txt File Incorrectly

I have a file in a .txt format with just a bunch of words, in a list like layout. When I open and read the file in python, it gives me all the margins, columns and format as the words. Like this:

word is {\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf340
word is {\fonttbl\f0\fswiss\fcharset0 Helvetica;}
word is {\colortbl;\red255\green255\blue255;}
word is \margl1440\margr1440\vieww10800\viewh8400\viewkind0
word is \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatur al
word is

it will also read out the words that are actually in the list with a "\" right after.

I've tried .rtf and .doc also. The .rtf does the same thing and it won't even open the .doc file format.

Any clues?
Any help will be much appreciated,,,thanks

Reply With Quote
  #2  
Old March 7th, 2013, 01:52 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 37 sec
Reputation Power: 1
Quote:
Originally Posted by CastorTroy
I have a file in a .txt format with just a bunch of words, in a list like layout. When I open and read the file in python, it gives me all the margins, columns and format as the words. Like this:

word is {\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf340
word is {\fonttbl\f0\fswiss\fcharset0 Helvetica;}
word is {\colortbl;\red255\green255\blue255;}
word is \margl1440\margr1440\vieww10800\viewh8400\viewkind0
word is \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatur al
word is

it will also read out the words that are actually in the list with a "\" right after.

I've tried .rtf and .doc also. The .rtf does the same thing and it won't even open the .doc file format.

Any clues?
Any help will be much appreciated,,,thanks


Please post sample input data, your python code, and expected output data. With your explanation above it's impossible to help. Also state your operating system and python version.

Reply With Quote
  #3  
Old March 7th, 2013, 11:03 AM
CastorTroy CastorTroy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 CastorTroy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by partoj
Please post sample input data, your python code, and expected output data. With your explanation above it's impossible to help. Also state your operating system and python version.


I'm using OS X ML and version 3.3. The code I wrote has a lot of flaws and needs to be completely redone. But I have a .txt file with over 100,000 words that I pulled off the internet so debugging takes a long time. That is why I created my own .txt file with about twenty words in it.

Maybe you can point me in the right direction. I'm trying to take a list of words and find the pairs that are exactly the same spelled backwards and put them into a list of its own. I tried using recursion, but that does not work unless I am doing something wrong.

Any ideas?

I'm kind of new to this and teaching myself through books and online tutorials so the simpler the better for now.

Thanks

Reply With Quote
  #4  
Old March 7th, 2013, 11:51 AM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 291 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 18 h 33 m 54 sec
Reputation Power: 7
Quote:
I've tried .rtf and .doc also. The .rtf does the same thing and it won't even open the .doc file format.
You have to save it as a text file. The crap you are getting when you read the file (color, font, etc.) is formatting info for the type of file it is. A text file does not have any of this. Something like the following will create a text output file that can be used for testing, as long as you don't view and (auto)save it from something else. Note that you will still have to strip() the newline
Code:
words_list = ["cat", "dog", "horse", "goat", "parrot"]

with open("./test_words.txt", "w") as output:
    for word in words_list:
        output.write("%s\n" % (word)) 

Last edited by dwblas : March 7th, 2013 at 12:05 PM.

Reply With Quote
  #5  
Old March 7th, 2013, 11:51 AM
Marbelous Marbelous is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 Marbelous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 41 m 11 sec
Reputation Power: 0
You still need to post your input file or we can't tell you much. Obviously it's not just text since the output contains formating data. Whatever Mac text editor you're using to create the file must be adding it. Is there a simple "Notepad" style text editor for Mac you can try?

Reply With Quote
  #6  
Old March 7th, 2013, 04:31 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
If you can make a list of the words,
Code:
def palindrome(a):
    return list(a)==list(reversed(a))

set((a for a in words if palindrome(a)))
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python is Reading My .txt File Incorrectly

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap