Write a Nested python list into csv file - python

I have below code to write my nested list into a csv file. The nested list looks like this
[['19181011', '13041519', '22121605', '11142007', '23000114'],
['1523141612', '2403051513', '0806022324', '1614012422', '0516121805'],
['23201621', '24171811', '08231524', '16011022', '17131220'],
['2317241822', '2220112421', '1124052211', '1010192318', '2108231524'],
['11220215', '24240507', '19180423', '07081422', '21201224']]
with open('MLpredictions.csv', 'w') as f:
writer = csv.writer(f, delimiter=';', lineterminator='\n')
writer.writerows(high5_pred)
But when i execute this code, i get like below in the csv file:
19181011;13041519;22121605;11142007;23000114
1523141612;2403051513;0806022324;1614012422;0516121805....
i changed the delimiter to ',' but then I get 5 different columns.
I want each list to be 1 row separated by ',' and not ';'.
Expected o/p, a single column:
19181011,13041519,22121605,11142007,23000114
1523141612,2403051513,0806022324,1614012422,0516121805
Any ideas how to do this?

Assuming that there is a specific reason why you want the data all in one column:
The reason you're getting seperate columns is because you're using the csv format, and your data is not escaped. Your raw file looks like this:
19181011,13041519,22121605,11142007,23000114
1523141612,2403051513,0806022324,1614012422,0516121805
but you need it to look like this:
"19181011,13041519,22121605,11142007,23000114"
"1523141612,2403051513,0806022324,1614012422,0516121805"
You're probably best to create a string object for each "row" of your output file. I'd do the following:
with open('MLpredictions.csv', 'w') as f:
writer = csv.writer(f, delimiter=';', lineterminator='\n')
rows = [','.join([str(number) for number in row]) for row in high5_pred]
writer.writerows(rows)
Note: unless you have a good reason why you don't want these numbers in different columns, I'd leave your code as is. It will be a lot easier to deal with the native csv format

Related

Create and append CSV row by row

I would just like to create a csv file and at the same time add my data row by row with a for loop.
for x in y:
newRow = "\n%s,%s\n" % (sentence1, sentence2)
with open('Mydata.csv', "a") as f:
f.write(newRow)
After the above process, I tried to read the csv file but I can't separate the columns. It seems that there is only one column, maybe I did something wrong in the csv creation process?
colnames = ['A_sentence', 'B_sentence']
Mydata = pd.read_csv(Mydata, names=colnames, delimiter=";")
print(Mydata['A_sntence']) #output Nan
When you are writing the file, it looks like you are using commas as separators, but when reading the file you are using semicolons (probably just a typo). Change delimiter=";" to delimiter="," and it should work.

Write a list of values as separate columns to a CSV file using Python

I am trying to write the elements of a list (each time I get from a for loop) as individual columns to a CSV file. But the elements are adding as an individual rows, but not columns.
I am new to python. What needs to be changed in my code? Please tell me
import csv
row=['ABC','XYZ','','','ABCD',''] #my list looks like this every time
with open('some.csv', 'w') as writeFile:
writer = csv.writer(writeFile, delimiter=',', quotechar='"')
for item in row:
writer.writerow([item])
But I am getting the output as below:
ABC
XYZ
ABCD
My expected output is a below:
ABC XYZ ABCD
You are iterating over the list row and writing each element as a new row in your code when you do writer.writerow([item]).
Instead you want to write the entire row in one line using writer.writerow
import csv
row=['ABC','XYZ','','','ABCD',''] #my list looks like this every time
with open('some.csv', 'w') as writeFile:
writer = csv.writer(writeFile, delimiter=',', quotechar='"')
#Write the list in one row
writer.writerow(row)
The file will look like
ABC,XYZ,,,ABCD,
Actually you are writing in different rows, you don't need to do that. You can write your whole list as a single row. You need spaces so for that you can modify your list.

How to replace one value with another in a csv file?

