【发布时间】:2021-12-07 22:17:35
【问题描述】:
我想根据条件从列表中插入值,
例如,请在下面找到我使用CSV模块的代码算法。
out_file = open("c://Project//in.csv", "w")
header = ['List','Integer', 'Float', 'String']
l = [[4,5], 12, 2.4, "This is a string", [1,2], 45, ["Second String"]]
writer = csv.writer(out_file)
insert_header_in_the_beginning
for i in l:
if check_first_element_is_list_using_regex:
insert_in_first_column_of_first_row(i)
elif check_second_element_is_integer_using_regex:
insert_in_second_column_of_first_row(i)
elif check_third_element_is_float_using_regex:
insert_in_third_column_of_first_row(i)
elif check_last_element_is_string_using_regex:
insert_in_last_column_of_first_row(i)
new_row_starts
期望的输出
List Integer Float String
[4,5] 12 2.4 This is a String
[1,2] 45 None Second String
.....
【问题讨论】:
-
我意识到这是伪代码,但我不明白使用 RE 分析列表内容的明显意图。你能解释一下吗?另外,请根据您的示例数据显示您的预期输出(CSV 文件内容)应该是什么样子
-
@BrutusForcus 使用 RE 识别元素是 Char、Int 还是 Decimal。实际上在这里我只是展示了一个最简单的例子,但是实际代码中的列表具有复杂的模式。是的,我已经在我的问题中更新了所需的输出。谢谢!
标签: python python-3.x list python-2.7 csv