Skip to content

Instantly share code, notes, and snippets.

@xtornasol512
Last active October 30, 2017 17:44
Show Gist options
  • Save xtornasol512/b7ee7aa36a2b4b344738a3068ae321d4 to your computer and use it in GitHub Desktop.
Save xtornasol512/b7ee7aa36a2b4b344738a3068ae321d4 to your computer and use it in GitHub Desktop.
Join Csv files in the same directory
'''
Script for join CSV
author: @xtornaso5l2
'''
import os
files = os.listdir(os.curdir)
# ['.DS_Store', 'download (1).csv', 'download (2).csv', 'download.csv']
files.remove('.DS_Store') # Only for mac
# ['download (1).csv', 'download (2).csv']
with open('name_file.csv', 'w') as outfile:
for file in files:
with open(file) as infile:
for line in infile:
outfile.write(line)
''' Can join two diferents csv files Author:@xtornasol512 '''
import csv
with open('results.csv', 'w') as outfile:
with open('file_one.csv', 'rb') as f:
# file has space to delimiter rows, actually is not a csv.
spamreader = csv.reader(f, delimiter=' ', quotechar='|')
for row in spamreader:
# add format converting to a csv separated by tabs
line_tab = '\t'.join(row) + '\t\n'
outfile.write(line_tab)
with open('file_two.csv', 'rb') as file:
# This file is already a tab csv so just add lines
for line in file:
outfile.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment