使用 Python 以小写形式读入 CSV 文件
Read in a CSV file in lower case with Python
我正在将 CSV 文件读入 namedtuple:
import csv
from collections import namedtuple
#So we can handle bad CSV files gracefully
def unfussy_reader(reader):
while True:
try:
yield next(reader.lower())
# This is a bad row that has an error in it (csv.Error)
# Alternately it may be a line that doesn't map to the structure we've been given (TypeError)
except (csv.Error, TypeError):
pass
continue
# Create the CSV reader object
csv_reader = csv.reader(file_stream, delimiter=' ', quotechar='"', escapechar='^')
# Set up the named tuple
csvline = namedtuple('csv_line', 'field1, field2, field3')
# Create the named tuple mapping object
map_to_tuple = map(csvline._make, csv_reader)
for line in unfussy_reader(map_to_tuple):
# do stuff
这很好用,但我的问题是 - 我希望 CSV 的所有内容都以小写形式读取。根据 this question,一个简单的 lambda 就可以做到:
map(lambda x:x.lower(),["A","B","C"])
但在数据最终进入元组之前我找不到任何地方可以放置它(因此无法更改)。
有没有办法在这个结构中做到这一点 (Python 3.5)?
这个怎么样:
csv_reader = csv.reader(map(lambda line:line.lower(),file_stream), delimiter=' ', quotechar='"', escapechar='^')
您可以在为流创建 CSV reader 之前将 lower
转换应用于流。
lower_stream = (line.lower() for line in file_stream)
csv_reader = csv.reader(lower_stream, delimiter=' ', quotechar='"', escapechar='^')
lower_stream
赋值目标周围的括号指定 generator expression。它不会用完 file_stream
,也不会将所有 file_stream
拉入内存。
我正在将 CSV 文件读入 namedtuple:
import csv
from collections import namedtuple
#So we can handle bad CSV files gracefully
def unfussy_reader(reader):
while True:
try:
yield next(reader.lower())
# This is a bad row that has an error in it (csv.Error)
# Alternately it may be a line that doesn't map to the structure we've been given (TypeError)
except (csv.Error, TypeError):
pass
continue
# Create the CSV reader object
csv_reader = csv.reader(file_stream, delimiter=' ', quotechar='"', escapechar='^')
# Set up the named tuple
csvline = namedtuple('csv_line', 'field1, field2, field3')
# Create the named tuple mapping object
map_to_tuple = map(csvline._make, csv_reader)
for line in unfussy_reader(map_to_tuple):
# do stuff
这很好用,但我的问题是 - 我希望 CSV 的所有内容都以小写形式读取。根据 this question,一个简单的 lambda 就可以做到:
map(lambda x:x.lower(),["A","B","C"])
但在数据最终进入元组之前我找不到任何地方可以放置它(因此无法更改)。
有没有办法在这个结构中做到这一点 (Python 3.5)?
这个怎么样:
csv_reader = csv.reader(map(lambda line:line.lower(),file_stream), delimiter=' ', quotechar='"', escapechar='^')
您可以在为流创建 CSV reader 之前将 lower
转换应用于流。
lower_stream = (line.lower() for line in file_stream)
csv_reader = csv.reader(lower_stream, delimiter=' ', quotechar='"', escapechar='^')
lower_stream
赋值目标周围的括号指定 generator expression。它不会用完 file_stream
,也不会将所有 file_stream
拉入内存。