[英]Entire Python list not getting written into CSV file
I have a Python list as output from a program which contains multiple lists inside it. 我有一个Python列表作为程序的输出,该程序内部包含多个列表。 I'm trying to write it into a CSV file, but only the fist part is getting written.
我正在尝试将其写入CSV文件,但只有第一部分正在编写。 The list is:
清单是:
"['DELETE', 'FROM', 'abc', 'WHERE', ['c', '=', 'book']]\n- from: [['abc']]\n [0]:\n ['abc']\n- table: [['abc']]\n [0]:\n ['abc']\n- where_expr: ['c', '=', 'book']"
I converted the list to dictionary as follows: 我将列表转换为字典,如下所示:
dict1={'delete': delete_list}
df = pd.DataFrame(dict1, index=[0])
My output is: 我的输出是:
delete
0 ['DELETE', 'FROM', 'abc', 'WHERE', ['c', '=', 'book']]
I read some similar question and tried this..The entire output is written in the dataframe but I'm getting a single character in every row. 我读了一些类似的问题,并尝试了此方法。.整个输出都写在数据帧中,但每行都有一个字符。
df2=pd.DataFrame([i for i in delete_list],columns=['Query'])
df2
Output is: 输出为:
Query
0 [
1 '
2 D
3 E
4 L
5 E
6 T
.
.
it goes on.... 它继续....
What I'm actually trying is to have a different column for every nested list. 我实际上正在尝试的是为每个嵌套列表使用不同的列。 Like 1st column will have the first list then 2nd column will have the list from:
[['abc']]\\n [0]:\\n ['abc']
3rd will have table: [['abc']]
and so on.. Also how do I get rid of \\n?? 像第一列将具有第一个列表,然后第二列将具有以下列表:
[['abc']]\\n [0]:\\n ['abc']
第三列将具有表: [['abc']]
和等等。另外我该如何摆脱\\ n? strip() is not working! strip()不起作用!
If the goal is to write to a CSV file, you don't need pandas
, simply use the csv
module (in the standard library): 如果目标是写入CSV文件,则不需要
pandas
,只需使用csv
模块(在标准库中):
import csv
with open('dump.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=';', lineterminator='\n')
writer.writerow([delete_list])
or perhaps 也许
import csv
with open('dump.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=';', lineterminator='\n')
writer.writerow(delete_list.split("\n"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.