Remove comma in between each word in file - python

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

Related

csvtolist function breaks when i create a new file variable in write mode

from pathlib import Path
from csv import reader
base_folder = Path(__file__).parent.resolve()
csvfilewrite = open(str(base_folder) + "/ourresults.csv", 'w')
csvfilewrite.write("")
csvfilewrite.close()
csvfileapp = open(str(base_folder) + "/ourresults.csv", 'a')
ListOfVals = ['.2.', '[89....1]']
def csvtolist(filename):
with open(filename) as csvfile:
csv_read = reader(csvfile, delimiter=',')
for row in csv_read:
print(row)
ListOfVals.append(str(row).split(",")[0])
return ListOfVals
def returnnumbers(Array): #Array Elements added from the Csv look like this "['2']"
newstr = ''
for i in Array:
for j in range(0, len(i)):
if i[j].isdigit():
newstr += i[j]
newstr += ", "
return newstr
for i in range(1, 21):
csvfileapp.write(str(i) + "\n")
csvtolist(str(base_folder) + "/ourresults.csv")
print(returnnumbers(ListOfVals) + "ugh")
csvfileapp.close()
i use the csvfilewrite variable to clear the csv file when the program opens.
The output i get if i keep it in write mode is
2, 891, ugh
if i either set the csvfilewrite in append mode, or comment out csvfilewrite, the csvfiletolist function will work as i intend. Heres a recreation of what shows up in the terminal in vs code:
...
['20']
2, 891, 1, 2, 3, 4 ... 20, ugh
Why does the csvfiletolist function break like this?

I cannot let the list write in txt (python)

I want to use writelines() to let the list write in txt, but after running, there's nothing in my txt. What's wrong with my code?
Help me if you could. Thank you!
example list(records): [['flower', '200'], ['ham', '60'], ['van', '150']]
I want to write in the txt as below:
flower 200
ham 60
van 50
my code:
def save(initial_money, records): # I need both so don't change.
with open('records.txt', 'w') as f:
first_line = str(initial_money) + "\n"
f.write(first_line)
return initial_money
L = []
for rec, amt in records:
all_rec = str(rec) + " " + str(amt) + "\n"
L.append(all_rec)
f.writelines(records) # must use writelines
return records
This should do:
def save(initial_money, records):
with open('records.txt', 'w') as f:
first_line = str(initial_money) + "\n"
f.write(first_line)
for rec, amt in records:
f.write(str(rec) + " " + str(amt) + "\n")
The first return closes the function, you don't need second return either, records is available as argument.
If you insist on using writelines, you can modify it like below:
def save(initial_money, records):
with open('records.txt', 'w') as f:
first_line = str(initial_money) + "\n"
f.write(first_line)
L = []
for rec, amt in records:
L.append(str(rec) + " " + str(amt) + "\n")
f.writelines(L)
I didn't succeed to understand what you need two arguments.
here is my code:
def save(initial_money,records):
first_line = str(initial_money) + "\n"
with open('records.txt', 'w') as f:
f.writelines(first_line)
for items in records:
f.writelines(f"{items[0]} {items[1]} ")
f.writelines('\n')
records = [['flower', '200'], ['ham', '60'], ['van', '150']]
initial_money = 0
save(initial_money, records)

Keeping the quotes when reading CSV file in python

Truoble with a really annoying homework. I have a csv-file with lots of comma-delimitered fields per row. I need to take the last two fields from every row and write them into a new txt-file. The problem is that some of the latter fields have sentences, those with commas are in double quotes, those without them aren't. For example:
180,easy
240min,"Quite easy, but number 3, wtf?"
300,much easier than the last assignment
I did this and it worked just fine, but the double quotes disappear. The assignment is to copy the fields to the txt-file, use semicolon as delimiter and remove possible line breaks. The text must remain exactly the same. We have an automatic check system, so it's no use arguing if this makes any sense.
import csv
file = open('myfile.csv', 'r')
output= open('mytxt.txt', 'w')
csvr = csv.reader(file)
headline = next(csvr)
for line in csvr:
lgt = len(line)
time = line[lgt - 2].replace('\n', '')
feedb = line[lgt - 1].replace('\n', '')
if time != '' and feedb != '':
output.write(time + ';' + feedb + '\n')
output.close()
file.close()
Is there some easy solution for this? Can I use csv module at all? No one seems to have exactly the same problem.
Thank you all beforehand.
Try this,
import csv
file = open('myfile.csv', 'r')
output= open('mytxt.txt', 'w')
csvr = csv.reader(file)
headline = next(csvr)
for line in csvr:
lgt = len(line)
time = line[lgt - 2].replace('\n', '')
feedb = line[lgt - 1].replace('\n', '')
if time != '' and feedb != '':
if ',' in feedb:
output.write(time + ';"' + feedb + '"\n')
else:
output.write(time + ';' + feedb + '\n')
output.close()
file.close()
Had to do it the ugly way, the file was too irrational. Talked with some collaegues on the same course and apparently the idea was NOT to use csv module here, but to rehearse basic file handling in Python.
file = open('myfile.csv','r')
output = open('mytxt.txt', 'w')
headline = file.readline()
feedb_lst = []
count = 0
for line in file:
if line.startswith('1'): #found out all lines should start with an ID number,
data_lst = line.split(',', 16) #that always starts with '1'
lgt = len(data_lst)
time = data_lst[lgt - 2]
feedb = data_lst[lgt - 1].rstrip()
feedback = [time, feedb]
feedb_lst.append(feedback)
count += 1
else:
feedb_lst[count - 1][1] = feedb_lst[count - 1][1] + line.rstrip()
i = 1
for item in feedb_lst:
if item[0] != '' and item[1] != '':
if i == len(feedb_lst):
output.write(item[0] + ';' + item[1])
else:
output.write(item[0] + ';' + item[1] + '\n')
i += 1
output.close()
file.close()
Thank you for your help!

