1

EDIT* 解决方案是在列中换行。这将恢复原始格式。

我正在尝试使用 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()

示例输入(全在一个单元格中):

#
# 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

4

1 回答 1

0
  1. 将 .CSV 转换为 .XLSX
  2. 在 Excel 中,右键单击包含丢失格式的数据的列
  3. 选择“格式化单元格...”
  4. 选择“对齐”选项卡
  5. 检查“换行”
  6. 一切都很好!
于 2013-06-14T16:18:16.480 回答