import csv
import sys
writer = csv.writer(sys.stdout)
firstfile = True
for file in sys.argv[1:]:
with open(file, 'r') as rawfile:
reader = csv.reader(rawfile)
for idx, row in enumerate(reader):
# enumerate() is zero-based by default; 0 is first line
if idx == 0 and not firstfile:
continue
writer.writerow(row)
firstfile = False
import csv
from datetime import datetime
import glob
Time = datetime.now()
Time = Time.strftime("%Y%B%d""_""%H%M")
inputFiles = [] #[i for i in glob.glob('*.{}'.format(extension))]
for file in glob.glob("*.csv"):
inputFiles.append(file)
print(inputFiles)
with open("combined" + Time + '.csv', 'xb') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
outputFile = "combined" + Time + '.csv'
for file in inputFiles:
f = open(file, "r") # set f as opening the given csv in the same file location
reader = csv.reader(f) # set reader as a readable copy of the csv
rows = [] # set rows as an empty list
for (
row
) in (
reader
): # for every row in reader, try to append a new row in our rows list, and if now, pass
try:
with open(outputFile, "a", newline="") as g:
# create a csv writer
writer = csv.writer(g)
# write the account number and the docket to the csv file
writer.writerow(row)
except:
pass
4条答案
按热度按时间yshpjwxd1#
我赞成Awk的解决方案,但由于这个问题明确地询问了Python,这里有一个解决方案。
用法:
python script.py first.csv second.csv etc.csv >final.csv
这个简单的脚本并没有真正受益于Python的任何特性,但是如果你需要计算非平凡CSV文件中的字段数(即,带引号的字段可能包含逗号,而不是分隔符),这在Awk中很难,而在Python中很容易(因为
csv
库已经知道如何处理)。pkmbmrz72#
您可以使用
awk
执行此操作:FNR
是指当前文件中的记录号(通常是行号),NR
是指总记录号。因此,接受第一个文件的第一行,忽略后续文件的第一行。这是假设您的所有csv文件都具有相同的列数和相同的顺序。
j8yoct9x3#
此命令应该适用于您:
tail -qn +2 *.csv > file.csv
但是,请注意,您需要在每个文件的末尾有一个额外的空行,否则文件将在同一行中连接
1, 12, 2
,而不是第1行中的1, 1
和第2行中的2, 2
。jhdbpxl94#
下面的代码对我很有效。