将文本转换为python中的数字

时间:2017-05-10 20:10:43

标签: python linux excel bash

输入是一个文本表 - 有些列是数字和一些文本。我有python脚本将此文本文件转换为xlsx。在Excel或Libre中打开后,所有字段都是文本。有没有选择将某些列转换为数字?

cat script.py:

import csv
import sys
from xlsxwriter.workbook import Workbook

# Add some command-line logic to read the file names.
tsv_file = sys.argv[1]
xlsx_file = sys.argv[2]

# Create an XlsxWriter workbook object and add a worksheet.
workbook = Workbook(xlsx_file)
worksheet = workbook.add_worksheet()

# Create a TSV file reader.
tsv_reader = csv.reader(open(tsv_file, 'rb'), delimiter='\t')

# Read the row data from the TSV file and write it to the XLSX file.
for row, data in enumerate(tsv_reader):
    worksheet.write_row(row, 0, data)

# Close the XLSX file.
workbook.close()

运行脚本:

python script.py in.txt out.xlsx

我想更改脚本以将某些列转换为文本,将某些列转换为数字并保留xlsx格式。知道怎么做吗?

2 个答案:

答案 0 :(得分:1)

根据文档,您需要添加数字格式,并在编写需要数字格式的单元格时使用它。

# Add a number format for cells with money.
money = workbook.add_format({'num_format': '$#,##0'})

...

worksheet.write(row_idx, col_idx, your_value, money)

请参阅Tutorial 2: Adding formatting to the XLSX File

答案 1 :(得分:1)

您可以使用strings_to_numbers XlsxWriter构造函数选项。来自docs

  

strings_to_numbers:启用worksheet.write()方法,尽可能使用float()将字符串转换为数字,以避免出现有关“存储为文本的数字”的Excel警告。默认值为False。要启用此选项,请使用:

workbook = xlsxwriter.Workbook(filename, {'strings_to_numbers': True})

示例:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx', {'strings_to_numbers': True})
worksheet = workbook.add_worksheet()

worksheet.write(0, 0, 'Hello')
worksheet.write(1, 0, '123') # Write this string as a number.

workbook.close()

<强>输出:

enter image description here