为了账号安全,请及时绑定邮箱和手机立即绑定

使用列表比较和更新 CSV 文件

使用列表比较和更新 CSV 文件

江户川乱折腾 2023-06-27 17:21:30
我正在写一些需要两个 CSV 的内容:#1 是电子邮件列表,每封电子邮件收到 # 封电子邮件,#2 是记录中的每个电子邮件地址的目录,每个报告期间收到的电子邮件编号,日期在顶部注释列。import csvfrom datetime import datetimedatestring = datetime.strftime(datetime.now(), '%m-%d')storedEmails = []newEmails = []sortedList = []holderList = []with open('working.csv', 'r') as newLines, open('archive.csv', 'r') as oldLines:   #readers to make lists    f1 = csv.reader(newLines, delimiter=',')    f2 = csv.reader(oldLines, delimiter=',')    print ('Processing new data...')    for row in f2:        storedEmails.append(list(row))                                   #add archived data to a list    storedEmails[0].append(datestring)                                   #append header row with new date column    for col in f1:        if col[1] == 'email' and col[2] == 'To Address':                 #new list containing new email data            newEmails.append(list(col))    counter = len(newEmails)    n = len(storedEmails[0])                                             #using header row len to fill zeros if no email received    print(storedEmails[0])    print (n)    print ('Updating email lists and tallies, this could take a minute...')with open ('archive.csv', 'w', newline='') as toWrite:                   #writer to overwrite old csv    writer = csv.writer(toWrite, delimiter=',')    for i in newEmails:        del i[:3]                   #strip useless identifiers from data        if int(i[1]) > 30:          #only keep emails with sufficient traffic            sortedList.append(i)    #add these emails to new sorted listCSV 看起来与此类似。我不确定我在哪里出错了 - 但它不断产生重复的条目。一些功能是存在的,但我已经使用它太久了,我的视野狭隘,试图找出我的循环做错了什么。我知道我的零填充部分最终也是错误的,因为它将附加到新创建的记录的末尾,而不是填充零直到其第一次出现。我确信有更有效的方法可以做到这一点,我是编程新手,所以它可能过于复杂和混乱 - 最初我尝试将 CSV 与 CSV 进行比较,并意识到这是不可能的,因为你不能同时读写时间,所以我尝试转换为使用列表,我也知道当列表变大时,由于内存限制,列表不会永远工作。
查看完整描述

2 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

  • 我会加载数据pandas.read_csv

  • .rename一些专栏

    • 创建数据帧后working,查看数据帧以验证已加载正确的列,并且使用正确的列索引进行重命名。

    • 重命名 , 中的列working取决于列索引,因为working.csv没有列标题。

    • datearch实际上应该是email,因为标题标识其下方的内容,而不是其他列标题。

    • 一旦在 中更改了列名称archive.csv,则不再需要重命名。

  • pandas.merge在柱子上email

    • 由于两个数据框都有一列重命名为email,因此合并结果将只有一email列。

    • 如果合并发生在两个不同的列名称上,则结果将有两列包含电子邮件地址。

    • pandas:合并、连接、连接和比较

  • 只要文件中的列一致,就应该无需修改即可工作

import pandas as pd

from datetime import datetime


# get the date string

datestring = datetime.strftime(datetime.now(), '%d-%b')


# read archive

arch = pd.read_csv('archive.csv')


# rename columns

arch.rename(columns={'date': 'email'}, inplace=True)


# read working, but only the two columns that are needed

working = pd.read_csv('working.csv', header=None, usecols=[1, 3])


# rename columns

working.rename(columns={1: 'email', 3: datestring}, inplace=True)


# only emails greater than 30 or already in arch

working = working[(working[datestring] > 30) | (working.email.isin(arch.email))]


# merge

arch_updated = pd.merge(arch, working, on='email', how='outer').fillna(0)


# save to csv

arch_updated.to_csv('archive.csv', index=False)


# display(arch_updated)

          email  01-sep  27-Aug

 asdf@email.com   154.0    31.0

 fsda@email.com   128.0    19.0

 qwer@gmail.com    77.0    92.0

 ffff@xmail.com    63.0     0.0

 zxcv@email.com     0.0   117.0


查看完整回答
反对 回复 2023-06-27
?
慕沐林林

TA贡献2016条经验 获得超9个赞

所以,问题是你有两组数据。两者都通过“关键”条目(电子邮件)存储数据以及您想要压缩到一个存储中的其他数据。确定这两组数据都存在相似的“密钥”可以大大简化这一过程。


将每个键想象为一个存储桶的名称。每个存储桶需要两条信息,一条来自一个 csv,另一条来自另一个 csv。


现在,我必须绕点小弯路来解释一下Python中的字典。这是从这里窃取的定义


字典是一个无序、可更改且有索引的集合。


集合是一个类似于保存数据的列表的容器。无序和索引意味着字典不能像列表一样访问,其中数据可以通过索引访问。在这种情况下,字典是使用键访问的,键可以是字符串或数字之类的任何东西(从技术上讲,键必须是可散列的,但这太深入了)。最后,可更改意味着字典实际上可以更改其存储的数据(再次过于简单化)。


例子:


dictionary = dict()

key = "Something like a string or a number!"

dictionary[key] = "any kind of value can be stored here! Even lists and other dictionaries!"

print(dictionary[key])  # Would print the above string

我建议您使用以下结构来代替大多数列表:


dictionary[email] = [item1, item2]

这样,您可以避免使用多个列表并大大简化您的代码。


查看完整回答
反对 回复 2023-06-27
  • 2 回答
  • 0 关注
  • 109 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信