简体   繁体   English

如何使用逗号读取csv字段并将字段中的字符串拆分为列表?

[英]How to read csv field with comma and split string in the field to a list?

I am trying to read csv and split some output string with delimiter into a list, but when I tried to read the list using array, it threw error saying "list index out of range".我正在尝试读取 csv 并将一些带有分隔符的输出字符串拆分为一个列表,但是当我尝试使用数组读取列表时,它抛出错误,提示“列表索引超出范围”。 The list should have 2 elements.该列表应该有 2 个元素。 Please, see attached codes.请参阅附加代码。 Please, kindly help me pointing out what was wrong with the code.请帮助我指出代码有什么问题。 The index 0 works just fine.索引 0 工作得很好。

The csv file look like this: ID, Full Name, Last First Name csv 文件如下所示:ID、全名、姓氏

1, John Smith, "Smith, Jonh" 1、约翰·史密斯《史密斯·乔恩》

2, Camil Johnson, "Johnson, Camil" 2、卡米尔约翰逊《约翰逊卡米尔》

3, Bang Dong, "Dong, Bang" 3、棒东,“咚,砰”

4, Charles Simpson, "Simpson, Charles" 4、查尔斯·辛普森《辛普森,查尔斯》

import csv
with open('testfile.csv', 'r', newline='') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        # print(row)
        # print (row[2])
        a = row[2].split(sep=", ")
        print(a[1])

示例 csv 文件

You might try this:你可以试试这个:

import pandas as pd

df = pd.read_csv('testfile.csv')

This will read in your csv file into a data frame, and you can access the columns and convert them to lists like this:这会将您的 csv 文件读入数据框中,您可以访问列并将它们转换为如下列表:

df['Full Name'].tolist()

The problem is your are using "," as a delimiter, but your "Last First Name" Column also contains commas.问题是您使用“,”作为分隔符,但您的“姓氏”列也包含逗号。

In this line of code:在这行代码中:

a = row[2].split(sep=", ")

you are trying to separate the first name from the last name and assume it returns a tuple which you are trying to display here: print(a[1])您试图将名字与姓氏分开,并假设它返回一个您试图在此处显示的元组:print(a[1])

The Problem is a will not be a tuple, because of the comma thats inside of this column.问题是 a 不会是元组,因为此列内有逗号。 Instead there will be a row[3], in your case "Smith" will be in row[2] and "John" in row[3].相反,会有一行 [3],在您的情况下,“Smith”将在行 [2] 中,而“John”将在行 [3] 中。 To avoid this, use a different delimiter .为避免这种情况,请使用不同的分隔符

Using pandas the same way I coded with csv works very well.使用与我使用 csv 编码相同的方式使用 Pandas 效果很好。 Thanks Nathaniel.谢谢纳撒尼尔。

import pandas as pd
df = pd.read_csv('testfile.csv')
a = df['Last First Name'].tolist()
for row in a:
    b = row.split(', ')
    print(b[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM