Sign up to take part
Registered users can ask their own questions, contribute to discussions, and be part of the Community!
I have a python recipe that export a dataset to a csv.
But I need to modify the first row of this csv and its extension.
I'm trying to do this (read the csv and write a new first line) after exporting the dataset but i can't read it with the library csv.
Could you help me ?
# Recipe outputs managed_folder_id = "pourImport" output_folder = dataiku.Folder(managed_folder_id) output_folder.upload_data(filename, analyses_df.to_csv(index=False, header=True, sep=";").encode("utf-8")) with output_folder.get_download_stream(filename) as stream: with open(stream, 'r', newline='') as csvfile: # read csv reader = csv.reader('/'+csvfile, delimiter=';') lignes = list(reader) # first row new_first_row = ['2','toto'] lignes = new_first_row # write csv with open(stream, 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter=';') for ligne in lignes: writer.writerow(ligne) TypeError: expected str, bytes or os.PathLike object, not HTTPResponse
This makes no sense to me. Why would you write it in the first place if you are going to modify later? Why not write it correctly in the first place? Exactly what are you trying to achieve? What is the problem with the file that requires you to write it twice? Thanks
The new first row has only 2 features while the folowing rows have 30 features.
The pandas .to_csv function adds separators at the first row.
I need a fisrt row like this
and not like this
Again you have not explined what your requirement is just what you think it should be done. I can't think of a reason as to why you would want to have a custom format CSV file. It is normal for empty columns to show like that, otherwise how would you know which column is which piece of data? In your sample the first two columns are fine the rest are empty but what if the empty columns are at the middle of the row?
Then scrap what you got and write the file correctly in the first place. Here is how you can iterate rows of a dataframe:
for index, row in df.iterrows(): print(row['c1'], row['c2'])
Thank you for your help,
I don't know how to directly write a csv in the output folder of the recipe without using the function df.to_csv().
can you help me again ?