[英]Iterator inside DictWriter.writerow
I'm trying to process a data struct as follows: 我正在尝试如下处理数据结构:
dict<ID, actions>
where actions
is a list of dictionaries, and each of them contain two pairs (at least): 'gameCode':value, 'gameTime':value. 其中
actions
是词典列表,每个词典至少包含两对:'gameCode':value,'gameTime':value。
So my data is like: 所以我的数据就像:
{user1: [{gameCode : 1, gameTime : 1}, {gameCode : 2, gameTime : 2}],
user2: [{gameCode : 1, gameTime : 1}, {gameCode : 2, gameTime : 2}]}
This data come from a log in which each row represents one action. 此数据来自日志,其中每一行代表一个动作。
I have to produce an output as the following: 我必须产生如下输出:
ID |
ID | action |
动作| time |
时间| action |
动作| time ...
时间 ...
I was wondering how could I write these pairs of action/times in a pythonic way, maybe using a list comprehension. 我想知道如何用Python的方式编写这些动作/时间对,也许使用列表理解。 This is what I have so far:
这是我到目前为止的内容:
event = 'gameCode'
data = 'gameTime'
# Open the .csv file and creates a dict of actions
with open(file_path, 'rb') as csvfile:
spamreader = csv.DictReader(csvfile, delimiter='\t')
for row in spamreader:
# Add an empty list for 'userID' if it is not registered yet in dict
user = row['userID']
actions = Users.get(user, [])
# Delete the 'userID' from the information
del row['userID']
# Add a register of actions for this user
actions.add(row)
# Update its values
users[user] = actions
# Sort each list of actions based on time
for key, value in users.iteritems():
users[key] = sorted(value, key=lambda k: k[data])
# Write a .csv to be consumed by the app
with open('eventsInput.csv', 'w') as csvfile:
csv.writer(csvfile, delimiter=' ')
for key, value in users.iteritems():
actions = [[d[event], d[data]] for d in value]
writer.writerow([key, 0, 0, 0, 0, [action for action in actions]])
I'm stuck at this last line. 我被困在最后一行。
With this, I get this output: 这样,我得到以下输出:
L11-13 0 0 0 0 "[['1002', '358']]" L11-13 0 0 0 0“ [['1002','358']]”
But I don't want nothing but 1002 358 (ie, no " , ' [ etc) 但是我什么也不要,只需要1002 358(即不,“,” [等)
Unfortunately you can't have multiple fieldnames with the same value in a DictWriter, you could revert to a normal csv writer (though you might have to deal with uneven lengths of action time tuples for a specific id) or consider that for any id you have a list of actions and a list of times: 不幸的是,在DictWriter中不能有多个具有相同值的字段名,您可以恢复为普通的csv编写器(尽管对于特定的id,您可能不得不处理长度不一的动作时间元组),或者对于任何id,您都可以考虑使用它有一个动作列表和一个时间列表:
from csv import DictWriter
data = [{'id': 1, 'actions':[1,2,3], 'times': [1,2,3]},
{'id': 2, 'actions':[4,5,6], 'times': [4,5,6]}]
with open('fred.csv', 'w+') as f:
writer = DictWriter(f, ['id', 'actions', 'times'])
writer.writeheader()
writer.writerows(data)
fred.csv fred.csv
id,actions,times
1,"[1, 2, 3]","[1, 2, 3]"
2,"[4, 5, 6]","[4, 5, 6]"
When you read it in, you can zip actions and times to get them back together. 阅读时,您可以压缩操作和时间以使它们重新组合在一起。
After some researcher and a lot of trials, I found out that I can accomplish what I want with this piece of code: 经过一些研究人员和大量的试验,我发现我可以用这段代码完成我想要的工作:
with open('eventsInput.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter='\t')
for key, value in users.iteritems():
# That was what changed the result
actions = list(chain.from_iterable((d[event], d[data]) for d in value))
print actions
writer.writerow([key, 0, 0, 0, 0] + [action for action in actions])
Just don't know why I can't do: 只是不知道为什么我不能做:
actions = [chain.from_iterable((d[event], d[data]) for d in value)]
To fix your updated code, you can just add lists: 要修复更新的代码,您只需添加列表即可:
writer.writerow([key, 0, 0, 0, 0] + [action for action in actions]])
But this may cause issues on read because you may have variable number of fields per row, unless len(actions)
is a constant. 但这可能会导致读取问题,因为除非
len(actions)
是常数,否则每行的字段数可能会变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.