简体   繁体   English

在 Python 的 csv.reader 中删除神秘的双标签

[英]Removing Mysterious Double Tabs in Python's csv.reader

I'm taking a lesson on the CSV module.我正在学习 CSV 模块。

Pesky '\\t\\t' 's are appearing on the last value of each line, which is an email value.讨厌的'\\t\\t'出现在每行的最后一个值上,这是一个电子邮件值。

> ['first_name', 'last_name', 'email'] ['John', 'Doe',
> 'john-doe@bogusemail.com\t\t'] ['Mary', 'Smith-Robinson',
> 'maryjacobs@bogusemail.com\t\t'] ['Dave', 'Smith',
> 'davesmith@bogusemail.com\t\t']

I think this is happening because excel adds two tabs after the email, presumably to keep it from being a hyperlink.我认为这是因为 excel 在电子邮件后添加了两个选项卡,大概是为了防止它成为超链接。 But this is TMI.但这是TMI。

How can I read this in and strip the '\\t\\t' 's after each email?如何在每封电子邮件后阅读并删除'\\t\\t'

Here's what I have so far:这是我到目前为止所拥有的:

with open('names cs.csv') as f:
    reader = csv.reader(f)
    for i in reader:
        print(i)

The CSV module does not have a way to resolve this. CSV 模块无法解决此问题。 The easiest method would be to clean your data after the import.最简单的方法是在导入后清理数据。

Loop through your emails and replace all the '\\t' with a blank.循环浏览您的电子邮件并将所有 '\\t' 替换为空白。

emails = ['jeffadfafadsf\t\t','asdfasdfadsfadf\t\t']

for i in emails:
    i = i.replace('\t','')
    print(i) 

#   The same can be accomplished with the following line of code:

emails = [i.replace('\t','') for i in emails]
print(emails)

Since your list has more fields, you will need to do something to the effect of this:由于您的列表具有更多字段,因此您需要对此进行一些操作:

for i in reader:
    i[2] = i.replace('\t','')

If you are curious and want to read any documentation, it can be found here and the optional arguments (dialects and formatting parameters) documentation can be found here如果您好奇并想阅读任何文档,可以在此处找到,可选参数(方言和格式参数)文档可以在此处找到

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

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