bri*_*nah 3 python csv dictionary count
所以我是python的新手,我正在尝试编写一个遍历目录中所有.txt文件的脚本,计算每个文件中的行数(除了空白或注释掉的行),然后写入最终输出到csv.最终输出应该如下所示:
agprices, avi, adp
132, 5, 8
Run Code Online (Sandbox Code Playgroud)
我在使用语法时遇到问题,将每个计数保存为字典的值.这是我的代码如下:
#!/usr/bin/env python
import csv
import copy
import os
import sys
#get current working dir, set count, and select file delimiter
d = os.getcwd()
count = 0
ext = '.txt'
#parses through files and saves to a dict
series_dict = {}
txt_files = [i for i in os.listdir(d) if os.path.splitext(i)[1] == ext]
#selects all files with .txt extension
for f in txt_files:
with open(os.path.join(d,f)) as file_obj:
series_dict[f] = file_obj.read()
if line.strip(): #Exclude blank lines
continue
else if line.startswith("#"): #Exclude commented lines
continue
else
count +=1
#Need to save count as val in dict here
#save the dictionary with key/val pairs to a csv
with open('seriescount.csv', 'wb') as f:
w = csv.DictWriter(f, series_dict.keys())
w.writeheader()
w.writerow(series_dict)
Run Code Online (Sandbox Code Playgroud)
所以这是编辑:
#!/usr/bin/env python
import csv
import copy
import os
import sys
import glob
#get current working dir, set count, and select file delimiter
os.chdir('/Users/Briana/Documents/Misc./PythonTest')
#parses through files and saves to a dict
series = {}
for fn in glob.glob('*.txt'):
with open(fn) as f:
series[fn] = (1 for line in f if line.strip() and not line.startswith('#'))
print series
#save the dictionary with key/val pairs to a csv
with open('seriescount.csv', 'wb') as f:
w = csv.DictWriter(f, series.keys())
sum(names.values())
Run Code Online (Sandbox Code Playgroud)
我在第二行到最后一行收到缩进错误,我不确定为什么?另外,我并不认为我在最后一部分正确编写语法.再一次,我只是想在{a:132,b:245,c:13}这样的文件中返回一个包含文件名和行数的字典.
你可以尝试这些方面:
os.chdir(ur_directory)
names={}
for fn in glob.glob('*.txt'):
with open(fn) as f:
names[fn]=sum(1 for line in f if line.strip() and not line.startswith('#'))
print names
Run Code Online (Sandbox Code Playgroud)
那将打印一个类似于的字典:
{'test_text.txt': 20, 'f1.txt': 3, 'lines.txt': 101, 'foo.txt': 6, 'dat.txt': 6, 'hello.txt': 1, 'f2.txt': 4, 'neglob.txt': 8, 'bar.txt': 6, 'test_reg.txt': 6, 'mission_sp.txt': 71, 'test_nums.txt': 8, 'test.txt': 7, '2591.txt': 8303}
Run Code Online (Sandbox Code Playgroud)
你可以使用那个Python dict csv.DictWriter.
如果你想要这些的总和,就这样做:
sum(names.values())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3030 次 |
| 最近记录: |