Read and write files in Python

by | Jul 15, 2017 | Python-example | 0 comments

Reading. CSV files

import os

os.chdir('/Users/diego/Documents/test/facta_example/')
print (os.getcwd())

import pandas as pd
file_csv = pd.read_csv('list_groups.csv', delimiter=";")
file_csv
file_csv.describe()

cell = file_csv.loc[1, ['family']].values[0]
cell

Write. CSV files

data.to_csv('file_name.csv')

Reading. FNA Files (fasta format)

import os;
os.chdir('/Users/diego/Documents/test/facta_example/')
print (os.getcwd())

for record in SeqIO.parse("fichero2.fna", "fasta"):
    print(record.seq)

for record in SeqIO.parse("fichero2.fna", "fasta"):
    print(record.description)

Massive reading of .fna files (compressed fasta format)

import glob, pprint
genomes = glob.glob('../viral/*.fna.gz')
genomes = list(sorted(genomes))

pprint.pprint(genomes)

# ! pip install screed
import screed
for g in genomes:
     for record in screed.open(g):
         print(record.sequence[:5])

Reading .txt files from URL

import urllib.request as p
path=p.urlopen('http://humanstxt.org/humans.txt')
fileFinal=path.read()
fileFinal

Reading .pdb files from URL

import urllib.request as p
path=p.urlopen('http://files.rcsb.org/download/101m.pdb')
fileFinal=path.read()
fileFinal

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *