csv-fi上的Python regex

2024-10-01 13:36:35 发布

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

我有一个问题,最好是说如何思考这个问题的最佳解决办法。 我的CSV文件看起来像:

,02/12/2013,03/12/2013,04/12/2013,05/12/2013,06/12/2013,07/12/2013,08/12/2013,
06:00,"06:00 World Sport","06:00 World Sport","06:00 World Sport","06:00 World Sport","06:00 World Sport","06:00 World Sport","06:00 World Sport",06:00
,,,,,,,,
06:15,,,,,,,,06:15
,,,,,,,,
06:30,"06:30 Inside Africa: November 29, 2013","06:30 African Voices: Agatha Achindu","06:30 Inside the Louvre","06:30 Talk Asia: Franz Harary","06:30 Blueprint","06:30 Inside the Middle East","06:30 CNNGo",06:30

好的,我需要做的是,在一张纸上编辑从1到多少的日期, 把日期写在每一行的开始前面,在逗号之前,就像这个例子:

^{pr2}$

我的起始代码是这样的:

尝试:

for line in fileinput.input(fnames):

    if re.search(r'\d{2}/\d{2}/\d{4}.*',line):
            line_date = re.findall(r'\d{2}/\d{2}/\d{4}',line)[0]
            output.write(line_date+'\n')

    if re.search(r'\".+?\"',line):
        line_sadrzaj = re.findall(r'\".+?\"',line)[0]
        output.write(line_sadrzaj+'\n')



output.close()

你对这个问题有什么更好的想法吗。在

也许这样:

for line in fileinput.input(fnames):

                if re.search(r'\d{2}/\d{2}/\d{4}.*',line):
                    line_date = re.findall(r'\d{2}/\d{2}/\d{4}.*',line)[0]
                    line_split = re.split(r'\,',line_date)
                    for line1 in line_split:
                        var = line1
                        output.write(var+'\n')

                if re.search(r'\".+?\".*',line):
                    line_sadrzaj = re.findall(r'\".+?\".*',line)[0]
                    line_split1  = re.split  (r'\,',line_sadrzaj)
                    for line2 in line_split1:
                        var2 = line2
                        output.write(var2+'\n')
                    #output.write(line_sadrzaj+'\n'

Tags: inreforworldoutputsearchdateif
1条回答
网友
1楼 · 发布于 2024-10-01 13:36:35

您根本不需要regex;只需使用csv模块读取csv文件,然后将结果转换为所需的输出。在

示例:

import csv
with open('csv.csv') as text:
    table = list(csv.reader(text))

# get all dates (skipping first and last column)
dates = table[0][1:-1]

# get all shows (skipping first and last column and empty rows)
shows =  filter(''.join, (t[1:-1] for t in table[1:]))

# join dates and shows back together and do some formatting
for line in [zip(dates, s) for s in shows]:
    print ', '.join('{}, "{}"'.format(*t) for t in line)

结果:

^{pr2}$

相关问题 更多 >