Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.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 将csv文件作为参数传递给函数_Python_Function_File_Csv - Fatal编程技术网

Python 将csv文件作为参数传递给函数

Python 将csv文件作为参数传递给函数,python,function,file,csv,Python,Function,File,Csv,我正在尝试将3个csv文件读入in、in1、in2。之后,我必须将它们传递给函数。问题是当我试图直接传入,in1,in2作为参数时 ValueError:基数为10的int的文本无效: 如何将in1、in2作为参数传递给函数定义和调用?这是正确的方法吗?那么为什么它会显示这个错误。还有其他更好更有效的方法吗?in是一个关键字。您应该更改该变量名 不仅仅是变量名。。。您还需要更改参数 我做到了,对我来说效果很好 convert_to(infile, infile1, infile2) def c

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


ValueError:基数为10的int的文本无效:


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

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

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

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

convert_to(infile, infile1, infile2)

def convert_to(..,..,..)
您可以将文件列表传递给函数,然后根据需要进行更改/转换。

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

  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     
另外,我不会这样做,因为我想确保文件在使用后关闭。看见我将创建一个函数,该函数接受文件名作为参数,然后在函数中处理文件:

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)

只是一个建议,你为什么不用熊猫呢

对于pandas,很容易将它们作为对象传递给函数,并对其执行任何操作

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)
让我知道熊猫是否适合你

另一个解决方案: 在convert_to中,您正在尝试将任何值转换为整数。这可能是你的问题。将字符串转换为整数将抛出您在顶部提到的错误。 以下是一个例子:

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)
你可以观察到

print int('56.0000')
print float('56.0000')
对于第一种情况和第二种情况

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

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

显示一些代码。替换in=open'abc.csv',r to in_0=open'abc.csv',如果您向我们展示代码,我们可以更好地帮助您。您以读取模式打开文件,但不读取csv的内容。一旦你做了填充=打开。。。你应该先读它infle_contents=infle.read,然后最后infle.close发布相关代码和完整的回溯。我编辑了我的文章以反映这些变化。试试看,让我知道你的想法。如果我试图在第一次传递函数时传递多个文件,比如['abc1.csv'、'xyz1.csv'、'abcd1.csv'、'abc2.csv'、'xyz2.csv'、'abcd2.csv'],我必须在第二次传递函数时读取['abc1.csv'、'xyz1.csv'、'abcd1.csv']。。。我可以将open更改为openabc_u%s.csv%i,r,但是for循环呢。。对多个文件的任何建议是的,您可以根据需要传递它们,但是您需要拆分列表并将它们传递给函数并进行计算。files=['abc.csv','abcd.csv','xyz.csv']1st call:convert_-tofiles[:2]2nd call:convert_-tofiles[2:]我的建议是使用python pandas处理csv和数据计算,它有许多功能。如果你能用简单的话告诉我们确切的需求,这两方面都会有很大的帮助。ThanksValueError:基数为10的int的文本无效:“44.37248774328121”。。如何修复这个do float'44.37248774328121',如果真的想把它转换成整数,那么这个数字的int需要转换成int。我会试试这个!
print int('56.0000')
print float('56.0000')
ValueError: invalid literal for int() with base 10: '56.0000000'
56.0