I have written a small python code where it will read a sample csv file and copy its first column to a temp csv file. Now when I try to compare that temporary file with another text file and try to write result to another file called result file, The file is created but with empty content.
But when i tested it in chunks, It is working fine
import csv
f = open("sample.csv", "r")
reader = csv.reader(f)
data = open("temp1.csv", "w")
w = csv.writer(data)
for row in reader:
my_row = []
my_row.append(row[0])
w.writerow(my_row)
with open('temp1.csv', 'r') as file1:
with open('serialNumber.txt', 'r') as file2:
same = set(file1).intersection(file2)
with open('result.txt', 'w') as file_out:
for line in same:
file_out.write(line)
print line
sample.csv
M11435TDS144,STB#1,Router#1
M11543TH4292,STB#2,Router#1
M11509TD9937,STB#3,Router#1
M11543TH4258,STB#4,Router#1
serialNumber.txt
G1A114042400571
M11543TH4258
M11251TH1230
M11435TDS144
M11543TH4292
M11509TD9937
You should close the output file (temp1.csv) before you can read data from it.
import csv
f = open("sample.csv", "r")
reader = csv.reader(f)
data = open("temp1.csv", "w")
w = csv.writer(data)
for row in reader:
my_row = []
my_row.append(row[0])
w.writerow(my_row)
data.close() # <--- Should close it before reading it in the same program !!
with open('temp1.csv', 'r') as file1:
with open('serialNumber.txt', 'r') as file2:
same = set(file1).intersection(file2)
with open('result.txt', 'w') as file_out:
for line in same:
file_out.write(line)
print line
Points regarding code:
data file handle is not closed. data.close() after writing to temp1.csv.
In your code, same = set(file1).intersection(file2), you are directly passing file handle file2 to intersection. It expects list. This is exact problem is. It should be same = set(file1.readlines()).intersection(file2.readlines())
Working Code:
import csv
f = open("sample.csv", "r")
reader = csv.reader(f)
data = open("temp1.csv", "wb")
w = csv.writer(data)
for row in reader:
my_row = []
if len(row) != 0:
my_row.append(row[0])
w.writerow(my_row)
#File should be closed
data.close()
with open('temp1.csv', 'r') as file1:
with open('serialNumber.txt', 'r') as file2:
tmp_list = (file1.readlines())
ser_list = (file2.readlines())
same = set(file1.readlines()).intersection(file2.readlines())
with open('result.txt', 'w') as file_out:
for line in same:
file_out.write(line)
Content of temp1.csv:
M11435TDS144
M11543TH4292
M11509TD9937
M11543TH4258
Content of result.txt :
M11543TH4258
M11543TH4292
M11435TDS144
You can use with for opening files sample.csv and temp1.csv as below.
import csv
with open("sample.csv") as f:
with open("temp1.csv",'wb') as data:
reader = csv.reader(f)
w = csv.writer(data)
for row in reader:
my_row = []
my_row.append(row[0])
w.writerow(my_row)
with open('temp1.csv', 'r') as file1:
with open('serialNumber.txt', 'r') as file2:
same = set(file1.readlines()).intersection(file2.readlines())
with open('result.txt', 'w') as file_out:
for line in same:
file_out.write(line)
Related
00,0,6098
00,1,6098
00,2,6098
00,3,6098
00,4,6094
00,5,6094
00,6,6094
00,7,6094
00,8,6094
That's a snip from my csv file. The file contains about 465 lines but when I read it only 380 lines are read. Here's my code:
import csv
import itertools
with open('lala.csv', 'r') as in_file:
lines = in_file.read().splitlines()
stripped = [line.replace(","," ").split() for line in lines]
grouped = itertools.izip(*[stripped]*1)
with open('lala.csv', 'w') as out_file:
writer = csv.writer(out_file)
for group in grouped:
writer.writerows(group)
file=open( "lala.csv", "r")
reader = csv.reader(file)
for line in reader:
print(line[2])
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
file 1.csv:
traider1,domain.net,tomb,Raider1
traider,domain.net,tomb,Raider
file 2.txt:
TECH-1377
TECH-1366
How to combine these 2 files into 3rd ?
so i can get 3.csv:
traider1,domain.net,tomb,Raider1,TECH-1377
traider,domain.net,tomb,Raider,TECH-1366
i tried :
import fileinput
files= ['1.csv','2.txt']
allfiles = fileinput.input(files)
for line in allfiles: # this will iterate over lines in all the files
print(line)
and got
traider1,domain.net,tomb,Raider1
traider,domain.net,tomb,Raider
TECH-1377
TECH-1366
import csv
with open('file 1.csv', 'r') as f:
data = f.readlines() #Read file1
with open('file 2.csv', 'r') as f2:
data2 = f2.readlines() #Read file2
with open('file 2.csv', 'w') as f3:
writer = csv.writer(f3, delimiter=',')
for i, line in enumerate(data):
val = "{0},{1}".format(line.strip(), data2[i].strip())
writer.writerow(val.split(","))
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)
with open(filename,"r") as f:
reader = csv.reader(f,delimiter = ",")
data = list(reader)
rownum = len(data)
with open(filename2,"r") as f:
reader2 = csv.reader2(f,delimiter = ",")
data2 = list(reader2)
rownum2 = len(data)
if rownum > rownum2:
delete(rownum until rownum = rownum2 at end)
It's pretty simple.
data = data[:max(rownum, rownum2)]
data2 = data2[:max(rownum, rownum2)]
Do you need to save the new data? If so this will do the job and the files must not even be in csv format:
with open(filename1, "rb") as f:
f1lines = f.readlines()
with open(filename2, "rb") as f:
f2lines = f.readlines()
length = min(len(f1lines), len(f2lines))
f1lines = f1lines[:length]
f2lines = f2lines[:length]
with open(filename1, "wb") as f:
for line in f1lines:
f.write(line)
with open(filename2, "wb") as f:
for line in f2lines:
f.write(line)