Print array to a file Ask Question. Asked 11 years, 9 months ago. Active 1 year ago. Viewed k times. I would like to print an array to a file. I would like the file to look exactly similar like how a code like this looks.
Is there any one lines solution for this rather than regular for each look. Gordon k 69 69 gold badges silver badges bronze badges. Atif Atif Add a comment. Active Oldest Votes. Gordon Gordon k 69 69 gold badges silver badges bronze badges. Show 3 more comments. Felix Kling Felix Kling k gold badges silver badges bronze badges.
Sarfraz Sarfraz k 70 70 gold badges silver badges bronze badges. Ahmad Ahmad 4, 8 8 gold badges 26 26 silver badges 39 39 bronze badges. I just wrote this function to output an array as text: Should output nicely formatted array. This script was created for internal use. This is not fool proof and should be used with trusted data only. Would be near impossible to do that with serialized output Not the cleanest function but it gets the job done. Method 1: Using File handling Creating a text file using the in-built open function and then converting the array into string and writing it into the text file using the write function.
Finally closing the file using close function. Skip to content. Change Language. Related Articles. Table of Contents. Improve Article. Save Article.
Like Article. Last Updated : 18 Jun, Okay, I'm trying to clean up some of my code. I had been storing values in their own individual text files, but that's too cluttered. So I'm thinking an array is the way to go, I can easily use file ; to read the data in as an array, but my problem is writing it back to the file when I'm done manipulating it.
The best I could figure out is Code:. Joined Nov 23, Messages 3, MadJuggla9 2[H]4U. Joined Oct 9, Messages 3, Click to expand Joined Feb 10, Messages 5, Note: Different operating system families have different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character s for your operating system.
If you use the wrong line ending characters when writing your files, you might find that other applications that open those files will "look funny". In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
0コメント