I have a Python list as output from a program which contains multiple lists inside it. I'm trying to write it into a CSV file, but only the fist part is getting written. 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?? strip() is not working!
If the goal is to write to a CSV file, you don't need pandas
, simply use the csv
module (in the standard library):
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"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.