Using python csv writer without quotations

前端 未结 4 936
猫巷女王i
猫巷女王i 2020-12-31 11:47

I\'m trying to write a list of strings like below to a file separated by the given delimiter.

res = [u\'123\', u\'hello world\']

When I try

4条回答
  •  悲哀的现实
    2020-12-31 12:46

    What worked for me was using a regular writer, not the csv.writer, and simply use your delimiter in between columns ('\t' in my case):

    with open(target_path, 'w', encoding='utf-8') as fd:
         # some code iterating over a pandas daftaframe called mydf     
         # create a string out of column 0, '\t' (tab delimiter) and column 1:
         output = mydf.loc[i][0] + '\t' + mydf.loc[i][1] +'\n'
         # write that output string (line) to the file in every iteration
         fd.write(output)
    

    It might not be the "correct" way but it definitely kept the original lines in my project, which included many strings and quotations.

提交回复
热议问题