Remove comma in between each word in file
Question:
My problem is after running my code came expected output, but in file contains comma between each words.
My input file:
idNo;UserGroup;Name;Description;Owner;Visibility;Members
id;grp1;bhalaji;asdfgh;bhalaji;public
abc
def
ghi
id;grp2;bhalaji;asdfgh;bhalaji;private
abc
def
ghi
My code:
import csv
output = []
temp = []
currIdLine = ""
with( open ('usergroups.csv', 'r')) as f:
for lines in f.readlines():
line = lines.strip()
if not line:
print("Skipping empty line")
continue
if line.startswith('idNo'): # append the header to the output
output.append(line)
continue
if line.startswith('id'):
if temp:
print(temp)
output.append(currIdLine + ";" + ','.join(temp))
temp.clear()
currIdLine = line
else:
temp.append(line)
output.append(currIdLine + ";" + ','.join(temp))
print("n".join(output))
with open('new.csv', 'w') as f1:
writer = csv.writer(f1)
writer.writerows(output)
Output of the code:
u,u,i,d,;,U,s,e,r,G,r,o,u,p,;,N,a,m,e,;,D,e,s,c,r,i,p,t,i,o,n,;,O,w,n,e,r,;,V,i,s,i,b,i,l,i,t,y,;,M,e,m,b,e,r,s ---> heading
i,d,:;,g,r,p,1,;,b,h,a,l,a,j,i,;,a,s,d,f,g,h,;,b,h,a,l,a,j,i,;,p,u,b,l,i,c,;,a,b,c,,d,e,f,,g,h,i
i,d,:;,g,r,p,2,;,b,h,a,l,a,j,i,;,a,s,d,f,g,h,;,b,h,a,l,a,j,i,;,p,r,i,v,a,t,e,;,a,b,c,,d,e,f,,g,h,i
My desired output:
uuid;UserGroup;Name;Description;Owner;Visibility;Members
id:grp1;bhalaji;asdfgh;bhalaji;public;abc,def,ghi
id:grp2;bhalaji;asdfgh;bhalaji;private;abc,def,ghi
I don’t know what is the issue in my code. Can anyone help?
Answers:
https://docs.python.org/3/library/csv.html#csv.csvwriter.writerows
writerows(rows)
takes a list of lists, but in your code it is a list of strings.
I don’t think you need the csv module at all.
with open('new.csv', 'w') as f1:
for row in output:
f1.write(row + 'n')
Taking advantage of range(start, stop, step=1)
step, this becomes pretty easy.
obj = """idNo;UserGroup;Name;Description;Owner;Visibility;Members
id;grp1;bhalaji;asdfgh;bhalaji;public
abc
def
ghi
id;grp2;bhalaji;asdfgh;bhalaji;private
abc
def
ghi
"""
lines = obj.strip().split("n")
output = [lines[0]]
for i in range(1, len(lines), 4):
output.append(lines[i] + ";" + ",".join(lines[i + 1 : i + 4]))
print("n".join(output))
idNo;UserGroup;Name;Description;Owner;Visibility;Members
id:grp1;grp1;bhalaji;asdfgh;bhalaji;public;abc,def,ghi
id:grp2;grp2;bhalaji;asdfgh;bhalaji;private;abc,def,ghi
My problem is after running my code came expected output, but in file contains comma between each words.
My input file:
idNo;UserGroup;Name;Description;Owner;Visibility;Members
id;grp1;bhalaji;asdfgh;bhalaji;public
abc
def
ghi
id;grp2;bhalaji;asdfgh;bhalaji;private
abc
def
ghi
My code:
import csv
output = []
temp = []
currIdLine = ""
with( open ('usergroups.csv', 'r')) as f:
for lines in f.readlines():
line = lines.strip()
if not line:
print("Skipping empty line")
continue
if line.startswith('idNo'): # append the header to the output
output.append(line)
continue
if line.startswith('id'):
if temp:
print(temp)
output.append(currIdLine + ";" + ','.join(temp))
temp.clear()
currIdLine = line
else:
temp.append(line)
output.append(currIdLine + ";" + ','.join(temp))
print("n".join(output))
with open('new.csv', 'w') as f1:
writer = csv.writer(f1)
writer.writerows(output)
Output of the code:
u,u,i,d,;,U,s,e,r,G,r,o,u,p,;,N,a,m,e,;,D,e,s,c,r,i,p,t,i,o,n,;,O,w,n,e,r,;,V,i,s,i,b,i,l,i,t,y,;,M,e,m,b,e,r,s ---> heading
i,d,:;,g,r,p,1,;,b,h,a,l,a,j,i,;,a,s,d,f,g,h,;,b,h,a,l,a,j,i,;,p,u,b,l,i,c,;,a,b,c,,d,e,f,,g,h,i
i,d,:;,g,r,p,2,;,b,h,a,l,a,j,i,;,a,s,d,f,g,h,;,b,h,a,l,a,j,i,;,p,r,i,v,a,t,e,;,a,b,c,,d,e,f,,g,h,i
My desired output:
uuid;UserGroup;Name;Description;Owner;Visibility;Members
id:grp1;bhalaji;asdfgh;bhalaji;public;abc,def,ghi
id:grp2;bhalaji;asdfgh;bhalaji;private;abc,def,ghi
I don’t know what is the issue in my code. Can anyone help?
https://docs.python.org/3/library/csv.html#csv.csvwriter.writerows
writerows(rows)
takes a list of lists, but in your code it is a list of strings.
I don’t think you need the csv module at all.
with open('new.csv', 'w') as f1:
for row in output:
f1.write(row + 'n')
Taking advantage of range(start, stop, step=1)
step, this becomes pretty easy.
obj = """idNo;UserGroup;Name;Description;Owner;Visibility;Members
id;grp1;bhalaji;asdfgh;bhalaji;public
abc
def
ghi
id;grp2;bhalaji;asdfgh;bhalaji;private
abc
def
ghi
"""
lines = obj.strip().split("n")
output = [lines[0]]
for i in range(1, len(lines), 4):
output.append(lines[i] + ";" + ",".join(lines[i + 1 : i + 4]))
print("n".join(output))
idNo;UserGroup;Name;Description;Owner;Visibility;Members
id:grp1;grp1;bhalaji;asdfgh;bhalaji;public;abc,def,ghi
id:grp2;grp2;bhalaji;asdfgh;bhalaji;private;abc,def,ghi