【问题标题】:Passing csv files as arguments to a function将 csv 文件作为参数传递给函数
【发布时间】:2018-08-28 04:55:57
【问题描述】:
  infile = open('abc.csv', "r")

  infile1 = open('xyz.csv', "r") 

  infile2 = open('pqr.csv', "r")

我正在尝试将 3 个 csv 文件读入 in、in1、in2。之后我必须将它们传递给一个函数。问题是当我试图直接传入 in1, in2 作为参数时,它的显示

ValueError: int() 以 10 为底的无效文字:

convert_to(infile, infile1, infile2)

def convert_to(..,..,..)

如何将 in1、in2 作为参数传入函数定义并调用?这是正确的方法,那么为什么它会显示此错误。还有其他更好更有效的方法吗?

【问题讨论】:

  • 显示一些代码。 mvce
  • 将 in = open('abc.csv', "r") 替换为 in_0 = open('abc.csv', "r")
  • 如果您向我们展示代码,我们可以为您提供更好的帮助。
  • 您正在以读取模式打开文件,但未读取 csv 的内容。一旦你做了 infile = open(...) 你应该读它 infile_contents = infile.read() 然后最后 infile.close()
  • 贴出相关代码和完整的回溯。

标签: python function file csv


【解决方案1】:

首先,您不能使用 in 作为变量名,因为它是 python 中的保留字:

import csv

file1 = open("abc.csv", "r")
file2 = open("xyz.csv", "r")
file3 = open("pqr.csv", "r")

def convert_to(a, b, c):
    ...

convert_to(file1, file2, file3)

另外,我不会这样做,因为我想确保文件在使用后关闭。见this。我将创建一个接受文件名作为参数的函数,然后处理函数内的文件:

import csv

filename1 = "abc.csv"
filename2 = "xyz.csv"
filename3 = "pqr.csv"

def convert_to(a, b, c):
    with open(a, "r") as file1:
        pass # do something with file abc.csv
    with open(b, "r") as file1:
        pass # do something with file xyz.csv
    with open(c, "r") as file1:
        pass # do something with file pqr.csv

convert_to(filename1, filename2, filename3)

【讨论】:

    【解决方案2】:

    只是一个建议,你为什么不使用pandas

    pandas 的情况下,很容易将它们作为对象传递给函数并做任何你想做的事情。

    import pandas as pd
    in1 = pd.read_csv('path/to/file1.csv')
    in2 = pd.read_csv('path/to/file2.csv')
    in3 = pd.read_csv('path/to/file3.csv')
    
    def convert_to(*args):
        for df in args:
            print "hi"
    
    convert_to(in1,in2,in3)
    

    让我知道 pandas 是否适合你。

    另一种解决方案:

    convert_to 中,您是否尝试将任何值转换为integer。在您的情况下,这可能是一个问题。将string 转换为integer 将抛出您在顶部提到的错误。 这是一个例子:

    print int('56.0000')
    print float('56.0000')
    

    你可以观察,

    ValueError: invalid literal for int() with base 10: '56.0000000'
    

    对于第一种情况和第二种情况,

    56.0
    

    我认为这是您遇到的问题。

    【讨论】:

    • ValueError: int() 基数为 10 的无效文字:'44.37248774328121'.. 如何解决此问题
    • float('44.37248774328121')然后如果真的想把它转换成整数然后做那个数字的int
    • 我需要转换为 int。我会试试这个!
    【解决方案3】:

    “in”是一个关键字。您应该更改该变量名称。

    不仅仅是变量名...您还需要更改参数。

    我做到了,对我来说效果很好。

    import csv
    
    in1 = open('abc.csv', "r")
    
    in2 = open('xyz.csv', "r")
    
    in3 = open('pqr.csv', "r")
    
    def convert_to(in1, in2, in3):
        return 'hi'
    
    print(convert_to(in1,in2,in3))
    

    【讨论】:

    • 我编辑了我的帖子以反映更改。试试看,然后告诉我你的想法。
    【解决方案4】:
      import csv
      csv_files = ['abc.csv','xyz.csv','abcd.csv']  
      def convert_to(files):
          for file in files:
              with open(file,'r') as f:
                  # Do something     
    

    您可以将文件列表传递给函数,然后根据需要进行更改/隐藏它们。

    【讨论】:

    • 如果我尝试传递多个文件,例如 ['abc1.csv','xyz1.csv','abcd1.csv','abc2.csv','xyz2.csv','abcd2. csv'] 我必须在第一次传递给函数时读取 ['abc1.csv','xyz1.csv','abcd1.csv'] 和 ['abc2.csv','xyz2.csv','abcd2. csv'] 在第二遍...我可以将 open 更改为 open("abc_%s.csv" % i, "r") 但是 for 循环呢.. 对多个文件的任何建议
    • 是的,您可以随意传递它们,但您需要拆分列表并将它们传递给函数并进行计算。 files = ['abc.csv','abcd.csv','xyz.csv'] 第一次调用:convert_to(files[:2]) 第二次调用:convert_to(files[2:]) 我的建议是使用 python pandas处理 CSV 和数据计算的东西,它有很多功能。如果您能用简单的话告诉我们确切的要求,那么这两种方式都会有很大的帮助。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-01-27
    • 2021-03-14
    • 2010-10-04
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多