gpt4 book ai didi

csv 文件上的 Python 正则表达式

转载 作者:行者123 更新时间:2023-11-30 23:30:01 24 4
gpt4 key购买 nike

我有一个更好的问题,那就是如何思考这个问题的最佳解决方案。我的 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 到一张纸中有多少,并将日期放在开始前面、逗号之前的每一行中,如下例所示:

02/12/2013, "06:00 World Sport", 03/12/2013 "06:00 World Sport", 04/12/2013 "06:00 World of Sport"...
02/12/2013, "06:30 Inside Africa: November 23,2013", 03/12/2013, "06:30 African Voices.."

我的起始代码是这样的:

尝试:

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'

最佳答案

你根本不需要正则表达式;只需使用 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)

结果:

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

关于csv 文件上的 Python 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com