我有一个要求,我需要在我的文本文件转换成csv和使用python来做。我的文本文件是这样的
Employee Name : XXXXX
Employee Number : 12345
Age : 45
Hobbies: Tennis
Employee Name: xxx
Employee Number :123456
Hobbies : Football
我希望我的CSV文件的列名为Employee Name、Employee Number、Age和hobients,当一个特定的值不存在时,它应该在那个特定的地方有一个值NA。有什么简单的解决办法吗?提前谢谢
也许这有助于你开始?它只是第一个雇员数据的静态输出。现在您需要将其包装成文件上的某种迭代。很有可能有一个更优雅的解决方案,但这是在没有一个import语句的情况下实现它的方法;)
你可以这样做:
当然,这个方法假设每个记录中至少有
Employee Name
字段。在为此,我遵循了非常简单的步骤,可能不是最优的,但解决了问题。这里我能看到的重要情况是,在一个文件中可以有多个密钥(“雇员姓名”等)。 台阶
下面是代码
输出:
^{pr2}$我希望这有帮助。在
相关问题 更多 >
编程相关推荐