read and save a specific column from csv file - python

need help! using previous topics, I found how I can read data form csv file and I do not have problem with this but I can not save a specific column (e.g. column 4 from file.csv) as new.csv file. my script prints column 4 correctly but it does not save it.
import csv
with open('file.csv') as csvfile:
file1 = csv.reader(csvfile, delimiter=',')
fourth_col = []
for cols in file1:
fourth_col = cols[3]
print (fourth_col)
new_file = open('new.csv', 'w')
writer = csv.writer(new_file)
writer.writerows(fourth_col)
new_file.close()

I tried the following code and it is working fine
import csv
new_file = open('new.csv', 'w')
writer = csv.writer(new_file)
with open('file.csv') as csvfile:
file1 = csv.reader(csvfile, delimiter=',')
fourth_col = []
for cols in file1:
fourth_col = cols[3]
print(fourth_col)
writer.writerows(fourth_col)
new_file.flush()
new_file.close()
sample files:
file.csv
a,b,c,d,e
f,g,h,i,j
k,l,m,,n
,,,o,
new.csv
d
i
o

Related

how to write a csv file in another csv file in python without pandas

I am a beginner in python, I want to read a csv file and create a second csv file with with some of the data from the first csv.I did this but the second file have only the header
def create_fifa(infile, outfile):
fieldnames = ["Team", "Nationality", "Position", "Age", "Potential" ,"Name"]
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
data = csv.writer(outfile)
writer.writeheader()
for row in csv.DictReader(infile):
writer.writerow(row)
data.writerow((row))
return outfile
I assume fieldnames is a subset of all the columns. In that case, you need to use extrasaction='ignore'.
An example with an NBA dataset as I don't have your FIFA dataset.
import csv
infile = 'C:\\Users\\BRB\\nba.csv'
outfile = 'C:\\Users\\BRB\\NewFile.csv'
keepList = ['Name','Team','Position','Age','Height','Weight']
def create_nba(infile, outfile, fields):
infile = csv.DictReader(open(infile))
writer = csv.DictWriter(open(outfile,'w'), fieldnames=fields, extrasaction='ignore')
writer.writeheader()
for row in infile:
writer.writerow(row)
return outfile
f = create_nba(infile, outfile, keepList)

how to replace blank cell with previous cell value use python

I want to replace blank cell to previous value in python.
Example:
data = [AAA, , ,BBB,CCC, ,DDD]
expected data = [AAA,AAA,AAA,BBB,CCC,CCC,DDD]
import csv
filename = 'some.csv'
with open(filename, newline='') as f:
reader = csv.reader(f)
rows = list(csv_reader)
print(rows)
Try this:
Before running the code, the image of CSV file:
The code:
import csv
filename = 'Book1.csv'
with open(filename, newline='') as f:
reader = csv.reader(f)
rows = list(reader)
with open(filename,'w',newline='') as file:
csvwriter = csv.writer(file)
previous=rows[0]
for row in rows:
if row==[]:
csvwriter.writerow(previous)
else:
csvwriter.writerow(row)
previous=row
After running the code:

Python file matching and appending

