【问题标题】:Problem when writing to a python and panda file写入 python 和 panda 文件时出现问题
【发布时间】:2021-04-30 00:43:41
【问题描述】:

图片中的代码是否正确? 执行时它不会打开任何东西。 代码应该复制第一个txt文件的内容并粘贴到第二个txt文件中,用“/t”分隔还是我错了?

infile = mainpath + "/" + "customer-churn-model/Customer Churn Model.txt"
outfile = mainpath + "/" + "customer-churn-model/Tab Customer Churn Model.txt"
  

  with open(infile,"r") as infile1:
    with open(outfile,"w") as outfile1:
        for line in infile1:
            fields = line.strip().split(",")
            outfile1.write("/t".join(fields))

【问题讨论】:

    标签: python text-files txt


    【解决方案1】:

    您没有正确读取输入文件。编辑你的循环就可以了。

    for line in infile1.readlines():
    

    【讨论】:

      【解决方案2】:

      试试这个:

      import pandas as pd
      
      df = pd.read_csv(infile)
      df.to_csv(outfile, sep="/t")
      

      【讨论】:

        猜你喜欢
        • 2014-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-12
        • 2016-08-22
        • 2014-01-28
        相关资源
        最近更新 更多