[英]write python dictionary to a file
I'm trying to split a csv file into multiple files, preserving the header each time: So if my current original file is : 我试图将一个csv文件拆分为多个文件,每次都保留标题:因此,如果我当前的原始文件是:
<t> ID Name Position <br> 1 Jose Engineer <br> 2 Maria Developer <br> 3 Keith Manager</t>
I want to split it into 3 files : 我想将其分为3个文件:
File1.txt
<t> ID Name Position <br> 1 Jose Engineer <br>
File2.txt
<t> ID Name Position <br> 2 Maria Developer <br>
File3.txt
<t> ID Name Position <br> 3 Keith Manager <br>
I am able to build the key-pair dictionary, but if I use 我可以构建密钥对字典,但是如果我使用
json.dump(keys, file_ptr, ensure_ascii=False
I get unwanted [
and ]
at the beginning and end of keys' list. 在键列表的开头和结尾,我得到了不必要的[
和]
。
I also tried 我也试过
writer = csv.writer(file_ptr)
writer.writerows(keys)
which splits each letter of the key like N
, a
, m
, e
, and so on. 它会拆分键的每个字母,例如N
, a
, m
, e
等。
Any suggestions? 有什么建议么?
Also, is it possible to avoid iterating over the keys in the dictionary while printing the corresponding values? 另外,是否可以避免在打印相应值时迭代字典中的键?
Change your csv code to use single row: 更改您的csv代码以使用单行:
writer.writerow(keys)
or wrap the keys into a list: 或将密钥包装到列表中:
writer.writerows([keys])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.