Last active
October 30, 2017 17:44
-
-
Save xtornasol512/b7ee7aa36a2b4b344738a3068ae321d4 to your computer and use it in GitHub Desktop.
Join Csv files in the same directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 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