how to remove List brackets in a csv file which was generated from a dictionary? - python-3.x

I am trying something similar-
sample={"name":["age","number","email"]}
#dictionary stores the relevant data in above format
with open('selected.csv','w') as csvf:
[csvf.write('{0},{1}\n'.format(key,value)) for key,value in
sample.items()]
#writing data in a csv file with my formatting
"Aaron",[21,020303030,"Aaron#blahblah.com"]
#csv file sample entry
Everything works fine but CSV file shows List brackets, how can I remove them?

This isn't difficult if you use the standard library csv module:
import csv
sample={"name":["age","number","email"]}
with open('selected.csv', 'w+', newline='') as csvf:
writer = csv.writer(csvf)
for k, v in sample.items():
writer.writerow([k, *v]) # unpack v into a list with k
This would produce a file with one line:
name,age,number,email

Related

list not split into proper csv columns using python

I wrote the following code to split my data matrix into a csv file:
f = open('midi_data.csv', 'w', newline="")
writer = csv.writer(f, delimiter= ',',quotechar =',',quoting=csv.QUOTE_MINIMAL)
for item in data:
writer.writerow(item)
print(item)
f.close()
But the csv file ends up looking like this:
tuples not separated by columns but by commas in one column only
What am I doing wrong?
The data seems to be written correctly inside the tuples, because when running the code it outputs the following:
enter image description here

python txt file to multiple txt files

I have a single txt file which contains multiple data samples in the form
ID-001
some data
ID-002
some other data
ID-003
some more data
and so on. Everything separated with an ID-i is a new data set, the IDs are unique, and the file is about 2000 lines.
I want to create a python script that will open the first file and create MULTIPLE txt files which will contain everything between ID-(i-1) to ID-i no matter how many different data samples there are in the file.
Any ideas?
You could use a regex like so:
import re
pat=r'^(ID-\d+)$([\s\S]+?)(?=(?:^ID-\d+)|\Z)'
with open(ur_file) as f:
for m in re.finditer(pat, f.read(), flags=re.M):
print(f'{m.group(2)}' )
Prints:
some data
some other data
some more data
m.group(1) will have the ID-xxx and you could use that to write each block into a file.
Or, you can split the block into data blocks like so:
import re
with open(ur_file) as f:
print([b for b in re.split(r'^ID-\d+', f.read(), flags=re.M) if b])
Prints:
['\nsome data\n\n', '\nsome other data\n\n', '\nsome more data\n']
Or you can use re.findall like so:
import re
pat=r'^(ID-\d+)$([\s\S]+?)(?=(?:^ID-\d+)|\Z)'
with open(ur_file) as f:
print(re.findall(pat, f.read(), flags=re.M))
Prints:
[('ID-001', '\nsome data\n\n'), ('ID-002', '\nsome other data\n\n'), ('ID-003', '\nsome more data\n')]
Again, you can use that tuple data to write into separate files.

How to write a list of floats to csv in columns?

i am searching everywhere for a method to write a list of floats into csv but must be in column format.
My code for writing csv as follow:
csvfile=open('Test.csv','w', newline='')
obj=csv.writer(csvfile)
obj.writerow(list_dis_B1_avg)
csvfile.close()
It turn out that the floats are written in rows.
I have a list of floats stored under "list_dis_B1_avg"
How can i just write it in column?
You dont need any csv module to do that:
with open("Test.csv", "w") as f: # use with to close the file in any case
f.write("\n".join(list_dis_B1_avg)) # newline between the elements
More about the with keyword: https://www.geeksforgeeks.org/with-statement-in-python/
More about str.join(): https://www.programiz.com/python-programming/methods/string/join

Original order of columns in csv not retained in unicodecsv.DictReader

I am trying read a CSV file into python 3 using unicodecsv library. Code follows :
with open('filename.csv', 'rb') as f:
reader = unicodecsv.DictReader(f)
Student_Data = list(reader)
But the order of the columns in the CSV file is not retained when I output any element from the Student_Data. The output contains any random order of the columns. Is there anything wrong with the code? How do I fix this?
As stated in csv.DictReader documentation, the DictReader object behaves like a dict - so it is not ordered.
You can obtain the list of the fieldnames with:
reader.fieldnames
But if you only want to obtain a list of the field values, in original order, you can just use a normal reader:
with open('filename.csv', 'rb') as f:
reader = unicodecsv.reader(f)
for row in reader:
Student_Data = row

How to convert a tab delimited text file to a csv file in Python

I have the following problem:
I want to convert a tab delimited text file to a csv file. The text file is the SentiWS dictionary which I want to use for a sentiment analysis ( https://github.com/MechLabEngineering/Tatort-Analyzer-ME/tree/master/SentiWS_v1.8c ).
The code I used to do this is the following:
txt_file = r"SentiWS_v1.8c_Positive.txt"
csv_file = r"NewProcessedDoc.csv"
in_txt = csv.reader(open(txt_file, "r"), delimiter = '\t')
out_csv = csv.writer(open(csv_file, 'w'))
out_csv.writerows(in_txt)
This code writes everything in one row but I need the data to be in three rows as normally intended from the file itself. There is also a blank line under each data and I don´t know why.
I want the data to be in this form:
Row1 Row2 Row3
Word Data Words
Word Data Words
instead of
Row1
Word,Data,Words
Word,Data,Words
Can anyone help me?
import pandas
It will convert tab delimiter text file into dataframe
dataframe = pandas.read_csv("SentiWS_v1.8c_Positive.txt",delimiter="\t")
Write dataframe into CSV
dataframe.to_csv("NewProcessedDoc.csv", encoding='utf-8', index=False)
Try this:
import csv
txt_file = r"SentiWS_v1.8c_Positive.txt"
csv_file = r"NewProcessedDoc.csv"
with open(txt_file, "r") as in_text:
in_reader = csv.reader(in_text, delimiter = '\t')
with open(csv_file, "w") as out_csv:
out_writer = csv.writer(out_csv, newline='')
for row in in_reader:
out_writer.writerow(row)
There is also a blank line under each data and I don´t know why.
You're probably using a file created or edited in a Windows-based text editor. According to the Python 3 csv module docs:
If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.

Resources