如何遍历 csv 文件行并删除所有多余的空格?
How do I loop through csv file rows and remove all excess spaces?
这是我的第一个 python 项目,如果这是一个愚蠢的问题,我深表歉意。我想遍历我创建的一些 csv 文件的行,并用一个 space 替换所有多余的 space,然后用逗号替换那些 space,这样我就可以制作我的专栏.
这是我目前所在的位置:
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
我收到以下错误:
filename = fspath(filename) # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not list
这是我尝试编辑的 csv 文件示例(在原始文件中,每个值之间的间距可变,它们都在 excel 电子表格的 A 列中):
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]
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")
我假设你正在使用 Python 3,所以我将使用 pathlib
库,它比 os
库更容易使用。
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)
备注
- 把root换成你自己的目录
- 我用
.glob()
代替os.walk()
,比较方便
- 我假设您想将文本文件就地转换为 CSV,意思是将输出写入同一个文件,替换旧内容
这是我的第一个 python 项目,如果这是一个愚蠢的问题,我深表歉意。我想遍历我创建的一些 csv 文件的行,并用一个 space 替换所有多余的 space,然后用逗号替换那些 space,这样我就可以制作我的专栏.
这是我目前所在的位置:
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
我收到以下错误:
filename = fspath(filename) # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not list
这是我尝试编辑的 csv 文件示例(在原始文件中,每个值之间的间距可变,它们都在 excel 电子表格的 A 列中):
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]
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")
我假设你正在使用 Python 3,所以我将使用 pathlib
库,它比 os
库更容易使用。
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)
备注
- 把root换成你自己的目录
- 我用
.glob()
代替os.walk()
,比较方便 - 我假设您想将文本文件就地转换为 CSV,意思是将输出写入同一个文件,替换旧内容