【问题标题】:CSV data with 2 different delimiters for Python 2/Ignition用于 Python 2/Ignition 的具有 2 个不同分隔符的 CSV 数据
【发布时间】:2021-12-26 13:18:24
【问题描述】:

我正在使用 Jython/Python 2 脚本在 Ignition 软件中开发代码。我们需要从 csv 文件中读取数据,该文件在标题中具有两个分隔符“,”,在数据中具有“\t”。我们使用的代码是:

file_path = r'T:\test1.csv'
csvData = csv.reader(open(file_path, 'r'))
header = csvData.next() # Skip the fist row
dataset = system.dataset.toDataSet(header,list(csvData))
calcwindow.rootContainer.getComponent('Power Table').data = dataset

应用此代码后,我们得到: Power Table

问题是我们如何分离数据,以便所有行和列都与 csv.reader 匹配,因为点火不支持 panda 或 re :(

更新代码,现在它可以正确分离数据:

csvData = csv.reader(open(file_path, 'r'),delimiter=',')
header = csvData.next()# Skip the fist row
for line in csvData:
    str1 = "".join(line) #removes commas
    #print str1
    parts = str1.split("\t")
    print parts
dataset = system.dataset.toDataSet(header,list(parts))
calcwindow.rootContainer.getComponent('Power Table').data = dataset

,但出现了错误代码:

第 0 行的列数与标题列表不同。

有什么建议吗??

谢谢 伊戈尔

【问题讨论】:

  • 实际数据使用两个分隔符“,”和“\t”
  • ['2021-11-05 15:36:58 UTC+0\t663330 - PP 预处理罐 1\t\tCond\t5234.6\t\xb5S/cm\tpH\t5 .79\t\t\t\t\tContent EQP\t4.3385\tmL\t\t\t\t50.5551\tg,,,,,,,,,,,,,,,,,,, ']

标签: javascript python jython opencsv ignition


【解决方案1】:

我自己想办法。

代码如下:

        file_path = r'T:\test1.csv'
    try:
        file = open(file_path)
        csvData = csv.reader(file,delimiter=',') # open the file with comma delimiter
        header = csvData.next()# Skip the fist row
        csvData1 = list(csvData) # create list from data
        lstLine = csvData1[-1] # selects last line added
        str1 = "".join(lstLine) #removes commas and create string
        parts = str1.split("\t") #split string back into list
        dataset = system.dataset.toDataSet(header,[parts])
        calcwindow.rootContainer.getComponent('Power Table').data = dataset
        file.close()
    except:
        print "CSV busy exporting from TIA software"

希望对大家有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多