在填充cs时,我尝试跳过行

2024-09-21 11:49:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用在线课程讨论论坛中的问题、答案和评论为SNA项目构建edgelist。你知道吗

以下是我构建edgelist的代码:

# import libraries
import csv
import collections
from collections import defaultdict

def multi_dimensions(n, type):
  # """ Creates an n-dimension dictionary where the n-th dimension is of type 'type' 
  if n<=1:
    return type()
  return defaultdict(lambda:multi_dimensions(n-1, type))

edgelist = multi_dimensions(2,dict)

csv.field_size_limit(1600000)
f = open('/Users/samuelfinegold/Documents/harvard/edXresearch/snaCreationFiles/time_series/time_series.csv','rU')
reader = csv.DictReader(f, delimiter=',')

for line in reader:
    # edgelist
    if line['types'] == 'Question':
        #print 'T'
        source = line['author_id']
    else:
        edgelist[source]['target'] = line['author_id']
        edgelist[source]['start_time'] = line['time']

以下是上下文的csv:

post_id thread_id   author_id       types   time    votes_up    votes_down  posters
1       0            Jan         Question     3/1/12 10:45  5   1   Jan, Janet, Jack
2       0            Janet       Answer   3/1/12 11:00  2   1   Jan, Janet, Jack
3       0            Jack        Comment      3/2/12 8:00   0   0   Jan, Janet, Jack
4       1            Jason       Question     3/4/12 9:00   3   1   Jason, Jan, Janet
5       1            Jan         Answer   3/7/12 1:00   3   1   Jason, Jan, Janet
6       1            Janet       Answer   3/7/12 2:00   1   2   Jason, Jan, Janet

以下是我给我的边缘专家的建议:

source  target  time
Jan         Jack    999
Jason   Janet   999

我不知道是什么导致时间如此不同。你知道吗

期望输出:

source     target     start_time
Jan        Janet      3/1/12 11:00
Jan        Jack       3/2/12 8:00
Jason      Jan        3/7/12 1:00
Jason      Janet      3/7/12 2:00

请注意,答案或评论发布的时间。对于那些熟悉动态SNA图的人,您可能会想知道为什么我没有结束时间。这是因为,为了简单起见,我将为每个帖子手动添加一个具有相同结束时间的列,这意味着图表不会显示任何消失。你知道吗


Tags: csvimportidsourcetimetypeline时间

热门问题