1

My code:

fd = open('C:\Python27\\alu.txt', 'r')
D = dict(line.split("\n") for line in fd)

It shows the following error

traceback (most recent call last):
  File "C:\Users\ram\Desktop\rest_enz3.py", line 8, in <module>
    D = dict(line.split("\n") for line in fd)
ValueError: dictionary update sequence element #69 has length 1; 2 is required
matsjoyce
  • 5,744
  • 6
  • 31
  • 38
  • How about this? `D = dict(line.strip().split(None, 1) for line in fd)` – falsetru Oct 03 '14 at 16:18
  • Could you give an example file like the one you are trying to read? – eri0o Oct 03 '14 at 16:24
  • it is a DNA file like ACGTCGCAGCAGACTAGATACGACT – chakri9121 Oct 03 '14 at 16:27
  • That's all that's in the file? Only those four letters and nothing else? No line breaks or anything? – Kevin Oct 03 '14 at 16:40
  • Take a look [here](http://stackoverflow.com/questions/17936166/unable-to-parse-just-sequences-from-fasta-file). It may help you! – Mauro Baraldi Oct 03 '14 at 16:40
  • Human Alu-Sb subfamily consensus sequence. GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGA TCACGAGGTCAGGAGATCGAGACCATCCTGGCTAACACGGTGAAACCCCGTCTCTACTAA AAATACAAAAATTAGCCGGGCGTGGTGGCGGGCGCCTGTAGTCCCAGCTACTCGGGAGGC TGAGGCAGGAGAATGGCGTGAACCCGGGAGGCGGAGCTTGCAGTGAGCCGAGATCGCGCC ACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAAAAA – chakri9121 Oct 03 '14 at 17:04
  • this is data in my file Kevin,Elric – chakri9121 Oct 03 '14 at 17:04
  • How would you to expect fill your dict? Put some example in the question. – Mauro Baraldi Oct 03 '14 at 18:34
  • I dont understand what you will gain by indexing a `dict`ioanry with line numbers when you can just store each line in a `list`. Can you show an example where this might be useful? There appears to be an overuse of dictionary in the community which works on DNA sequences ... – ssm Oct 04 '14 at 03:58

3 Answers3

1

The only newline you'll ever find in line will be the one at the very end, so line.split("\n") will return a list of length 1. Perhaps you meant to use a different delimiter. If your file looks like...

lorem:ipsum
dolor:sit

Then you should do

D=dict(line.strip().split(":") for line in fd)
Kevin
  • 74,910
  • 12
  • 133
  • 166
0

As Kevin above points out, line.split("\n") is a bit odd, but maybe the file is just a list of dictionary keys?

Regardless, the error you get implies that the line.split("\n") returns just a single element (in other words, the line is missing the trailing newline). For example:

"Key1\n".split("\n") returns ["Key1", ""]

while

"Key1".split("\n") returns ["Key1"]


dict([["key1", ""],["key2", ""]])

is fine, while

dict([["key1, ""],["key2"]])

returns the error you quote

It may be as simple as editing the file in question and adding a new line at the end of the file.

rkh
  • 1,761
  • 1
  • 20
  • 30
0

File example: alu.txt

GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGA
TCACGAGGTCAGGAGATCGAGACCATCCTGGCTAACACGGTGAAACCCCGTCTCTACTAA
AAATACAAAAATTAGCCGGGCGTGGTGGCGGGCGCCTGTAGTCCCAGCTACTCGGGAGGC
TGAGGCAGGAGAATGGCGTGAACCCGGGAGGCGGAGCTTGCAGTGAGCCGAGATCGCGCC

Reading file

with open('C:\\Python27\\alu.txt', 'r') as fp:
    dna = {'Key %i' % i: j.strip() for i, j in enumerate(fp.readlines())}

for key, value in dna.iteritems():
    print key, ':', value

Key 1 : GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGA
Key 2 : TCACGAGGTCAGGAGATCGAGACCATCCTGGCTAACACGGTGAAACCCCGTCTCTACTAA
Key 3 : AAATACAAAAATTAGCCGGGCGTGGTGGCGGGCGCCTGTAGTCCCAGCTACTCGGGAGGC

If you're using Python 3 change the iteration flow to this way

for key, value in dna.items():
    print(key, ':', value)
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43