Home > Software engineering >  How do I loop through csv file rows and remove all excess spaces?
How do I loop through csv file rows and remove all excess spaces?

Time:03-27

This is my first python project so my apologies if this is a silly question. I want to loop through the rows of some csv files I created and replace all excess spaces with a single space, and then replace those spaces with commas so I can make my columns.

Here's where I am so far:

for new_files in os.walk(target): #"target" is a desktop folder. Only contains .txt and .csv files
for new_name in new_files:
    raw_filename=os.fsdecode(new_name)
    raw_file=target '\\' new_name
    os.chmod(target,stat.S_IWRITE)
    if new_name.endswith('.csv'):
        for row in csv.reader(open(raw_file)):
            while row.contains('  '):
                str.replace('  ', ' ')
    else:
        continue

I get the following error:

filename = fspath(filename)  # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not list

Here is a sample of a csv file I am trying to edit (in the raw file there is variable spacing between each value, and they're all in column A of an excel spreadsheet):

Ch= a 
[ 1222 20940 85 -49 -11 284 5191 -2] 
Ch= a 
[11772 2319 116 -50 -10 302 5190 -2] 
Ch= a 
[1634 513 187 -49 -10 1051 5190 -2] 
Ch= a 
[ 370 470 1863 -49 -10 11516 5189 -2] 
Ch= a 
[ 294 374 9674 -50 -11 2048 5190 -2] 
Ch= a 
[ 345 490 5238 -50 -10 479 5190 -2]

CodePudding user response:

import os
import re
PATH = "path/to/folder"
for old_files in os.listdir(PATH):
    if old_files.endswith(".csv"):
        with open(f"{PATH}/{old_files}", "r") as f1:
            lines = list(
                map(
                    lambda x: x.strip(),
                    filter(lambda x: "Ch= a" not in x, f1.readlines()),
                )
            )
        with open(f"{PATH}/new_{old_files}", "w") as f2:
            for l in lines:
                m = re.search("(\[\s?)(.*)(\s?\])", l)
                f2.write(m.group(2).replace(" ", ","))
                f2.write("\n")

CodePudding user response:

I assume that you are using Python 3, so I am going to use the pathlib library, which is easier to use than the os one.

import csv
import pathlib


root = pathlib.Path("~/temp/csv_files").expanduser()
for path in root.glob("*.csv"):

    # Read a text file, convert text lines to row (a list of strings)
    rows = []
    with open(path, "r", encoding="utf-8") as stream:
        for line in stream:
            if line.startswith("Ch="):
                continue
            row = line.replace("[", "").replace("]", "").split()
            rows.append(row)

    # Write the rows
    with open(path, "w", encoding="utf-8") as stream:
        writer = csv.writer(stream)
        writer.writerows(rows)

Notes

  • Replace the root with your own directory
  • Instead of os.walk(), I use .glob(), which is more convenient
  • I assume you want to convert the text file into CSV in place, meaning to write the output to the same file, replacing the old contents
  • Related