You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python读取CSV生成一维邮箱列表的代码修正求助

Fixing the Nested List Issue for Email IDs from CSV

Hey there! I see exactly what's going on here—let's get that nested list sorted out into the flat format you need.

The Problem

When you use csv.reader, every row it returns is a list of values (even if your CSV only has one column). So when you do email_id.append(row), you're adding an entire sublist (like ['emailid1@xyz.com']) to your main list instead of just the email string itself.

You mentioned trying email_id.extend(row) and it didn't work—maybe you had a typo or placed it wrong? Let's walk through two solid solutions:

Solution 1: Use extend() instead of append()

Since each row is a single-element list, extend() will unpack that list and add the email string directly to your main list:

import csv
email_id = []
with open('contacts1.csv', 'r') as file:
    reader = csv.reader(file, delimiter=',')
    for row in reader:
        email_id.extend(row)  # Unpacks the row list and adds the email string
print(email_id)

Solution 2: Access the first element of the row

If you want to be explicit (great for readability, especially if you ever add more columns later), you can grab the first item in each row with row[0]:

import csv
email_id = []
with open('contacts1.csv', 'r') as file:
    reader = csv.reader(file, delimiter=',')
    for row in reader:
        # Make sure the row isn't empty before accessing index 0
        if row:
            email_id.append(row[0])
print(email_id)

I added a quick check for empty rows here to avoid index errors, which is a nice safety net if your CSV has any blank lines.

What the Output Will Look Like

Either solution will give you the flat list you want:

['emailid1@xyz.com','emailid2@xyz.com','emailid3@xyz.com']

内容的提问来源于stack exchange,提问作者nomanahmedna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:12:29