从python中包含混合字母数字字符的文本文件中获取特定数字

时间:2015-11-16 19:45:09

标签: python csv text-mining

我有一个namespace System.Collections.Generic { public static class Enumerable { //[System.Runtime.CompilerServices.Extension()] public static bool All<T>(this IEnumerable<T> list, T expectedAnswer) where T : IEquatable<T>, IComparable<T> { bool result = true; IEnumerator<T> enumerator = list.GetEnumerator(); while (enumerator.MoveNext()) { result = result && (Object.Equals(enumerator.Current, expectedAnswer)); if (!result) return result; } return result; } public delegate bool MyFunc<T>(T next); public static bool All<T>(this IEnumerable<T> list, MyFunc<T> fn) where T : IEquatable<T>, IComparable<T> { bool result = true; IEnumerator<T> enumerator = list.GetEnumerator(); while (enumerator.MoveNext()) { result = result && fn.Invoke(enumerator.Current); if (!result) return result; } return result; } } } 文件看起来像这样但更长:

IEnumerable<int> ints = new int[] { 2 + 2, 2 * 2, 1 * 3 + 1 };
Console.Write("Result := {0}", ints.All(4)); // outputs "Result := true"
Console.Write("Result := {0}", ints.All(t => t.Equals(4))); // outputs "Result := true"

我想将其转换为逗号分隔文件(csv),以便我可以查看相关性(R值)但由于此文件的奇怪格式而遇到问题。有没有办法在Python中做到这一点?

1 个答案:

答案 0 :(得分:1)

在python中使用 re csv 来解析您的文件并将其转换为csv文件:

import re
import csv

re_expression = '^(.*?) <- (.*?): \((.*?), (.*?)\) correlation \(R\)=(.*?) \((.*?) ms\)$'

with open('output.csv', 'w', newline='') as csvfile:
    outfile = csv.writer(csvfile)
    with open('input.txt') as f:
        while True:
            line = f.readline()
            if not line: break
            m = re.split(re_expression, line)
            outfile.writerow(m[1:-1])