【问题标题】:Copying the specific patterns from normal file to CSV file in Python在 Python 中将特定模式从普通文件复制到 CSV 文件
【发布时间】:2018-01-10 10:37:17
【问题描述】:

如果我在如下文件 (my.txt) 中有数据:

Version 3.1
# fruitstall name
# first-file
* i fruits friutname N 1 name S
* i fruits  friutname N 1  N 
+ x apple
+ y 1
+ z 23
+ a 51
+ x banana
+ y 2
+ z 68
+ a 27
+ x mango
+ y 3
+ z 46
+ a 49 
+ x orange
+ y 4
+ z 15
+ a 54
+ x butterfruit
+ y 5
+ z 76
+ a 86

如何使用 Python 将其写入 CSV 文件,该文件将包含这样的数据?

apple 1  23 51 
banana 2 68 27
mango 3 46 49
orange 4 15 54
butterfruit 5 76 86

我试过用这个:

with open(Unzipped_file_name) as f:
    for line in f:

在此之后,您能建议如何进行吗?

作为上述数据的图像格式的附加输入文本格式不正确:

input.jpg

上述数据格式的附加输出文本格式不正确:

output.jpg

【问题讨论】:

  • 线条是否总是在 x 和 y 之间交替?

标签: python python-3.x python-2.7 csv subprocess


【解决方案1】:
with open('file.txt') as f:  # open file in read mode
    data = {e: [] for e in ['x', 'y', 'z', 'a']}  # init data dict like this
    data = defaultdict(list)  # or like this. from collections import defaultdict
    for line in f:
        line = line.strip()  # remove new line char
        if line.startswith('+'):  # if line marked with plus
            _, t, v = line.split()  # get 't' (one of x, y, z, a) and v (fruit or num) 
            data[t].append(v)  # append to corresponded list
with open('out.txt', 'w') as f:  # open file in write mode
    for x, y, z, a in zip(data['x'], data['y'], data['z'], data['a']):
        f.write('{} {} {} {}\n'.format(x, y, z, a))

【讨论】:

  • 输入文件中的空行(不包括其结尾)会搞砸一切。
  • 您能否进一步指导我如何以我上面提到的格式将其写入 csv 文件
  • 在下面提供的数据之前,我在文本文件中有一些随机数据,例如版本 3.1 #fruitstall name # first-file * i fruits friutname N 1 name S * i fruits friutname N 1 N * i dsclocal TPAUser TPAUser_$dummy N 1 $dummy N + x apple + y 1 + z 23 + a 51 + x banna + y 2 + z 68 + a 27 + x mango + y 3 + z 46 + a 49 + x orange + y 4 + z 15 + a 54 + x butterfruit + y 5 + z 76 + a 86,因此我认为它无法执行拆分操作,因此使用文本文件中的随机数据如何获得解决方案
  • 由于以上文字未格式化,请在附图中找到如下输入输出数据,input.jpeg,ouput.jpeg
【解决方案2】:
path = 'Path to Text.txt'
file = open(path, "r") 
lines = " ".join(file.readlines()[5:])
lines = lines.split("+ x")
res = []
for i in lines:
    val = filter(None, i.replace("\n", "").replace("+", "").split(" "))
    if val:
        stringVal = val[0]
        for iVal in val[1:]:
            if iVal.isdigit():
                stringVal += " {}".format(iVal)
        res.append(stringVal)
print res

结果:

['apple 1 23 51', 'banna 2 68 27', 'mango 3 46 49', 'orange 4 15 54', 'butterfruit 5 76 86']

【讨论】:

  • 在下面提供的数据之前,我在文本文件中有一些随机数据,例如版本 3.1 #fruitstall name # first-file * i fruits friutname N 1 name S * i fruits friutname N 1 N * i dsclocal TPAUser TPAUser_$dummy N 1 $dummy N + x apple + y 1 + z 23 + a 51 + x banna + y 2 + z 68 + a 27 + x mango + y 3 + z 46 + a 49 + x orange + y 4 + z 15 + a 54 + x butterfruit + y 5 + z 76 + a 86,因此我认为它无法执行拆分操作,因此使用文本文件中的随机数据如何获得解决方案
  • 由于以上文字未格式化,请在附图中找到如下输入输出数据,input.jpeg,ouput.jpeg
  • 非常感谢您的回复,但现在我还有一个查询,如果我有如下列表格式的数据芒果 x 5 y 橙色 x 2 y 苹果 x 6 y 如何将其转换为 csv芒果 5 橙 2 苹果 6
猜你喜欢
  • 2012-07-20
  • 2016-06-07
  • 2014-04-06
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 2018-05-17
相关资源
最近更新 更多