How to Write List Elements into a Tab-Separated File

How to write list elements into a tab-separated file?

I think you're doing a list comprehension over the list inside of the dictionary. An alternative solution would be

with open('fname', 'w') as file:
for nested_list in dict_with_lists.values():
for word in nested_list:
file.write(word + '\t')
file.write('\n')

\I'm just looping over the values of the dictionaries, which are lists in this case and joining them using a tab and writing a newline at the end of each list. I haven't tested it but theoretically I think it should work.

Python - Nested List to Tab Delimited File?

with open('fname', 'w') as file:
file.writelines('\t'.join(i) + '\n' for i in nested_list)

save dataframe with lists in one column as tab separated single entries ".txt" file

This can be achieved by first collapsing the list:

for (i in 1:dim(df)[1]){
df$col1[i] <- paste(unlist(df$col1[i]), collapse = "\t")
}
df$col1 <- unlist(df$col1)

and then saving table

write.table(df, file = file, quote = F, sep = "\t")

Values from list of python dictionaries to tab separated file

Here is a simple example of using csv.DictWriter

>>> import csv
...
... fieldnames = ('id1', 'id2')
... result = [
... {'id1': 230423, 'id2': 'sdfkjs'},
... {'id1': 1932, 'id2': 'sjsdf'}
... ]
...
... with open('test.csv', 'w') as f:
... writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter='\t')
... writer.writeheader()
... writer.writerows(result)
...
>>> with open('test.csv', 'r') as f:
... for line in f:
... print(line.rstrip())
...
id1 id2
230423 sdfkjs
1932 sjsdf

Reading it back in as dictionaries is easy as well using csv.DictReader

>>> with open('test.csv', 'r') as f:
... reader = csv.DictReader(f, delimiter='\t')
... for line in reader:
... print(dict(line))
...
{'id1': '230423', 'id2': 'sdfkjs'}
{'id1': '1932', 'id2': 'sjsdf'}

write dictionary of lists to a tab delimited file in python, with dictionary key values as columns without Pandas

This solution will work for an ambiguous number of items and subitems in the dict:

d = {'item': [1, 2, 3], 'id': [4, 5, 6]}

for i in d:
print(i + "\t", end="")
numSubItems = len(d[i])
print()


for level in range(numSubItems):
for i in d:
print(str(d[i][level]) + "\t", end="")
print()




EDIT:
To implement this with writing to a text file:

d = {'item': [1, 2, 3], 'id': [4, 5, 6], 'test': [6, 7, 8]}


with open('file.txt', 'w') as f:
for i in d:
f.write(i + "\t")
numSubItems = len(d[i])
f.write("\n")

for level in range(numSubItems):
for i in d:
f.write(str(d[i][level]) + "\t")
f.write("\n")

Create tab separated print output from a list of tuples python

Two lines will do:

>>> A = [('1_1248', 1, 1),('1_148', 7, 0),('1_18', 6, 1),('1_10', 2, 1)]
>>> for a in A:
... print "\t".join([str(i) for i in a])
...
1_1248 1 1
1_148 7 0
1_18 6 1
1_10 2 1
>>>

Dictionary of lists: writing lists (with tab separated items) to different files for each key (Python)

The normal file.write takes no keywords args, you are probably getting confused with and should should use the csv module which does take the delimiter argument, iterating over d.items, passing the k key to str.format and the list of lists to csv.writer.writerows.

import csv

for k, rows in d.items():
with open("letter_{}.csv".format(k), "w") as out:
wr = csv.writer(out,delimiter='\t')
wr.writerows(rows)

LetterA.csv:

A   bb  cc
A ff gg

LetterB.csv:

B   dd  ee
B hh ii

write list of tuples of lists to text file

The quotes are not part of the string, they denote the string. You would not be able to remove them.

The csv module makes this taks pretty straightforward:

import csv, itertools
with open('file.csv', 'wb') as f:
writer = csv.writer(f, delimiter="\t")
writer.writerows(list(itertools.chain(*t)) for t in results)

This results in a file where each row corresponds to a tuple and a row contains the letters of both lists, separated by tabs.

Writing into text file as tab separated columns in Python

You just need to pair the elements in the two list. You can do that using the zip function:

with open ('processed_seq.txt','a') as proc_seqf:
for a, am in zip(A, A_modified):
proc_seqf.write("{}\t{}".format(a, am))

I have also used format (see specs) to format the string to get everything in a single line.



Related Topics



Leave a reply



Submit