2017-04-23 53 views
-4

我正在制作一个简单的脚本,用于计算从Google表单生成的csv文件中的某些值。Python:从命令行参数中读取文件名

该脚本如下:

import csv 
import os.path 

fileName=None 

if __name__=="__main__": 
    try: 
     fileName=argv[1] 
     isFile(fileName) 
     pass 
    except Exception as e: 
     print("You must provide a valid filename as parameter") 
     raise 

def isFile(fileName): 
    if(not os.path.isfile(fileName)): 
     raise ValueError("You must provide a valid filename as parameter") 

print fileName 


def readCsvAndCountPercentPerFormItemFromGoogleForms(fileName): 
    times={} 
    totalRows=0 
    with open(fileName,'r') as csvfile: 

     csvReader=csv.reader(csvfile); 

     for row in csvreader: 

      value=row[1] 

      if(value in times.values()): 
       times[value]+=1 
      else: 
       times[value]=1 

      totalRows+=1 

     return calculateDictionaryAsPercent(times,totalRows) 

def calculateDictionaryAsPercent(times,totalRows): 

    if(totalRows==0): 
     raise ValueError("The file does not contain any rows") 

    for key,val in times.items(): 
      times[key]=(val/totalRows)*100 

    return times 


finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName) 

print finalTimes 

,但我得到了以下错误:

Traceback (most recent call last): 
    File "csv.py", line 1, in <module> 
    import csv 
    File "/home/pcmagas/Kwdikas/python/csv.py", line 54, in <module> 
    finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName) 
    File "/home/pcmagas/Kwdikas/python/csv.py", line 26, in readCsvAndCountPercentPerFormItemFromGoogleForms 
    with open(fileName,'r') as csvfile: 
TypeError: coercing to Unicode: need string or buffer, NoneType found 

的问题是,由于某种原因,可变fileName不会更改值:

if __name__=="__main__": 
    try: 
     fileName=argv[1] 
     isFile(fileName) 
     pass 
    except Exception as e: 
     print("You must provide a valid filename as parameter") 
     raise 

下面这段代码是基准吨在找到的第一个答案

所以,你可以给我一个解决方案,因为我不经常写python,所以我做了以下脚本,以便更好地学习它。

编辑1

的代码已经被重新格式化,以这样的:

import csv 
from sys import argv 
import os.path 


def isFile(fileName): 
    if(not os.path.isfile(fileName)): 
     raise ValueError("You must provide a valid filename as parameter") 


def readCsvAndCountPercentPerFormItemFromGoogleForms(fileName): 
    times={} 
    totalRows=0 
    with open(fileName,'r') as csvfile: 

     csvReader=csv.reader(csvfile); 

     for row in csvreader: 

      value=row[1] 

      if(value in times.values()): 
       times[value]+=1 
      else: 
       times[value]=1 

      totalRows+=1 

     return calculateDictionaryAsPercent(times,totalRows) 

def calculateDictionaryAsPercent(times,totalRows): 

    if(totalRows==0): 
     raise ValueError("The file does not contain any rows") 

    for key,val in times.items(): 
      times[key]=(val/totalRows)*100 

    return times 


fileName=None 

if __name__=="__main__": 
    try: 
     fileName=argv[1] 
     print("Filename: ",fileName) 
     isFile(fileName) 
     pass 
    except Exception as e: 
     print("You must provide a valid filename as parameter") 
     raise 

print("Filename: ",fileName) 
finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName) 

print finalTimes 

还是同样的错误。

+1

你必须'def isFile(fileName):'__before__你试着调用它。 –

+1

看来你的文件被命名为'csv.py'。您不能在名为'csv.py'的文件中导入csv';它只会导入自己。为您的文件选择一个不同的名称。 –

回答

-1

最后提到脚本应该是这个一个:

import csv 
from sys import argv 
import os.path 


def readCsvAndCountPercentPerFormItemFromGoogleForms(fileName): 
    times={} 
    totalRows=0 
    with open(fileName,'r') as csvfile: 

     csvReader=csv.reader(csvfile); 

     for row in csvReader: 

      value=row[1] 

      if(value in times.values()): 
       times[value]+=1 
      else: 
       times[value]=1 

      totalRows+=1 

     return calculateDictionaryAsPercent(times,totalRows) 

def calculateDictionaryAsPercent(times,totalRows): 

    if(totalRows==0): 
     raise ValueError("The file does not contain any rows") 

    for key,val in times.items(): 
      times[key]=(val/totalRows)*100 

    return times 


def isFile(fileName): 
    if(not os.path.isfile(fileName)): 
     raise ValueError("You must provide a valid filename as parameter") 

fileName=None 

if __name__=="__main__": 
    try: 
     fileName=argv[1] 
     isFile(fileName) 
     pass 
    except Exception as e: 
     print("You must provide a valid filename as parameter") 
     raise 

finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName) 

print finalTimes 
-1

你有这样一行:

fileName=argv[1] 

当它应该是:

fileName=sys.argv[1] 

更何况你还没有进口sys

另请注意,我更喜欢​​

+0

如果您打算投票,请留下原因,以便我可以A)修改答案,或B)如果我误解了,请将其删除。这有助于网站上的每个人。谢谢 –

+0

那么我改变了它,仍然是相同的错误 –

+0

为什么你在开始时设置'filename = None',你也可以正确地格式化你的代码,为什么你的主要功能高于你所有的其他功能。 –

相关问题