Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x 如何在一个或多个csv文件的每行中计算指定字段中的字符串_Python 3.x_Linux_Parsing_Typeerror_Counting - Fatal编程技术网

Python 3.x 如何在一个或多个csv文件的每行中计算指定字段中的字符串

Python 3.x 如何在一个或多个csv文件的每行中计算指定字段中的字符串,python-3.x,linux,parsing,typeerror,counting,Python 3.x,Linux,Parsing,Typeerror,Counting,编写Python程序(3版),对一个或多个csv文件的每行中指定字段中的字符串进行计数 其中csv文件包含: Field1, Field2, Field3, Field4 A, B, C, D A, E, F, G Z, E, C, D Z, W, C, Q 执行脚本,例如: $ ./script.py 1,2,3,4 file.csv $ ./script.py 1,2,3,4 file.csv file.csv file.csv 结果是: A 10 C 7 D

编写Python程序(3版),对一个或多个csv文件的每行中指定字段中的字符串进行计数

其中csv文件包含:

Field1, Field2, Field3, Field4
A, B, C, D
A, E, F, G
Z, E, C, D
Z, W, C, Q
执行脚本,例如:

$ ./script.py 1,2,3,4 file.csv
$ ./script.py 1,2,3,4 file.csv file.csv file.csv
结果是:

A       10
C       7
D       2
E       2
Z       2
B       1
Q       1
F       1
G       1
W       1
错误 执行脚本,例如:

$ ./script.py 1,2,3,4 file.csv
$ ./script.py 1,2,3,4 file.csv file.csv file.csv
发生错误的地方:

for rowitem in reader:
    for pos in field:
        pos = rowitem[pos] ##<---LINE generating error--->##

        if pos not in fieldcnt:
            fieldcnt[pos] = 1

        else:
            fieldcnt[pos] += 1
读取器中的行项目的
:
对于字段中的pos:
pos=行项目[pos]####
如果pos不在现场CNT中:
fieldcnt[pos]=1
其他:
fieldcnt[pos]+=1
TypeError:列表索引必须是整数或片,而不是str


谢谢大家!

从输出判断,csv文件中的字段不会影响字符串的计数。如果字符串的唯一性不区分大小写,请记住使用
yourstring.lower()
返回字符串,以便实际将不同大小写的匹配计算为一个。还请记住,如果您的文本很大,您可能会发现的唯一字符串的数量也可能很大,因此必须进行某种排序才能理解它!(或者它可能是一个很长的随机计数列表,其中很大一部分仅为1)

现在,使用
collections
模块获取唯一字符串的计数是一种简单的方法

file = open('yourfile.txt', encoding="utf8")
a= file.read()

#if you have some words you'd like to exclude
stopwords = set(line.strip() for line in open('stopwords.txt')) 
stopwords = stopwords.union(set(['<media','omitted>','it\'s','two','said']))
# make an empty key-value dict to contain matched words and their counts
wordcount = {}
for word in a.lower().split(): #use the delimiter you want (a comma I think?)
    # replace punctuation so they arent counted as part of a word
    word = word.replace(".","")
    word = word.replace(",","")
    word = word.replace("\"","")
    word = word.replace("!","")
    if word not in stopwords:
        if word not in wordcount:
            wordcount[word] = 1
        else:
            wordcount[word] += 1
我希望这能解决你的问题。让我知道你是否面临问题