Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中读取文本文件中的单词 我有这个文本文件: 我需要程序读取并发送输出,如下所示:_Python_File - Fatal编程技术网

如何在python中读取文本文件中的单词 我有这个文本文件: 我需要程序读取并发送输出,如下所示:

如何在python中读取文本文件中的单词 我有这个文本文件: 我需要程序读取并发送输出,如下所示:,python,file,Python,File,我应该如何在python中执行此操作?, 顺便说一句,它的python3.8有点明显,你可以试试这个: file1 = open('myfile.txt', 'r') Lines = file1.readlines() for line in Lines: print(line.replace(",","=")) 说明: 首先以“r”指示的读取模式打开文件 循环浏览文件中的所有行 使用split(',')分隔单词,它将第一个单词存储在 s和s1中的第二个 打印出来 关闭文件 像

我应该如何在python中执行此操作?, 顺便说一句,它的python3.8有点明显,你可以试试这个:

file1 = open('myfile.txt', 'r') 
Lines = file1.readlines() 

for line in Lines: 
    print(line.replace(",","="))
说明:

  • 首先以“r”指示的读取模式打开文件
  • 循环浏览文件中的所有行
  • 使用split(',')分隔单词,它将第一个单词存储在 s和s1中的第二个
  • 打印出来
  • 关闭文件
  • 像这样:

    with open('text.txt','r') as f: # Opens the file with the read format
        print(f.read().replace(', ',' = ')) # Prints out the contents, with each ', ' replaced with ' = '
    

    循环浏览文件并使用
    str.replace
    函数我们可以知道您尝试了什么。很明显,如果不显示代码,很难找出您遇到了什么问题。很明显,人们不会为您编写代码,谢谢。@D.Seah:是的,事实上我没有写任何其他东西,因为我只需要知道如何开始。。。
    file1 = open('myfile.txt', 'r') 
    Lines = file1.readlines() 
    
    for line in Lines: 
        print(line.replace(",","="))
    
    f = open("file.txt","r")
    for i in f:
         s,s1 = i.split(',')
         print(s+" = "+s1)
    f.close()
    
    with open('text.txt','r') as f: # Opens the file with the read format
        print(f.read().replace(', ',' = ')) # Prints out the contents, with each ', ' replaced with ' = '