简体   繁体   English

CSV文件创建问题

[英]CSV file creation issue

I'm creating a csv file from a python script with this lines: 我正在使用以下行从python脚本创建一个csv文件:

def createFile():
    logfile = csv.writer(open('logfile.csv', 'wb'), dialect='excel')
    formater = zip(dates, hours, threadids, loglevels, errorcodes, errormessage)
    for i in formater:
        logfile.writerow(i)

And everything works fine until I open the file with Excel because it opens the whole text in one column. 一切正常,直到我用Excel打开文件,因为它在一列中打开整个文本。 This is how one row from the csv looks like: 这是csv中的一行是这样的:

4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.

Is there any way to open the file splitted in columns how it should be? 有没有办法打开在列中拆分的文件应该如何? Regards. 问候。

I saved the following in foo.csv and it opens fine in Excel. 我在foo.csv保存了以下foo.csv ,它在Excel中打开很好。

4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.
4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.
4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.
4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.
4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.

在此输入图像描述

You might want to check if you have the right list separator configured in your regional settings using one of the following: 您可能希望使用以下方法之一检查是否在区域设置中配置了正确的列表分隔符:

  1. Windows Start Button > Control Panel > Regional and Language Options > Regional Options > Customize > List Separator. Windows开始按钮>控制面板>区域和语言选项>区域选项>自定义>列表分隔符。
  2. Windows Start Button > Control Panel > Regional and Language Options > Formats > Additional Settings > List Separator. Windows开始按钮>控制面板>区域和语言选项>格式>其他设置>列表分隔符。

Note that you can also launch 'Regional and Language Options' with the command intl.cpl , ie press Windows + R button and enter intl.cpl and press the 'OK' button. 请注意,您还可以使用命令intl.cpl启动“区域和语言选项”,即按Windows + R按钮并输入intl.cpl并按“确定”按钮。

在此输入图像描述

For Excel to automatically parse your .CSV and split it into columns without you either do so or it asking, your delimiter will need to match your Locale Delimiter which you can find under 为了让Excel自动解析你的.CSV并将其拆分成列而你不这样做或要求它,你的分隔符将需要匹配你可以找到的Locale Delimiter

Control Panel -> Region & Language -> Additional Settings -> List Seperator

And that needs to match the column delimiter you are using in your .CSV file for Excel to open and split the columns correctly. 这需要匹配您在.CSV文件中使用的列分隔符,以便Excel正确打开和拆分列。

The first line of your CSV should be: 您的CSV的第一行应该是:

sep=,

This manner your are saying to Excel to delimit the content by "," automatically. 这样你就可以用Excel来自动地用“,”分隔内容。

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

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