【发布时间】:2018-06-06 03:44:27
【问题描述】:
我希望将 test.txt 中的数据添加到 result.csv 的“结果”列下。 我怎样才能完成任务?我尝试了很多方法,但我无法完成。
顺便说一句,如果两个“运行图像”可以在一个单元格中,那就太好了。该行应为“版本”。
test.txt
Running Image: SMM_2x10G
Running Image: SMM_2x10G
C100G
SystemUptime:1d,19h,40m,30s
CPU utilization is normal with 96.1 idle
Memory is with good with 50.00%
结果.csv
Content Results
Version
Product
System Uptime
CPU status
MemFree/MemTotal
最终的结果应该是这样的:
Content Results
Version " Running Image: SMM_2x10G
Running Image: SMM_2x10G"
Product C100G
System Uptime SystemUptime:1d,19h,40m,30s
CPU status CPU utilization is normal with 96.1 idle
MemFree/MemTotal Memory is with good with 50.00%
提前谢谢你!
我想我找到了一种方法: 代码如下:
import pandas,sys
import pandas as pd
df1=pd.read_csv('result.csv')
df2=pd.read_csv('test.csv')
df3=pd.concat([df1,df2],axis=1)
print(df3)
with open('test-result.csv','w',encoding="utf-8") as f:
df3.to_csv(f,index=False)
然后我查看了我之前的代码并遇到了问题。实际上 test.csv 是从包含大量数据的日志文件中提取的。我使用“for...if..” 得到想要的数据。如果在日志中找不到数据,我希望它最终在“test.csv”中打印一个空白行,但是使用“for ...if...else”,我有很多空白行.... ..
for line in fp:
if "Unknown(255) Unknown" in line:
Unknown = line_stream
print("The module is in unknown status %s:" % line_stream, file=fp1)
else:
print("\n",file=fp1)
知道如何通过google解决....谢谢。
【问题讨论】:
-
用您尝试过但无效的方法更新您的问题。
-
您能发布一个示例,说明 result.csv 应该是什么样的吗?在我看来,您提供的架构有两列和几行,反之亦然。
-
不知道怎么把csv粘贴到comment..text.txt里面的数据应该都在result.csv的Result列下。我对csv很新鲜。我知道如何使用新的 csv 在列中添加数据,但不知道如何在现有的 csv 中添加...你能帮我吗?非常感谢。
-
要添加到现有的 csv 文件,以“追加”模式打开 csv 文件:
with open("filename.csv", "a") as f: -
如何在“结果”列下添加数据?谢谢。