如何将.txt打印到我的.py?

时间:2015-12-13 02:11:43

标签: python

如何修复它,我试图将.txt文件(字典)调用到我的.py,我试图拆分字典并用|调用它。

例如:

Please | help me 
Please : help me 

我的代码:

def load_library(file):
    with open(file,'rt') as x:
        mylist = x.split("|")
        for y in mylist:
            mod = getattr(x, y)
        return mod

1 个答案:

答案 0 :(得分:-1)

据我所知,这就是你想要做的事情。

def load_lib(file):
     allTheData = []
     with open(file,"r") as f:
          for line in f:
              data = line.split("|")
              ####DO something
              # data[1] is the stuff after |
              printAsYouWant(data)
              ## allTheData holds all the definitions
              allTheData.append(data[0] + ":" + data[1])
          printList(allTheData)

def printAsYouWant(data):
   print(data[0] + ":" + data[1])
####PRINT THE DATA
def printList(yourData):
    for i in yourData:
         print(i)
  

您可以修改printAsYouWant(data)以更改数据。

或者,作为一个功能。

def load_lib(file):
     allTheData = []
     with open(file,"r") as f:
          for line in f:
              data = line.split("|")
              ####DO something
              # data[1] is the stuff after |
              print(data[0] + ":" + data[1])
              allTheData.append(data[0] + ":" + data[1])
          for i in allTheData:
              print(i)

编辑:从我所看到的情况来看,这应该与您在问题中指定的一样。