Create a new .txt file as output with extra text from commands

So I want to write a function annotate() which takes a file name as a parameter and prints it to a new file out_annotated.txt with:
the original text
row number
the total amount of words up to and including that row.
Let's say my .txt file is as following:
hello you
the sun is warm
I like dogs
I want the output to be:
hello you 1 2
the sun is warm 2 6
I like dogs 3 9
The code I used before was
def main():
length = count_rows("file.txt")
print(length)
def count_rows(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
if __name__ == "__main__":
main()
But how do I progress into making a new .txt file with the output including row numbers and the total amount of words?
You can create the name of your output file using os.path:
base, ext = os.path.splitext(fname)
out_path = base + "_annotated" + ext
Now you can open them both: one for reading one for writing, while holding a total words counter. Using enumerate as you did is good to keep track of line numbers, but according to your example you want to start from 1. We will split the lines to count words:
total_words = 0
with open(fname) as f_in, open(out_path, 'w') as f_out:
for line_num, line in enumerate(f_in, start=1):
total_words += len(line.split())
Lastly, because you want to add at the end of each line, you need to avoid the ending '\n', so you can write the lines after you strip them and add the row number and word count:
f_out.write("{} {} {}\n".format(line.strip(), line_num, total_words))
All together we have:
import os
def count_rows(fname):
base, ext = os.path.splitext(fname)
out_path = base + "_annotated" + ext
total_words = 0
with open(fname) as f_in, open(out_path, 'w') as f_out:
for line_num, line in enumerate(f_in, start=1):
total_words += len(line.split())
f_out.write("{} {} {}\n".format(line.strip(), line_num, total_words))
Running this on a file named file.txt with the contents as your example, produces a file called file_annotated.txt with contents of:
hello you 1 2
the sun is warm 2 6
I like dogs 3 9
Something like this will work:
def AppendNumbers(input_file, output_file):
# initialize variables:
total_number_of_words = 0
with open(input_file, 'r') as in_file, open(output_file, 'w+') as out_file:
for line in in_file.readlines():
# get number of words on each line:
number_of_words_per_line = len(line.split(' '))
# add to total word count:
total_number_of_words += number_of_words_per_line
# add words to new line:
new_line = line.replace('\n', '')
new_line = new_line + ' ' + str(number_of_words_per_line) + ' ' + str(total_number_of_words) + '\n'
# write new line to outfile:
out_file.write(new_line)
if __name__ == "__main__":
input_file = 'file.txt'
output_file = 'out_file.txt'
AppendNumbers(input_file, output_file)

How to overwrite a specific set of characters from a specific line in python

So, I'm trying to create a program that will automatically edit a specific set of characters in a file (it will read and replace them). No other data can be moved in the file otherwise it might become corrupted so I need to replace the text in the exact same place as before. I have looked around and found nothing useful but here is my code so far:
l = 3
w = 0
with open("InidCrd000.crd") as myfile:
hexWord = myfile.readlines()[l].split()[w]
codeA = hexWord[58]
codeB = hexWord[59]
print("Current value: ", codeA, codeB)
codeA = " "
codeB = "Ð"
print("New value: ", codeA, codeB)
EDIT - I now have this code (credit - Ilayaraja), which works but then it breaks the file up into lines and places random data in incorrect positions (although the inputted data is in the correct position):
def replace(filepath, lineno, position, newchar):
with open(filepath, "r") as reader:
lines = reader.readlines()
l = lines[lineno-1]
l = l[0:position] + newchar + l[position+1:]
lines[lineno-1] = l
with open(filepath, "w") as writer:
writer.writelines(lines)
replace("InidCrd000.crd", 4, 57, "")
replace("InidCrd000.crd", 4, 58, "Ð")
If you want the file for testing, here it is: 1drv.ms/u/s!AqRsP9xMA0g1iqMl-ZQbXUqX2WY8aA (It's a onedrive file)
first find the code you want to change using this :
l = 3
w = 0
with open("InidCrd000.crd") as myfile:
hexWord = myfile.readlines()[l].split()[w]
codeA = hexWord[58]
codeB = hexWord[59]
myfile.close()
then change like this :
import fileinput
with fileinput.FileInput(fileToSearch, inplace=True, backup='.bak') as file:
for line in file:
line.replace(codeA, textToReplace)
Define a function with the arguments the path of the file(filepath), line number(lineno 1 to N), position of the character in the line(position 0 to N) and the new character to be overwritten(newchar) as follows:
def replace(filepath, lineno, position, newchar):
with open(filepath, "r") as reader:
lines = reader.readlines()
l = lines[lineno-1]
l = l[0:position] + newchar + l[position+1:]
lines[lineno-1] = l
with open(filepath, "w") as writer:
writer.writelines(lines)
You can call the function as follows to replace the characters:
replace("InidCrd000.crd", 3, 58, " ")
replace("InidCrd000.crd", 3, 59, "Ð")

Categories

Resources