0

I'm a new learner of Python and use python 2.7 in mac ox 10.8.3.

Today I met a problem that python don't get the right data when executing file reading.

my input file include two website url like this:

    www.google.com
    www.facebook.com

and python codes are below, just to print the input:

f = open("weblist.rtf","r")
print f.read()
f.close()

But after run, output is like this:

    {\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf370
    {\fonttbl\f0\fnil\fcharset134 STHeitiSC-Medium;}
    {\colortbl;\red255\green255\blue255;}
    \paperw11900\paperh16840\margl1440\margr1440\vieww12200\viewh12840\viewkind1
    \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural

   \f0\b\fs36 \cf0 www.google.com\
   www.facebook.com}

How to solve this problem? Anyone has suggestion?

1
  • 3
    It's not a read error. Your text file is an RTF file. That's what it looks like. Commented May 15, 2013 at 5:38

2 Answers 2

1

RTF files are not like simple text files (for example, windows .txt files), and there're specific headers for RTF files.

You may have a try on a simple text file instead of other kind of text files.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help. I changed it to a text file and solved the problem. Great Help
1

You cannot treat RTF files like normal text files and read them line-by-line.

You could look at the following link on stackoverflow which deals with converting RTF files to text files.

Is there a Python module for converting RTF to plain text?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.