当一列有多行时,如何使用python解析csv

当一列有多行时,如何使用python解析csv,python,csv,Python,Csv,我有一个csv文件,是“名字,地点,东西”。thing列通常有“word\nanotherword\nanotherword\n”我正在尝试找出如何将其解析为单独的行,而不是单个列中的多行条目。i、 e 姓名、地点、单词 姓名、地点、其他单词 姓名、地点、其他单词 我确信这很简单,但我很难理解我需要做什么。用这个专栏包装器包装您的csv阅读器: def column_wrapper(reader): for name, place, thing in reader: fo

我有一个csv文件,是“名字,地点,东西”。thing列通常有“word\nanotherword\nanotherword\n”我正在尝试找出如何将其解析为单独的行,而不是单个列中的多行条目。i、 e

姓名、地点、单词

姓名、地点、其他单词

姓名、地点、其他单词


我确信这很简单,但我很难理解我需要做什么。

用这个
专栏包装器包装您的csv阅读器

def column_wrapper(reader):
    for name, place, thing in reader:
        for split_thing in thing.strip().split('\n'):
            yield name, place, split_thing
data = []
with open('filewithdata.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        name, place, thing = tuple(row)
        if '\\n' in thing:
            for item in thing.split('\\n'):
                if item != '\n':
                    data.append([name, place, item)]

您将获得金牌。

使用此
列包装器包装您的csv阅读器

def column_wrapper(reader):
    for name, place, thing in reader:
        for split_thing in thing.strip().split('\n'):
            yield name, place, split_thing
data = []
with open('filewithdata.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        name, place, thing = tuple(row)
        if '\\n' in thing:
            for item in thing.split('\\n'):
                if item != '\n':
                    data.append([name, place, item)]

您将获得金牌。

您始终可以逐行读取文件

#! /usr/bin/env python2.7.2
file = open("demo.csv", "r+");
for line in file:
   line =  line.replace(",", " ")
   words = line.split()
   print(words[0])
   print(words[1])
   print(words[2])   
file.close()
假设文件内容是

name1,place1,word1
name2,place2,anotherword2
name3,place3,anotherword3

您总是可以逐行读取文件

#! /usr/bin/env python2.7.2
file = open("demo.csv", "r+");
for line in file:
   line =  line.replace(",", " ")
   words = line.split()
   print(words[0])
   print(words[1])
   print(words[2])   
file.close()
假设文件内容是

name1,place1,word1
name2,place2,anotherword2
name3,place3,anotherword3

在不深入代码的情况下,基本上您要做的是检查“thing”中是否有换行符。如果有,则需要在换行符上拆分它们。这将为您提供一个令牌列表(“thing”中的行),因为这本质上是一个内部循环,所以您可以使用原始的
名称
位置
以及新的
thing\u令牌
。生成器函数很适合于此

这让我想到了kroolik的答案。然而,kroolik的回答有一点错误:

如果要使用
列包装器
生成器,则需要考虑csv读取器在换行符中转义反斜杠,因此它们看起来像
\\n
而不是
\n
。此外,你需要检查空白的“东西”

def column_wrapper(reader):
    for name, place, thing in reader:
        for split_thing in thing.strip().split('\\n'):
            if split_thing:
                yield name, place, split_thing
然后您可以获得如下数据:

with open('filewithdata.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    data = [[data, name, thing] for data, name, thing in column_wrapper(reader)]
或(不带
列包装器
):

我建议使用
column\u wrapper
,因为生成器更通用、更具python风格


确保将
import csv
添加到文件的顶部(尽管我相信您已经知道)。希望有帮助

在不深入代码的情况下,基本上您要做的是检查“thing”中是否有换行符。如果有,则需要在换行符上拆分它们。这将为您提供一个令牌列表(“thing”中的行),因为这本质上是一个内部循环,所以您可以使用原始的
名称
位置
以及新的
thing\u令牌
。生成器函数很适合于此

这让我想到了kroolik的答案。然而,kroolik的回答有一点错误:

如果要使用
列包装器
生成器,则需要考虑csv读取器在换行符中转义反斜杠,因此它们看起来像
\\n
而不是
\n
。此外,你需要检查空白的“东西”

def column_wrapper(reader):
    for name, place, thing in reader:
        for split_thing in thing.strip().split('\\n'):
            if split_thing:
                yield name, place, split_thing
然后您可以获得如下数据:

with open('filewithdata.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    data = [[data, name, thing] for data, name, thing in column_wrapper(reader)]
或(不带
列包装器
):

我建议使用
column\u wrapper
,因为生成器更通用、更具python风格


确保将
import csv
添加到文件的顶部(尽管我相信您已经知道)。希望有帮助

您是否自己为此编写了任何代码?查看csv模块:您是否自己为此编写了任何代码?查看csv模块: