将数据集导出到python中的文本文件

时间:2018-11-09 13:11:26

标签: python

我有一个类似此示例的列表:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-af1f7fb2dc52> in <module>()
----> 1 import fastai

~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/__init__.py in <module>()
----> 1 from .basic_train import *
      2 from .callback import *
      3 from .callbacks import *
      4 from .core import *
      5 from .basic_data import *

~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/basic_train.py in <module>()
      1 "Provides basic training and validation with `Learner`"
----> 2 from .torch_core import *
      3 from .basic_data import *
      4 from .callback import *
      5

~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/torch_core.py in <module>()
      1 "Utility functions to help deal with tensors"
----> 2 from .imports.torch import *
      3 from .core import *
      4
      5 AffineMatrix = Tensor

~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/imports/__init__.py in <module>()
      1 from .core import *
----> 2 from .torch import *

~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/imports/torch.py in <module>()
----> 1 import torch, torch.nn.functional as F
      2 from torch import ByteTensor, DoubleTensor, FloatTensor, HalfTensor, LongTensor, ShortTensor, Tensor
      3 from torch import nn, optim, as_tensor
      4 from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, TensorDataset

~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/__init__.py in <module>()
     82     pass
     83
---> 84 from torch._C import *
     85
     86 __all__ += [name for name in dir(_C)

ImportError: dlopen: cannot load any more object with static TLS

In [2]: QXcbConnection: XCB error: 145 (Unknown), sequence: 171, resource id: 0, major code: 139 (Unknown), minor code: 20

我正在尝试将其导出到mylist = ['chr10 30930000 32110000 ARHGAP12,PFKP,ZEB1,ZNF438 4', 'chr15 76430000 77240000 ETFA,ISL2, 4'] 中。实际上每个text filestring would be one line。每个字符串中的每个every line would have 5 columns将是space separated part。因此,在one column中有一些column 4,它们将是comma separated characters。该文件将为in the same column

对于该示例,预期的输出将如下所示:

tab separated

我正在chr10 30930000 32110000 ARHGAP12,PFKP,ZEB1,ZNF438 4 chr15 76430000 77240000 ETFA,ISL2 4 中使用以下代码进行操作:

python

但是它不完全返回我想要的。你知道如何解决吗?

3 个答案:

答案 0 :(得分:3)

代替:


<style name="AppTheme" parent="Theme.MaterialComponents.Light">

按空格分隔后,按标签加入:

f.write("%s\n" % item+ '\t')

答案 1 :(得分:3)

这应该非常简单,如下所示-

aa=['chr10 30930000 32110000 ARHGAP12,PFKP,ZEB1,ZNF438 4', 'chr15 76430000 77240000 ETFA,ISL2, 4']
with open("aa.txt","w") as wr:
    for item in aa:
        wr.write("\t".join(item.split(" ")) + "\n")

答案 2 :(得分:2)

您的item中的每一个都是字符串,例如'chr10 30930000 32110000 ARHGAP12,PFKP,ZEB1,ZNF438 4'。 我想您希望将每个列值之间的空格转换为制表符。为了达到这一目的,您应该添加另一个嵌套循环。像这样:

with open('outfile.txt', "w") as f:
    for line in mylist:
        for item in line.split(' '):
            f.write(item + '\t')
        f.write("\n")