csv - Writing a list of lists to file, removing unwanted characters and a new line for each -
i have list "newdetails" list of lists , needs written csv file. each field needs take cell (without trailing characters , commas) , each sublist needs go on new line.
the code have far is:
file = open(s + ".csv","w") file.write(str(newdetails)) file.write("\n") file.close()
this however, writes csv in following, unacceptable format:
[['12345670' 'iphone 9.0' '500' 2 '3' '5'] ['12121212' 'samsung laptop' '900' 4 '3' '5']]
the format wish in shown below:
12345670 iphone 9.0 500 5 3 5 12121212 samsung laptop 900 5 3 5
you can use csv
module write information csv file.
please check below links:
code:
import csv new_details = [['12345670','iphone 9.0','500',2,'3','5'], ['12121212','samsung laptop','900',4,'3','5']] import csv open("result.csv","w",newline='') fh writer = csv.writer(fh,delimiter=' ') data in new_details: writer.writerow(data)
content of result.csv:
12345670 "iphone 9.0" 500 2 3 5 12121212 "samsung laptop" 900 4 3 5
Comments
Post a Comment