社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

有没有办法使用python中的函数反转文本文件中的行顺序?

Athar Jaffery • 3 年前 • 1196 次点击  
def encrypt():
  while True:
    try:
        userinp = input("Please enter the name of a file: ")
        file = open(f"{userinp}.txt", "r")
        break  
    except:
      print("That File Does Not Exist!")
  second = open("encoded.txt", "w")
  
  
  for line in file:
    reverse_word(line)




def reverse_word(line):
  data = line.read()
  data_1 = data[::-1]
  print(data_1)
  return data_1

encrypt()











我现在应该制作一个程序,以某种方式对文本文件进行加密,我尝试使用的一种方法是反转文本文件中的行序列。我的所有其他函数都已经使用了“for line in file”,其中“line”被带到每个单独的函数中,然后为了加密而更改,但是当我试图在这里做同样的事情来反转文件中的行的顺序时,我得到了一个错误

“str”对象没有“read”属性

我试着使用与下面相同的顺序,但不是继承文件,这是可行的,但我想让它在我从文件中继承单独的行时可以工作,就像我目前使用的其他函数一样(或者更简单地说,在for循环中有这个函数)。

有什么建议吗?谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132210
 
1196 次点击  
文章 [ 2 ]  |  最新文章 3 年前