简体   繁体   English

将.csv文件中的混合数据写入带有xlwt的.xls - 如何格式化数字?

[英]writing mixed data from a .csv file to an .xls with xlwt - how to format numbers?

I'm trying to read a bunch of mixed data (strings & numbers) from a csv & write everything to an excel file. 我正在尝试从csv中读取一堆混合数据(字符串和数字)并将所有内容写入excel文件。 unfortunately, everything is written as text since in csv text is the only format. 不幸的是,一切都写成文本,因为在csv文本中是唯一的格式。 how can i write the numerical data in the correct format? 如何以正确的格式编写数值数据? this is the code i have so far... 这是我到目前为止的代码......

import xlwt, csv

wb = xlwt.Workbook()
ws = wb.add_sheet('testSheet')

sourceCSV = csv.reader(open('sourceCSVfile.csv', 'rb'), delimiter=",")

for rowi, row in enumerate(sourceCSV):
  for coli, value in enumerate(row):
      ws.write(rowi, coli, value)
wb.save('TEST.xls') 

Somehow, depending on your data, you need to be able to determine what data-type is in each field. 不知何故,根据您的数据,您需要能够确定每个字段中的数据类型。

If your data has the same data-type in each column, you can do something like this: 如果您的数据在每列中具有相同的数据类型,则可以执行以下操作:

# 5 columns: text, integer, float, float, date in YYYY-MM-DD format
import datetime
def date_conv(s):
    return datetime.datetime.strptime(s, "%Y-%m-%d")
converters = (str.strip, int, float, float, date_conv)
# use int if you want to check that it is an int.
# xlwt will convert the int to float anyway.
...
for rowi, row in enumerate(sourceCSV):
  for coli, value in enumerate(row):
      ws.write(rowi, coli, converters[coli](value))

Other possibilities: 其他可能性:

(1) the suck-it-and-see approach: (1)看得见的方法:

def float_if_possible(strg):
    try:
        return float(strg)
    except ValueError:
        return strg
...
ws.write(rowi, coli, float_if_possible(value))

(2) the analytical approach: (2)分析方法:

You need to write carefully nitpicky regexes to analyse your text, and you need to apply them in the appropriate order. 您需要仔细编写挑剔的正则表达式来分析您的文本,并且需要以适当的顺序应用它们。

For a float, consider this: 对于浮动,请考虑以下事项:

float_const_pattern = r"""
    [+-]? # optional sign
    (?:
        (?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
        |
        (?: \d+ \. ) # 1. 12. 123. etc
        |
        (?: \d+ ) # 1 12 123 etc
    )
    # followed by optional exponent part
    (?: [Ee] [+-]? \d+ ) ?
    # followed by end of string
    \Z # don't use $
    """

along with the flag re.VERBOSE . 随着国旗re.VERBOSE Note in particular the "end of string" check. 请特别注意“字符串结束”检查。 If you don't do that, given input 123qwerty , the regex will match 123 and the float("123qwerty") call will raise an exception. 如果你不这样做,给定输入123qwerty ,正则表达式将匹配123并且float("123qwerty")调用将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM