Python字典到CSV文件,但作为(键:值)分隔行
因此,我创建了一个爬虫字典,然后检查html文件并查找指向其他html文件的“href=”短语 因此,字典如下所示:Python字典到CSV文件,但作为(键:值)分隔行,python,html,Python,Html,因此,我创建了一个爬虫字典,然后检查html文件并查找指向其他html文件的“href=”短语 因此,字典如下所示: {'1.html': ['2.html', '3.html'], '2.html': ['3.html', '4.html'], '3.html': ['5.html', '7.html'], '5.html': [], '7.html': ['2.html'], '4.html': ['6.html'], '6.html': ['2.html']} def print_dic
{'1.html': ['2.html', '3.html'], '2.html': ['3.html', '4.html'], '3.html': ['5.html', '7.html'], '5.html': [], '7.html': ['2.html'], '4.html': ['6.html'], '6.html': ['2.html']}
def print_dict(d, file):
for key in d.keys():
file.write(key)
for item in d[key]:
file.write("," + item)
file.write("\n")
我想从这个字典创建一个CSV文件。
我希望将其组织为每个键的分隔线:此设计的值:
key,value1a,value1b
key2,value2a,value2b
例如:
1.html,2.html,3.html
2.html,3.html,4.html
and etc
这应该很容易,我确实创建了一个csv文件,看起来像:
> 1.html,['2.html', '3.html']
> 2.html,['3.html', '4.html']
> 3.html,['5.html', '7.html']
> 5.html,[]
> 7.html,['2.html']
> 4.html,['6.html']
> 6.html,['2.html']
使用代码:
with open('my_file.csv', 'w') as f:
[f.write('{0},{1}\n'.format(key, value)) for key, value in dic.items()]
但是现在我必须删除括号和键后的“,”而不附加任何值(例如:5.html)
所以我想做一个if语句来检查这个值是否存在。
如果有,打印将按“键,值”
如果没有,打印将只按“键”
然后我想写一个循环,遍历每一行,检查括号并删除它们。
是的,我知道我可能写错了,所以我很高兴知道我的错误在哪里。
谢谢。使用
str.join
Ex:
[f.write('{0},{1}\n'.format(key, ",".join(value))) for key, value in dic.items()]
使用
str.join
Ex:
[f.write('{0},{1}\n'.format(key, ",".join(value))) for key, value in dic.items()]
使用键的join命令,如下所示:
with open('file.csv', 'w') as f:
for key, value in dic.items:
if value:
f.write('{0},{1}\n'.format(key, ",".join(value)))
else:
f.write('{0}\n'.format(key))
使用键的join命令,如下所示:
with open('file.csv', 'w') as f:
for key, value in dic.items:
if value:
f.write('{0},{1}\n'.format(key, ",".join(value)))
else:
f.write('{0}\n'.format(key))
你可以这样写:
{'1.html': ['2.html', '3.html'], '2.html': ['3.html', '4.html'], '3.html': ['5.html', '7.html'], '5.html': [], '7.html': ['2.html'], '4.html': ['6.html'], '6.html': ['2.html']}
def print_dict(d, file):
for key in d.keys():
file.write(key)
for item in d[key]:
file.write("," + item)
file.write("\n")
其中file是您创建的文件,d是您创建的字典
它将打印所需的输出您可以这样写:
{'1.html': ['2.html', '3.html'], '2.html': ['3.html', '4.html'], '3.html': ['5.html', '7.html'], '5.html': [], '7.html': ['2.html'], '4.html': ['6.html'], '6.html': ['2.html']}
def print_dict(d, file):
for key in d.keys():
file.write(key)
for item in d[key]:
file.write("," + item)
file.write("\n")
其中file是您创建的文件,d是您创建的字典
并且它将打印所需的输出我将使用
打印
功能执行以下任务:
dct = {'1.html': ['2.html', '3.html'], '2.html': ['3.html', '4.html'], '3.html': ['5.html', '7.html'], '5.html': [], '7.html': ['2.html'], '4.html': ['6.html'], '6.html': ['2.html']}
with open('my_file.csv', 'w') as f:
for k,v in dct.items():
print(k, *v, sep=',', file=f)
my_file.csv
内容:
1.html,2.html,3.html
2.html,3.html,4.html
3.html,5.html,7.html
5.html
7.html,2.html
4.html,6.html
6.html,2.html
*
表示所谓的拆包我将使用打印
功能来完成以下任务:
dct = {'1.html': ['2.html', '3.html'], '2.html': ['3.html', '4.html'], '3.html': ['5.html', '7.html'], '5.html': [], '7.html': ['2.html'], '4.html': ['6.html'], '6.html': ['2.html']}
with open('my_file.csv', 'w') as f:
for k,v in dct.items():
print(k, *v, sep=',', file=f)
my_file.csv
内容:
1.html,2.html,3.html
2.html,3.html,4.html
3.html,5.html,7.html
5.html
7.html,2.html
4.html,6.html
6.html,2.html
*
表示所谓的拆包不要对侧效应使用列表理解也不要对侧效应经常使用列表理解。我不知道“”在python中的含义,但我读到了它,几乎完全理解了您的答案。谢谢我还不太清楚“”是什么意思。非常感谢。我不知道“”在python中的含义,但我读到了它,几乎完全理解了您的答案。谢谢我还不太清楚“”是什么意思。谢谢!很高兴知道,我至少知道如何定义if语句,但只在“join”方面出错了。这很有效。谢谢,如果你发现你的问题得到了回答,请留下一张向上的选票,然后结束这个问题。谢谢!很高兴知道,我至少知道如何定义if语句,但只在“join”方面出错了。这是有效的。谢谢,如果你发现你的问题得到了回答,请留下一张向上的选票,然后结束这个问题。