I have a CSV file with information and want to replace the information in a specific location with a new value.
For example if my CSV file looks like this:
example1,example2,0
example3,example4,0
exampple5,example6,0
Note that each row is labelled for example:
test = row[0]
test1 = row[1]
test2 = row[2]
If I want to replace
test[0]
with a new value how would I go about doing it?
Simplest way without installing any additional package would be to use built-in csv to read the whole file in a matrix and replace the desired element.
Here is code that would do just that:
import csv
with open('test.csv', 'r') as in_file, open('test_out.csv', 'wb') as out_file:
data = [row for row in csv.reader(in_file)]
data[0][0] = 'new value'
writer = csv.writer(out_file)
writer.writerows(data)
There are a handful of ways to do this, but personally I'm a big fan of pandas. With pandas, you can read a csv file with df = pd.read_csv('path_to_file.csv'). Make changes however you want, if you wanted row 1 column 1, you'd use df.loc[0,0] = new_val. Then when you are done save to the same file df.to_csv('path_to_file.csv').

Syntax - saving a dictionary as a csv file

I am trying to "clean up" some data - I'm creating a dictionary of the channels that I need to keep and then I've got an if block to create a second dictionary with the correct rounding.
Dictionary looks like this:
{'time, s': (imported array), 'x temp, C':(imported array),
'x pressure, kPa': (diff. imported array).....etc}
Each imported array is 1-d.
I was looking at this example, but I didn't quite get the way to parse it so that I ended up with what I want.
My desired output is a csv file (do not care if the delimiter is spaces or commas or whatever) with the first row being the keys and the subsequent rows simply being the values.
I feel like what I'm missing is how to use the map function properly.
Also, I'm wondering if I'm using DictWriter when I should be using DictReader.
This is what I originally tried:
with open((filename), 'wb') as outfile:
write = csv.DictWriter(outfile, Fieldname_order)
write.writer.writerow(Fieldname_order)
write.writerows(data)
DictWriter's API doesn't match the data structure you have. DictWriter requires list of dictionaries. You have a dictionary of lists.
You can use the ordinary csv.writer:
my_data = {'time, s': [0,1,2,3], 'x temp, C':[0,10,20,30],
'x pressure, kPa': [0,100,200,300]}
import csv
with open('outfile.csv', 'w') as outfile:
writer = csv.writer(outfile)
writer.writerow(my_data.keys())
writer.writerows(zip(*my_data.values()))
That will write the columns in arbitrary order, which order may change from run to run. One way to make the order to be consistent is to replace the last two lines with:
writer.writerow(sorted(my_data.keys()))
writer.writerows(zip(*(my_data[k] for k in sorted(my_data.keys()))))
Edit: in this example data is a list of dictionaries. Each row in the csv contains one value for each key.
To write your dictionary with a header row and then data rows:
with open(filename, 'wb') as outfile:
writer = csv.DictWriter(outfile, fieldnames)
writer.writeheader()
writer.writerows(data)
To read in data as a dictionary then you do need to use DictReader:
with open(filename, 'r') as infile:
reader = csv.DictReader(infile)
data = [row for row in reader]

How to avoid splitting on a delimiter if it appears inside quotes?

I have problem in splitting data. I have data as follows in CSV file:
"a";"b";"c;d";"e"
The problem is when I used line.split(";") function, it splits even between c and d. I don't want c and d to be separated. Later I need to store these four values in four different columns in a table, but using this function I get five different columns.
I want the results to be "a" "b" "cd" "e".
I tried with line.split('";"'), but it did not help.
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';')
for row in reader:
print row
Try this out.
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';', quoting=csv.QUOTE_NONE )
for row in reader:
print row
This ^^^ if you want quotes preserved
Edit: If you want ';' removed from the field content ('c;d' = 'cd' case) - you may do the post processing on rows returned, something like this:
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';', quoting=csv.QUOTE_NONE )
for row in reader:
print [item.replace(';', '') for item in row]
In other contexts, the shlex.split() function could be used

Categories