编辑* 解决方案是在列中包装文本。这将恢复原始格式。
我正在尝试使用Python提供的CSV模块创建CSV。我的问题是,当创建CSV时,插入到字段中的文件的内容会丢失它的格式。
示例输入可以从'whois 8.8.8.8'中提取。我希望该字段保存该输入的格式。
有没有办法在单元格内维护文件的原始格式?
#!/usr/bin/python
import sys
import csv
file1 = sys.argv[1]
file2 = sys.argv[2]
myfile1 = open(file1, "rb")
myfile2 = open(file2, "rb")
ofile = open('information.csv', "wb")
stuffwriter = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
stuffwriter.writerow([myfile1.read(),myfile2.read()])
myfile1.close()
myfile2.close()
ofile.close()
示例输入(All In One Cell):
#
# ARIN WHOIS data and services are subject to the Terms of Use
# available at: https://www.arin.net/whois_tou.html
#
#
# Query terms are ambiguous. The query is assumed to be:
# "n 8.8.8.8"
#
# Use "?" to get help.
#
#
# The following results may also be obtained via:
# http://whois.arin.net/rest/nets;q=8.8.8.8?showDetails=true&showARIN=false&ext=netref2
#
Level 3 Communications, Inc. LVLT-ORG-8-8 (NET-8-0-0-0-1) 8.0.0.0 - 8.255.255.255
Google Incorporated LVLT-GOOGL-1-8-8-8 (NET-8-8-8-0-1) 8.8.8.0 - 8.8.8.255
#
# ARIN WHOIS data and services are subject to the Terms of Use
# available at: https://www.arin.net/whois_tou.html
#
希望单元格保持上面的格式。目前,当我在Excel中打开时,它就是一行。
我从执行中获取数据:
whois 8.8.8.8 > inputData.txt
echo "8.8.8.8 - Google" > inputData2.txt
python CreateCSV inputData2.txt inputData.txt
这是我想看到的: http://www.2shared.com/photo/WZwDC7w2/Screen_Shot_2013-06-06_at_1231.html
这就是我所看到的: http://www.2shared.com/photo/9dRFGCxh/Screen_Shot_2013-06-06_at_1222.html
答案 0 :(得分:0)