Giter Site home page Giter Site logo

ekapope / combine-csv-files-in-the-folder Goto Github PK

View Code? Open in Web Editor NEW
143.0 143.0 76.0 3 KB

Tiny script to automate everyday task

Home Page: https://medium.com/free-code-camp/how-to-combine-multiple-csv-files-with-8-lines-of-code-265183e0854

Python 100.00%
automation combine csv excel python

combine-csv-files-in-the-folder's People

Contributors

ekapope avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

combine-csv-files-in-the-folder's Issues

How to remove double quotes while writing to CSV file?

Hi All,
i am trying below code to read from txt/csv file and writing to csv file. below is the code and csv.writer is appending with double qoutes. can some one help me how to remove double qoutes while writing to csv?

import csv
with open('file.txt') as infile, open('test.csv', 'w', newline='') as outfile:
reader = csv.reader(infile)
for line in (reader):
newline = [','.join(line)]
writer = csv.writer(outfile)
writer.writerow(newline)

output generated by csv.writer:-

"create table script(id number(2),name varchar2(10));"
"create table script1(id number(2),name varchar2(10));"

Input file looks like:-

create table script
(
id number(2)
,name varchar2(10)
);

create table script1
(
id number(2)
,name varchar2(10)
);

output file should be:-

1.create table script(id number(2),name varchar2(10));
2.create table script1(id number(2),name varchar2(10));

Unicode Error or Win Error File Not Found

Hello - I'm fairly new to Python so this might be a stupid question, when I try this code:

`# -- coding: utf-8 --
"""
Created on Sat Jan 12 06:56:16 2019
@author: Chris
"""
#credited:
#https://stackoverflow.com/questions/9234560/find-all-csv-files-in-a-directory-using-python/12280052

import os
import glob
import pandas as pd
#set working directory
os.chdir("C:\Users\chadc\desktop\TEST")

#find all csv files in the folder
#use glob pattern matching -> extension = 'csv'
#save result in list -> all_filenames
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
#print(all_filenames)

#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')`

I get this error:

os.chdir("C:\Users\chadc\desktop\TEST")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

And when I run this code:

-- coding: utf-8 --

"""
Created on Sat Jan 12 06:56:16 2019
@author: Chris
"""
#credited:
#https://stackoverflow.com/questions/9234560/find-all-csv-files-in-a-directory-using-python/12280052

import os
import glob
import pandas as pd
#set working directory
os.chdir("\desktop\TEST")

#find all csv files in the folder
#use glob pattern matching -> extension = 'csv'
#save result in list -> all_filenames
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
#print(all_filenames)

#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')

I get this error:

Traceback (most recent call last):
File "C:\Users\chadc\Desktop\Combine.py", line 13, in
os.chdir("\desktop\TEST")
FileNotFoundError: [WinError 3] The system cannot find the path specified: '\desktop\TEST'
[Finished in 2.2s]

Using Sublime text editor on Windows 10...

Encoded

image
So, I have to compile a lot of CSV, but the encode of they is cp1252 (Portuguese Brazilian) How I can define that, I search in a lot of places
the give that error:
image
but I declare upside # -- coding: cp1252 --

Identify axis

Beautiful code! Thanks!

Your code appended my columns.
I resolved it with:
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ],axis=1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.