This is one file result.csv:
M11251TH1230
M11543TH4292
M11435TDS144
This is another file sample.csv:
M11435TDS144,STB#1,Router#1
M11543TH4292,STB#2,Router#1
M11509TD9937,STB#3,Router#1
M11543TH4258,STB#4,Router#1
Can I write a Python program to compare both the files and if line in result.csv matches with the first word in the line in sample.csv, then append 1 else append 0 at every line in sample.csv?
import pandas as pd
d1 = pd.read_csv("1.csv",names=["Type"])
d2 = pd.read_csv("2.csv",names=["Type","Col2","Col3"])
d2["Index"] = 0
for x in d1["Type"] :
d2["Index"][d2["Type"] == x] = 1
d2.to_csv("3.csv",header=False)
Considering "1.csv" and "2.csv" are your csv input files and "3.csv" is the result you needed
The solution using csv.reader and csv.writer (csv module):
import csv
newLines = []
# change the file path to the actual one
with open('./data/result.csv', newline='\n') as csvfile:
data = csv.reader(csvfile)
items = [''.join(line) for line in data]
with open('./data/sample.csv', newline='\n') as csvfile:
data = list(csv.reader(csvfile))
for line in data:
line.append(1 if line[0] in items else 0)
newLines.append(line)
with open('./data/sample.csv', 'w', newline='\n') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(newLines)
The sample.csv contents:
M11435TDS144,STB#1,Router#1,1
M11543TH4292,STB#2,Router#1,1
M11509TD9937,STB#3,Router#1,0
M11543TH4258,STB#4,Router#1,0
With only one column, I wonder why you made it as a result.csv. If it is not going to have any more columns, a simple file read operation would suffice. Along with converting the data from result.csv to dictionary will help in quick run as well.
result_file = "result.csv"
sample_file = "sample.csv"
with open(result_file) as fp:
result_data = fp.read()
result_dict = dict.fromkeys(result_data.split("\n"))
"""
You can change the above logic, in case you have very few fields on csv like this:
result_data = fp.readlines()
result_dict = {}
for result in result_data:
key, other_field = result.split(",", 1)
result_dict[key] = other_field.strip()
"""
#Since sample.csv is a real csv, using csv reader and writer
with open(sample_file, "rb") as fp:
sample_data = csv.reader(fp)
output_data = []
for data in sample_data:
output_data.append("%s,%d" % (data, data[0] in result_dict))
with open(sample_file, "wb") as fp:
data_writer = csv.writer(fp)
data_writer.writerows(output_data)
The following snippet of code will work for you
import csv
with open('result.csv', 'rb') as f:
reader = csv.reader(f)
result_list = []
for row in reader:
result_list.extend(row)
with open('sample.csv', 'rb') as f:
reader = csv.reader(f)
sample_list = []
for row in reader:
if row[0] in result_list:
sample_list.append(row + [1])
else:
sample_list.append(row + [0]
with open('sample.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(sample_list)

python write to new column in csv file

I want to add a new column to an existing file. But it gets a little complicated with the additional loops i add.
input file:
testfile.csv
col1,col2,col3
1,2,3
3,4,5
4,6,7
output i want:
USA_testfile.csv
col1,col2,col3,country
1,2,3,USA
3,4,5,USA
4,6,7,USA
UK_testfile.csv
col1,col2,col3,country
1,2,3,UK
3,4,5,UK
4,6,7,UK
This is what i have tried:
import csv
import sys
country_list= ['USA', 'UK']
def add_col(csv_file):
for country in country_list:
with open(csv_file, 'rb') as fin:
with open(country+"_timeline_outfile_"+csv_file, 'wb') as fout:
writer = csv.writer(fout, lineterminator='\n')
reader = csv.reader(fin)
all_rows =[]
row = next(reader)
row.append('country')
all_rows.append(row)
print all_rows
for row in reader:
row.append(country)
all_rows.append(row)
writer.writerows(all_rows)
add_col(sys.argv[1])
And this is the error i got:
File "write_to_csv.py", line 33, in add_col
writer.writerows(all_rows)
ValueError: I/O operation on closed file
I was trying to follow this post here
import csv
countries = ['USA', 'UK']
data = list(csv.reader(open('testfile.csv', 'rb')))
for country in countries:
with open('{0}_testfile.csv'.format(country), 'wb') as f:
writer = csv.writer(f)
for i, row in enumerate(data):
if i == 0:
row = row + ['country']
else:
row = row + [country]
writer.writerow(row)
I couldn't reproduce your error, but i cleaned your code a bit.
There is no reason to reopen the input file for every language.
def add_col(csv_file):
with open(csv_file, 'rb') as fin:
reader = csv.reader(fin)
for country in country_list:
fin.seek(0) # jump to begin of file again
with open(country+"_timeline_outfile_"+csv_file, 'wb') as fout:
writer = csv.writer(fout, lineterminator='\n')
header = next(reader)
header.append('country')
writer.writerow(header)
for row in reader:
row.append(country)
writer.writerow(row)

Merging selected colums from multiple text file to one text file using Python

I have multiple text files with 4 columns (tab separated). Each file will have around 2000 rows. Using Python, how can i create a new file that looks like this?
file1column1 file1column4 file2column4 file3column4 ...fileNcolumn4
Thanks.
Here is the code i tried:
file_lists = ['file1.data', 'file2.data']
temp_data = []
for a_file in file_lists:
file_h = open(a_file)
a_list = []
csv_reader = csv.reader(file_h, delimiter='\t')
for row in csv_reader:
if afile == "file1.data":
a_list.extend([row[0], row[3]])
else:
a_list.append(row[3])
temp_data.append((n for n in a_list))
file_h.close()
with open('output.data', 'w') as output_file:
csv_writer = csv.writer(output_file, delimiter='\t')
for row in list(zip(*temp_data)):
csv_writer.writerow(row)
output_file.close()
Courtesy: Combining columns of multiple files in one file - Python
I am getting results in following format though:
file1column1 file2column4
file1column4 file2column4
file1column1 file2column4
file1column4 file2column4
What about:
file_lists = [('file1.data', 1), ('file1.data', 3), ('file2.data',3)]
temp_data = []
for a_file in file_lists:
file_h = open(a_file[0])
a_list = []
csv_reader = csv.reader(file_h, delimiter='\t')
for row in csv_reader:
a_list.append(row[a_file[1]])
temp_data.append((n for n in a_list))
file_h.close()
with open('output.data', 'w') as output_file:
csv_writer = csv.writer(output_file, delimiter='\t')
for row in list(zip(*temp_data)):
csv_writer.writerow(row)
You don't need to close file when you use with ... as

Categories