使用Python读取RTF文件

发布于 2025-01-30 05:28:36 字数 1032 浏览 4 评论 0原文

  • 使用striprtf rtf_to_text读取RTF文件
  • 无法读取URL,在代码中需要进行哪些更改?

输入 获取 [

> > 上获取最新新闻更新

所需的输出 获取 [emage  protinted]

python代码: - python代码: - python代码: -

import os
from striprtf.striprtf import rtf_to_text
import pandas as pd
from os import path

path_of_the_directory= r'C:\Users\Documents\filename.rtf'
print("Files and directories in a specified path:")
for filename in os.listdir(path_of_the_directory):
    f = os.path.join(path_of_the_directory,filename)
    
    if os.path.isfile(f):
      print(f)
      open_rtf_file=open(f,'r')
      file_content_read=open_rtf_file.read()
      text_content=rtf_to_text(file_content_read)
      print(text_content)
  • reading RTF file using striprtf
  • rtf_to_text not able to read URL,what changes need to make in the code?

Input
Get latest news update at [email protected]

Output
Get latest news update at

Desired Output
Get latest news update at [email protected]

python code:-

import os
from striprtf.striprtf import rtf_to_text
import pandas as pd
from os import path

path_of_the_directory= r'C:\Users\Documents\filename.rtf'
print("Files and directories in a specified path:")
for filename in os.listdir(path_of_the_directory):
    f = os.path.join(path_of_the_directory,filename)
    
    if os.path.isfile(f):
      print(f)
      open_rtf_file=open(f,'r')
      file_content_read=open_rtf_file.read()
      text_content=rtf_to_text(file_content_read)
      print(text_content)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下雨或天晴 2025-02-06 05:28:36

看起来您将文件视为目录。您的path_of_the_directory Varaible实际上是RTF文件名的路径。不知道您在运行时遇到的特定错误,在我看来是问题所在。修复它的一种简单方法是检查以确保在调用os.listdir之前的路径是目录,就像我在下面的示例中所做的那样。

path_of_the_directory= r'C:\Users\Documents\filename.rtf' #<--- this is  a file
print("Files and directories in a specified path:")
if os.path.isdir(filename):                              # check if path is directory
    for filename in os.listdir(path_of_the_directory):   
        f = os.path.join(path_of_the_directory,filename)
    
        if os.path.isfile(f):
            print(f)
            open_rtf_file=open(f,'r')
            file_content_read=open_rtf_file.read()
            text_content=rtf_to_text(file_content_read)
            print(text_content)

It looks like you are treating a file as a directory. your path_of_the_directory varaible is actually the path to a rtf file name. Without knowing what specific error you are getting at runtime, it looks to me like that is the problem. An easy way to fix it is to check to make sure the path is a directory prior to calling os.listdir like I do in the example below.

path_of_the_directory= r'C:\Users\Documents\filename.rtf' #<--- this is  a file
print("Files and directories in a specified path:")
if os.path.isdir(filename):                              # check if path is directory
    for filename in os.listdir(path_of_the_directory):   
        f = os.path.join(path_of_the_directory,filename)
    
        if os.path.isfile(f):
            print(f)
            open_rtf_file=open(f,'r')
            file_content_read=open_rtf_file.read()
            text_content=rtf_to_text(file_content_read)
            print(text_content)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文