如何让 Python 只看逗号,前后没有 space 作为分隔符
How to make Python only look at commas with no space before or after as delimiters
我有一个 csv 文件,我正在尝试将其读入 python、操作,然后写入另一个 csv 文件。
我目前的问题是,虽然文件是逗号分隔的,但并不是所有的逗号都是分隔符。
只有 NOT 前面 and/or 后跟 space 的逗号才应该算作分隔符。 (只有“,”而不是“,”或“,”)。
我的代码如下所示:
import csv
#open file for reading
with open(mypath, 'r', encoding = 'utf_8') as csvfile:
myfile = list(csv.reader(csvfile, dialect = 'excel', delimiter = ','))
#specifying columns to be deleted
BadCols = [29,28,27,25,21,20,19,18,16,15,14,13,12,11,8,7,4,3]
#Loop through column indices to be deleted
for col in BadCols:
#Loop through each row to delete columns
for i, row in enumerate(myfile):
#Delete Column, which is basically a list item at that row
myfile[i].pop(col)
#Open file for writing
with open(mypath2, "w", encoding = 'utf_8', newline='') as csvfile:
csv_file = csv.writer(csvfile, dialect = 'excel', delimiter = ',')
for i, row in enumerate(myfile):
for j, col in enumerate(row):
csvfile.write('%s, ' %col)
csvfile.write('\n')
csvfile.close
我的数据如下所示:
Date,Name,City
May 30, 2016,Ryan,Boston
以下是我使用 excel 打开文件时希望看到的内容:
Date Name City
May 30, 2016 Ryan Boston
这是我从 Excel 中实际看到的内容:
Date [Blank column name] Name City
May 30 2016 Ryan Boston
因此,日期被读取为两个元素而不是一个。
如有任何帮助,我们将不胜感激。
正则表达式可能是您最好的选择:
import re
patt = re.compile(r"\b,\b")
with open("in.csv") as f:
for row in map(patt.split, f):
print(row)
哪个会给你:
['Date', 'Name', 'City\n']
['May 30, 2016', 'Ryan', 'Boston']
您将不得不处理尾随的白色space,但这应该不是什么大问题。显然,如果您将 "foo,bar"
作为名称,您也会 运行 遇到问题,即但如果不是,则 re 方法会很好。
另一种选择可能是将 ", "
或 " ,"
替换为 space:
import csv
import re
patt = re.compile(r"\s(,)|(,)\s")
with open("in.csv") as f:
for line in csv.reader(map(lambda s: patt.sub(" ", s), f)):
print(line)
因此:
Date,Name,City
May 30, 2016,Ryan,Boston
May 31 ,2016,foo,Narnia
您将获得:
['Date', 'Name', 'City']
['May 30 2016', 'Ryan', 'Boston']
['May 31 2016', 'foo', 'Narnia']
我有一个 csv 文件,我正在尝试将其读入 python、操作,然后写入另一个 csv 文件。
我目前的问题是,虽然文件是逗号分隔的,但并不是所有的逗号都是分隔符。
只有 NOT 前面 and/or 后跟 space 的逗号才应该算作分隔符。 (只有“,”而不是“,”或“,”)。
我的代码如下所示:
import csv
#open file for reading
with open(mypath, 'r', encoding = 'utf_8') as csvfile:
myfile = list(csv.reader(csvfile, dialect = 'excel', delimiter = ','))
#specifying columns to be deleted
BadCols = [29,28,27,25,21,20,19,18,16,15,14,13,12,11,8,7,4,3]
#Loop through column indices to be deleted
for col in BadCols:
#Loop through each row to delete columns
for i, row in enumerate(myfile):
#Delete Column, which is basically a list item at that row
myfile[i].pop(col)
#Open file for writing
with open(mypath2, "w", encoding = 'utf_8', newline='') as csvfile:
csv_file = csv.writer(csvfile, dialect = 'excel', delimiter = ',')
for i, row in enumerate(myfile):
for j, col in enumerate(row):
csvfile.write('%s, ' %col)
csvfile.write('\n')
csvfile.close
我的数据如下所示:
Date,Name,City
May 30, 2016,Ryan,Boston
以下是我使用 excel 打开文件时希望看到的内容:
Date Name City
May 30, 2016 Ryan Boston
这是我从 Excel 中实际看到的内容:
Date [Blank column name] Name City
May 30 2016 Ryan Boston
因此,日期被读取为两个元素而不是一个。
如有任何帮助,我们将不胜感激。
正则表达式可能是您最好的选择:
import re
patt = re.compile(r"\b,\b")
with open("in.csv") as f:
for row in map(patt.split, f):
print(row)
哪个会给你:
['Date', 'Name', 'City\n']
['May 30, 2016', 'Ryan', 'Boston']
您将不得不处理尾随的白色space,但这应该不是什么大问题。显然,如果您将 "foo,bar"
作为名称,您也会 运行 遇到问题,即但如果不是,则 re 方法会很好。
另一种选择可能是将 ", "
或 " ,"
替换为 space:
import csv
import re
patt = re.compile(r"\s(,)|(,)\s")
with open("in.csv") as f:
for line in csv.reader(map(lambda s: patt.sub(" ", s), f)):
print(line)
因此:
Date,Name,City
May 30, 2016,Ryan,Boston
May 31 ,2016,foo,Narnia
您将获得:
['Date', 'Name', 'City']
['May 30 2016', 'Ryan', 'Boston']
['May 31 2016', 'foo', 'Narnia']