如何在写入CSV之前删除数据中不必要的换行符

时间:2019-06-05 08:18:14

标签: python

我想做的是将一堆信息存储到.csv中 在运行脚本并将所有电子邮件地址放入一个csv文件后,我注意到以下问题。在单元格CE周围,行中断并且正在创建新行。有时,该行最终在电子邮件地址的中间中断。 实际示例:

电子邮件,name @ email.com,name @ email.com,name @ email.com,name @ em

ail.com,name @ email.com,name @ email.com,name @ email.com,name @ email.c

om,name @ email.com,name @ email.com,name @ email.com,name @ email.com]

我还需要提到我正在尝试使用此脚本拉动2万个联系人。

import requests
import csv
import unicodedata
import getpass

# Credentials
url = 'https://....com/api/v2/users.json'
csvfile = 'users2.csv'

output_3 = []
output_3.append("email")

while url:

    # Use Your Sign-On Credentials
user = '...'
pwd = '...'
response = requests.get(url, auth=(user, pwd))
data = response.json()

for user in data['users']:
    email = user['email']
    decode_3 = bool(email) and unicodedata.normalize('NFKD', email)
    output_3.append(decode_3)

print(data['next_page'])
url = 'https://.../api/v2/users.json' and data['next_page']
print("users:")
print(len(output_3))

# Write to a csv file
with open((csvfile), mode='w', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow([output_3])

1 个答案:

答案 0 :(得分:0)

您的output_3列表可能包含了空格或换行符。您可以尝试剥离尾随空白str.strip()的邮件,或手动删除空白字符(str.replace('\n', '')str.replace(' ', ''))。