Help appending a slice to next row in CSV file

agolangf · · 525 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi all, I&#39;m trying to build out an error tracking CSV file in a program I&#39;m writing. I&#39;ve been able to figure out how to build a CSV file with a 2d slice in one go, but I can&#39;t find any simple way to append a single slice to the next line of the CSV file so I can log content into the file WHILE the program runs rather than in one big go at the end. It&#39;s important to log with each action rather than when all the processing is done so if the program crashes I have record of any errors up to the point of the crash.</p> <p>Any thoughts? This must be pretty simple and I&#39;m sure the need for something like this must be somewhat common.</p> <p>Thanks in advance as always!! :)</p> <p>Update: thanks to <a href="/u/earthboundkid" rel="nofollow">u/earthboundkid</a> for a great simple solution, here: <a href="http://play.golang.org/p/CzFNG1eDec" rel="nofollow">http://play.golang.org/p/CzFNG1eDec</a></p> <p>I modified it a little because it was writing each string 10 times due to the for loop; in my case, I only wanted it to write one slice to the csv file once. This way it can easily be called at any time to append a slice to the file... perfect for creating a simple log file that updates in real-time :)</p> <pre><code>//csvWriter appends a slice of strings to a CSV file func csvWriter(yourSliceGoesHere []string) { f, err := os.OpenFile(&#34;yourFile.csv&#34;, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) if err != nil { fmt.Println(&#34;Error: &#34;, err) return } w := csv.NewWriter(f) w.Write(yourSliceGoesHere) w.Flush() } </code></pre> <hr/>**评论:**<br/><br/>earthboundkid: <pre><p><a href="http://play.golang.org/p/CzFNG1eDec" rel="nofollow">http://play.golang.org/p/CzFNG1eDec</a> appears to work for me.</p></pre>Rabiesalad: <pre><p>I love you. That&#39;s so simple and perfect. Appears to work well in my testing too. I&#39;ll update the OP with this link.</p> <p>THANKS!!</p></pre>dchapes: <pre><p>Your edit to the original post seems to have missed the point. Do <strong>not</strong> re-open the file each time.</p> <p><em>Once</em>:</p> <ul> <li><p>open the file, use <code>os.O_APPEND</code> or seek to the end</p></li> <li><p>make a <code>csv.Writer</code> wrap that</p></li> </ul> <p>Then, as needed:</p> <ul> <li>call the <code>csv.Writer</code>&#39;s <code>Write</code> method and possibly the <code>Flush</code> method</li> </ul></pre>djherbis: <pre><p>The CSV Writer should do this: <a href="https://golang.org/pkg/encoding/csv/#Writer.Write" rel="nofollow">https://golang.org/pkg/encoding/csv/#Writer.Write</a></p> <p>If you&#39;re doing this and not seeing the data in the file, maybe you just need to call Flush().</p></pre>Rabiesalad: <pre><p>Thanks! I tried using the writer, but it seems to want to overwrite the whole document rather than append to a new line whenever I use it...</p> <p>Any further direction would be much appreciated!</p></pre>djherbis: <pre><p>Can you post a code snippet on the playground?</p></pre>earthboundkid: <pre><p>How did you open the file that you passed into the Writer?</p></pre>Rabiesalad: <pre><p>you can see the solution posted in the OP now;</p> <p>originally I was using os.create and os.open, which it seems don&#39;t provide the same options as os.OpenFile which allows you to set the append parameter.</p> <p>All the examples I was seeing used os.create and os.open; maybe my lack of experience is the cause of me not realizing what I needed to look up was more along the lines of &#34;how to open files in append mode&#34; rather than &#34;how to append a slice to csv&#34; which really didn&#39;t provide useful results. I&#39;m betting if I had more experience in other languages this idea would have been more obvious to me...</p> <p>Thanks for checking in :)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

525 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传