Removing Second Row Of Csv File And Modify The Columns Using Python
I am currently use the below-provided code to copy the CSV files from a folder to another and remove the second row of the CSV file present in it.
The above code removes the second row, i.e., row 1, from all the CSV files, but at the same time, it adds an empty row after each row in all the CSV files.
Example - Actual CSV:
| row_no | name | test |
| | | |
| | | input |
-------------------------
| 0 | abc | 123 |
| 1 | def | 456 |
| 2 | ghi | 789 |
| 3 | jkl | 101 |
After applying code:
| row_no | name | test |
| | | |
| | | input |
-------------------------
| 0 | abc | 123 |
| | | |
| 2 | ghi | 789 |
| | | |
| 3 | jkl | 101 |
Question 1: How do I remove the empty rows?
Question 2: I would like to remove the empty spaces in the column names;
Column Example:
| test |
| | -> |testinput|
| input |
Thanks
Answer
Q1: By default, csv.writer
writes CSV files in the "excel" dialect, which means line endings are \r\n
. Try setting csv.writer
into "unix"
mode, which has line endings that are plain \n
.
Q2: Simply use str.replace
to replace all spaces with nothing.
Here is a modified version of your code:
import glob, os, shutil, csv
source_path = "/home/Desktop/Output/"
dest_path = "/home/Desktop/Output2/"
file_read_path = "/home/Desktop/Output2/*.csv"
## Code to copy .csv files from one folder to another
for csv_file in glob.iglob(os.path.join(source_path, "*.csv"), recursive = True):
shutil.copy(csv_file, dest_path)
## Code to delete the second row in all .CSV files
for filename in glob.glob(file_read_path):
with open(filename, "r") as file:
reader = list(csv.reader(file , delimiter = ","))
# Remove second row
del reader[2] # 0=columnnames, 1=first data row, 2=second data row
# Remove spaces from column names
reader[0] = [colname.replace(" ", "") for colname in reader[0]]
with open(filename, "w") as output:
# Set csv.writer to output files in the "unix" dialect
writer = csv.writer(output, delimiter = ",", dialect="unix")
for row in reader:
writer.writerow(row)
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module