How to convert a list into a csv file? - python

I'm trying to write csv file from a list:
list:
newest = ['x11;y11;z11', 'x12;y12;z12', 'x13;y13;z13', 'x14;y14;z14', 'x15;y15;z15', 'x16;y16;z16', 'x17;y17;z17', 'x18;y18;z18', 'x19;y19;z19', 'x20;y20;z20']
My actual code:
with open(r'listtocsv.csv', 'w', newline='\n') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL, delimiter=';')
wr.writerow(newest)
My actual result:
Result wanted:

import csv
newest = ['x11;y11;z11', 'x12;y12;z12', 'x13;y13;z13', 'x14;y14;z14', 'x15;y15;z15', 'x16;y16;z16', 'x17;y17;z17', 'x18;y18;z18', 'x19;y19;z19', 'x20;y20;z20']
new = []
for i in newest:
new.append(i.split(";"))
with open("file.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(new)
output:

Related

Write single list to csv by column

The following code searches for words given in the list and when matched writes the line to a csv row by row.
I am trying to find a solution so that instead of writing by row only, the 1st item in the list is column1 and 2nd item to column2 and so on.
parm_list = ["electricalAntennaTilt ","iuantSectorId ", "eUtranCellFDDId "]
os.remove("Parm.csv")
#keyword = input("Enter keyword here: ")
with open('Parm.csv', 'w', newline='\n', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["eNB", "Parameter", "Value"])
for filename in os.listdir(directory):
for i in parm_list:
if filename.endswith(".txt"):
with open(filename, "r", encoding="UTF-8") as file:
for line in file:
if re.search(i, line):
with open('Parm.csv', 'a', newline='\n', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([filename] + [line])
else:
continue
Current Output:
315655.txt electricalAntennaTilt 30
315655.txt iuantSectorId 315655_1_4
Expected output:
315655.txt electricalAntennaTilt 30 iuantSectorId 315655_1_4
I don't see your txt files, so just a guess:
import csv
import os
import re
directory = "~/Desktop" # put here a path to your directory
parm_list = ["electricalAntennaTilt ","iuantSectorId ", "eUtranCellFDDId "]
os.remove("Parm.csv")
#keyword = input("Enter keyword here: ")
with open("Parm.csv", "w", newline="\n", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["eNB", "Parameter", "Value"])
for filename in os.listdir(directory):
if filename.endswith(".txt"):
row = [filename] # <--------- make a row for every file
for i in parm_list:
with open(filename, "r", encoding="UTF-8") as file:
for line in file:
if re.search(i, line):
row.append(line) # < ----- fill the row
# write the row to the csv file
with open("Parm.csv", "a", newline="\n", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(row)
Why do you close the file every time? Beside of that, try iterating over parameters (parm_list) in an inner loop instead:
parm_list = ["electricalAntennaTilt ","iuantSectorId ", "eUtranCellFDDId "]
os.remove("Parm.csv")
#keyword = input("Enter keyword here: ")
with open('Parm.csv', 'w', newline='\n', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=["eNB"] + parm_list)
writer.writeheader()
for filename in os.listdir(directory):
if filename.endswith(".txt"):
with open(filename, "r", encoding="UTF-8") as file:
data = {'eNB': filename}
for line in file:
if data.keys() > set(parm_list):
break
for pattern in parm_list:
if pattern not in data and re.search(pattern, line):
data[pattern] = line.strip()
writer.writerow(data)
Not sure if this is what you are looking for, but change
with open('Parm.csv', 'a', newline='\n', encoding='utf-8') as csvfile:
to
with open('Parm.csv', 'a', newline='', encoding='utf-8') as csvfile:
in the second loop.

How to print output to a csv file?

I need help in writing code to print output to csv file. The code is posted below. Appreciate your help.
import csv
result = {}
with open('data.csv', 'rb') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvreader:
if row[0] in result:
result[row[0]].append(row[1])
else:
result[row[0]] = [row[1]]
print(result)
Use csv.writer to write the rows in result to an output stream:
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for row in result.items():
csvwriter.writerow(row)

converting TXT to CSV python

I have a txt data. it looks as follows
time pos
0.02 1
0.1 2
...
and so on. so the each line is separated with a space. I need to convert it in to a CSV file. like
time,pos
0.02,1
0.1,2
0.15,3
How can I do it with python ? This is what I have tried
time = []
pos = []
def get_data(filename):
with open(filename, 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
next(csvFileReader)
for row in csvFileReader:
time.append((row[0].split(' ')[0]))
pos.append((row[1]))
return
with open(filename) as infile, open('outfile.csv','w') as outfile:
for line in infile:
outfile.write(line.replace(' ',','))
From here:
import csv
with open(filename, newline='') as f:
reader = csv.reader(f, delimiter=' ')
for row in reader:
print(row)
For writing just use default options and it would save file with comma as a delimiter.
try:
import pandas as pd
with open(filename, 'r') as fo:
data = fo.readlines()
for d in range(len(data)):
if d==0:
column_headings = data[d].split()
data_to_insert = data[d].split()
pd.DataFrame(data_to_insert).to_excel('csv_file.csv', header=False, index=False, columns = column_headings))
You can use this:
import csv
time = []
pos = []
def get_data(filename):
with open(filename, 'r') as csvfile:
csvfile1 = csv.reader(csvfile, delimiter=' ')
with open(filename.replace('.txt','.csv'), 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
for row in csvfile1:
writer.writerow(row)

Cut data from one sheet into another in csv in python

I am working on a simple program to open a file and read certain rows and then print them in another new file but I want to cut them and remove them from the earlier csv. how do I do that?. This is what I have tried.
import csv
f = open('1.csv')
csv_f = csv.reader(f)
content_value = []
for row in csv_f:
if 'yepme' in row[2]:
content_value.append(row)
g = open('output.csv', 'wb')
wr = csv.writer(g, dialect='excel')
wr.writerows(content_value)
I am editing and found the answer:
import csv
f = open('1.csv')
csv_f = csv.reader(f)
content_value = []
old_value = []
for row in csv_f:
if 'yepme' in row[2]:
content_value.append(row)
else:
old_value.append(row)
g = open('output.csv', 'wb')
wr = csv.writer(g, dialect='excel')
wr.writerows(content_value)
h = open('2.csv','wb')
ws = csv.writer(h, dialect='excel')
ws.writerows(old_value)
A similar problem is mentioned in this question.
Short solution: Write two files: One with the extracted lines, one with the leftovers.
Coded solution:
import csv
with open('1.csv', 'r') as f:
csv_f = csv.reader(f)
new_content = []
old_content = []
for row in csv_f:
if 'yepme' in row[2]:
new_content.append(row)
else:
old_content.append(row)
with open('output.csv', 'wb') as f:
wr = csv.writer(f, dialect='excel')
wr.writerows(new_content)
with open('1.csv', 'wb') as f:
wr = csv.writer(f, dialect='excel')
f.writerows(old_content)
I never used csv, but you should get the idea. If your csv-file is very huge, you should probably read and write line-by-line to avoid memory issues.

How to export data from csv as a list(python 3)

I have a list like this(python 3)
my_list = [["xxx","moon",150],["wordq","pop",3]]
and i save it on a csv using this code
import csv
myfile = open("pppp.csv", 'wb')
with open("pppp.csv", "w", newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_NONE)
wr.writerows(list_of_DVDsuppliers)
now i need to export this csv in to my program as a list and change the data .
please help me ?
Just convert the data you get from reader() to a list:
data = csv.reader(open('example.csv','r'))
data = list(data)
print data
Unless you have a reason why you are using newline='', you can skip that and below code works with python 2.7,
import csv
my_list = [["xxx","moon",150],["wordq","pop",3]]
myfile = open("pppp.csv", 'wb')
with open("pppp.csv", "w") as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_NONE)
wr.writerows(my_list)
data = csv.reader(open('pppp.csv','r'))
for row in data:
print row

Categories

Resources