Write single list to csv by column - python

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.

Related

How to convert a list into a csv file?

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:

Copy csv values to another csv

I'm copying the contents of a csv file and writing these to another so I can modify it later but can't figure out what the simple solution is. I thought I could keep the input csv_file and output writer files open while copying the contents, don't know if that's a good idea. My code
import csv, os
file_path = 'path/to/file.csv'
output_path = os.path.dirname(os.path.abspath(file_path)) + '/'
with open(file_path) as csv_file:
data_source = csv.reader(csv_file, delimiter=',')
with open(output_path + 'results.csv', 'w', newline='') as writer:
for line in data_source:
writer.writerow(line)
This is the error I get:
AttributeError: '_io.TextIOWrapper' object has no attribute 'writerow'
The object that you think is the writer is not. Instead, you should construct the writer separately.
with open(file_path) as csv_file:
data_source = csv.reader(csv_file, delimiter=',')
with open(output_path + 'results.csv', 'w', newline='') as results_file:
data_sink = csv.writer(results_file) #This is the important line
for line in data_source:
data_sink.writerow(line)
Can you try this code and see if this works?
import csv, os
file_path = 'path/to/file.csv'
output_path = os.path.dirname(os.path.abspath(file_path)) + '/'
with open(file_path) as csv_file:
data_source = csv.reader(csv_file, delimiter=',')
f = open(output_path + 'results.csv', 'w', newline='')
with f:
writer = csv.writer(f)
for line in data_source:
writer.writerow(line)
The error means that there is no method for writerow for the writer.

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)

Python read CSV file columns and write file name and column name in a csv file

I have many CSV files, need to read all the files in loop and write file name and all the columns (header in row 1) in an output file.
Example
Input csv file 1 (test1.csv)
Id, Name, Age, Location
1, A, 25, India
Input csv file 2 (test2.csv)
Id, ProductName
1, ABC
Outputfile
test1.csv Id
test1.csv Name
test1.csv Age
test1.csv Location
test2.csv Id
test2.csv ProductName
Many thanks for your help.
Update:
This code works fine for this purpose:
import os
import csv
ofile = open('D:\Anuj\Personal\OutputFile/AHS_File_Columns_Info.csv', 'w')
directory = os.path.join('D:\Anuj\Personal\Python')
for root, dirs, files in os.walk(directory):
for file in files:
fullfilepath = directory + "/" + file
with open(fullfilepath,'r') as f:
output = file +','+ f.readline()
ofile.write(output)
clean solution using csv module for reading and writing
open output file and create a csv.writer instance on its handle
open each input file and create a csv.reader instance on their handle
get first row using next on the csv.reader iterator: gets titles as list (with a small post-processing to remove the spaces)
write titles alongside the current filename in a loop
code:
import csv
files=["test1.csv","test2.csv"]
with open("output.tsv","w",newline='') as fw:
cw = csv.writer(fw,delimiter="\t") # output is tab delimited
for filename in files:
with open(filename,'r') as f:
cr = csv.reader(f)
# get title
for column_name in (x.strip() for x in next(cr)):
cw.writerow([filename,column_name])
There are several advantages using csv module, the most important being that quoting & multi-line fields/titles are managed properly.
But I'm not sure I understand you correctly.
import csv
from typing import List
from typing import Tuple
TableType = List[List[str]]
def load_csv_table(file_name: str) -> Tuple[List[str], TableType]:
with open(file_name) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
headers = next(csv_reader)
data_table = list(csv_reader)
return headers, data_table
def save_csv_table(file_name: str, headers: List[str], data_table: TableType):
with open(file_name, 'w', newline='') as csv_file:
writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(headers)
for row in data_table:
writer.writerow(row)
input_files = ['file1.csv', 'file2.csv', 'file3.csv']
new_table = []
new_headers = []
for file_name in input_files:
headers, data_table = load_csv_table(file_name)
if not new_headers:
new_headers = ['Source'] + headers
new_table.extend(([file_name] + line for line in data_table))
save_csv_table('output.csv', new_headers, new_table)
A simple method is to use readline() on the file object:
files=["test1.csv","test2.csv"]
for my_file in files:
with open(my_file,'r') as f:
print my_file, f.readline()

csv value modification for certain cells on odd rows on a particular column

Hi I'm trying to finish this small piece of code for modifying csv files, I've got this far with some help:
edit... some more info.
Basically what I’m looking to do is make some small changes to the csv file depending on the project and parent issue in JIRA. Python will then make the changes to the csv file before it is then read into JIRA - that’s the second part of the program I’ve not even really looked at yet.
I’m only looking to change the BOX-123 type cells and leave the blank ones blank.
But the idea of the program is that I can use it to make some small changes to a template which will then automatically create some issues in JIRA.
import os
import csv
project = 'Dudgeon'
parent = 'BOX-111'
rows = (1,1007)
current = os.getcwd()
filename = 'test.csv'
filepath = os.path.join(os.getcwd(), filename)
#print(current)
#print(filename)
print(filepath)
with open(filepath, 'r') as csvfile:
readCSV = csv.reader(csvfile)
next(readCSV, None)
for row in readCSV:
print(row[16])
row_count =sum(1 for row in readCSV)
print(row_count)
with open(filepath, 'r') as infile, open('out.csv', 'w') as outfile:
outfile.write(infile.readline()) # write out the 1st line
for line in infile:
cols = line.strip().split(',')
cols[16] = project
outfile.write(','.join(cols) + '\n')
with open('out.csv', 'r') as infile, open('out1.csv', 'w') as outfile:
for row in infile:
if row % 2 != 0:
cols [15] = parent
outfile.write()
Any help really appreciated.
You want to use the row's index when comparing to 0. Use enumerate():
with open('out.csv', 'r') as infile, open('out1.csv', 'w') as outfile:
for rowidx,row in enumerate(infile):
cols = row.strip().split(',')
if rowidx % 2 != 0:
cols[15] = parent
outfile.write(cols)
You really should be using the csv module here, though. Untested but should get you started.
with open('out.csv', 'r') as infile, open('out1.csv', 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for rowidx,row in enumerate(reader):
if rowidx % 2 != 0:
row[15] = parent
writer.write_row(row)
A friend helped me last night and this is what they came up with:
with open(filepath, 'r') as infile, open('out.csv', 'w') as outfile:
outfile.write(infile.readline()) # write out the 1st line
for line in infile:
cols = line.strip().split(',')
cols[16] = project
outfile.write(','.join(cols) + '\n')
with open('out.csv', 'r') as infile, open('out1.csv', 'w') as outfile:
outfile.write(infile.readline()) # write out the 1st line
lineCounter = 0
for line in infile:
lineCounter += 1
cols = line.strip().split(',')
if lineCounter % 2 != 0:
cols[15] = parent
outfile.write(','.join(cols) + '\n')

Categories

Resources