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)
Related
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:
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.
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)
I have the following list of numbers: ['Number', 1,2,3,4]
If I have the following CSV file:
`Name`
`First`
`Second`
`Third`
`Fourth`
How do I add my list of numbers to it and make it look like this:
`Name Number`
`First 1`
`Second 2`
`Third 3`
`Fourth 4`
You can use fileinput.input with inplace=True to modify the original file:
import fileinput
import sys
l =['Number', 1,2,3,4]
for ind, line in enumerate(fileinput.input("in.csv",inplace=True)):
sys.stdout.write("{} {}\n".format(line.rstrip(), l[ind]))
Input:
Name
First
Second
Third
Fourth
Output:
Name Number
First 1
Second 2
Third 3
Fourth 4
Or write to a tempfile and move with shutil.move to replace the original file:
l =['Number', 1,2,3,4]
from shutil import move
from tempfile import NamedTemporaryFile
with open('in.csv') as csvfile, NamedTemporaryFile("w",dir=".", delete=False) as temp:
r = csv.reader(csvfile)
wr = csv.writer(temp,delimiter=" ")
for row,new in zip(r,l):
wr.writerow(row+[new])
move(temp.name,"in.csv")
Not an elegant way but It works:
#!/usr/bin/python
import csv
import sys
def csv_to_dict(csv_file_path):
csv_file = open(csv_file_path, 'rb')
csv_file.seek(0)
sniffdialect = csv.Sniffer().sniff(csv_file.read(10000), delimiters='\t,;')
csv_file.seek(0)
dict_reader = csv.DictReader(csv_file, dialect=sniffdialect)
csv_file.seek(0)
dict_data = []
for record in dict_reader:
dict_data.append(record)
csv_file.close()
return dict_data
def dict_to_csv(csv_file_path, dict_data):
csv_file = open(csv_file_path, 'wb')
writer = csv.writer(csv_file, dialect='excel')
headers = dict_data[0].keys()
writer.writerow(headers)
for dat in dict_data:
line = []
for field in headers:
line.append(dat[field])
writer.writerow(line)
csv_file.close()
if __name__ == '__main__':
org_path = sys.argv[1]
new_path = sys.argv[2]
your_array = ['Number', 1, 2, 3, 4]
org_csv = csv_to_dict(org_path)
new_data = []
for line in org_csv:
new_line = dict()
new_line['Name'] = line['Name']
new_line[your_array[0]] = your_array[org_csv.index(line)+1]
new_data.append(new_line)
if new_data:
dict_to_csv(new_path, new_data)
Hope that will help!
import csv
with open('existing_file.csv', 'rb') as infile:
reader = csv.reader(infile)
your_list = list(reader)
list2 = ['Number', 1,2,3,4]
zipped= zip(your_list, list2)
with open("test.csv", "wb") as outfile:
writer = csv.writer(outfile)
writer.writerows(zipped)
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.