-2

EDIT:

data from cosiek.txt:

time  20
speed 50
sth   30
car   new

I have a problem with importing data from my text (tab delimited) file to python script

I got:

name,value = line.split("\t")

ValueError: not enough values to unpack (expected 2, got 1)

this is my code and picture of cosiek44.txt

enter image description here

variables = {}

with open("cosiek44.txt") as f:
     for i,line in enumerate(f):
        if i <=2:
            name,value = line.split("\t")
            variables[name] = float(value)
        else:
            name,value = line.split("\t")
            variables[name] = str(value)

a=variables["time"]
b=variables["speed"]
c=variables["sth"]
d=variables["new"]
Duraa
  • 45
  • 1
  • 1
  • 7

1 Answers1

0

this should work:

with open('cosiek44.txt') as f:
    lines=f.readlines()
    for line in lines:
        line_content=lines.split()
        if line_content[-1].isdigit():
            variables[line_content[0]]=int(line_content[-1])
        else:
            variables[line_content[0]]=line_content[-1]

i dont know what you did wrong except maybe there existing empty lines at the bottom of the file or some error in the line for i,line in enumerate(f):

Imtinan Azhar
  • 1,725
  • 10
  • 26