Python Forum
ValueError-trying to split a line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError-trying to split a line
#1
Hi,

I'm trying to split a line to use some numbers somwhere else, but i dont understand what is causing this error. I'm still a newbie in python so probably it's easy.
So the error shows that:
Error:
Traceback (most recent call last): File "C:\Users\samue\Documents\Documents\prepa\PT\tipe\expérimentation\releves\moyenne_val_triée.py", line 18, in <module> t,h,Text,Tint,lux = ligne.strip().split("\t") ValueError: not enough values to unpack (expected 5, got 1)
The code i use
with open("data.txt","r") as f:
    for i in f:
        for _ in range(10):
            ligne=f.readline()
            print(ligne)
            t,h,Text,Tint,lux = ligne.strip().split("\t")
Maybe it could help to see whhat look like the line i try to split:
0.0 59.8 8.78 9.375 3546.53
0.0 59.77 8.77 9.375 3586.64
0.0 59.75 8.77 9.375 3627.19
and the code i use to organize the text:
fichier=open("data.txt","w")
for t,h,Text,Tint,Lux in zip(liste_t,liste_h,liste_Text,liste_Tint,liste_Lux):
    print(t,h,Text,Tint,Lux,sep="\t")
    t=str(t)
    h=str(h)
    Text=str(Text)
    Tint=str(Tint)
    Lux=str(Lux)
    fichier.write(t+"\t")
    fichier.write(h+"\t")
    fichier.write(Text+"\t")
    fichier.write(Tint+"\t")
    fichier.write(Lux+"\t"+"\n")
fichier.close()
As you see, I just show you the part of the programm which create the "\t" if you want to see the rest just ask.Same if you need further information.

Cordially,Samu3l
Reply
#2
The error is from line 18, but that doesn't match your code.
Reply
#3
(Feb-05-2018, 10:10 PM)Larz60+ Wrote: The error is from line 18, but that doesn't match your code.
well it's just that I initialize some element before but the error is in the text shown. Anyway i add those lines if it can help.
import os
os.chdir("C:\\Users\\samue\\Documents\\Documents\\prepa\\PT\\tipe\\expĂŠrimentation\\releves")
liste_t=[]
liste_Text=[]
liste_Tint=[]
liste_h=[]
liste_Lux=[]
tps=0
tempext=0
tempint=0
hum=0
lum=0
with open("data.txt","r") as f:
    for i in f:
        for _ in range(10):
            ligne=f.readline()
            print(ligne)
            t,h,Text,Tint,lux = ligne.strip().split("\t")
Reply
#4
change this line:
            t,h,Text,Tint,lux = ligne.strip().split("\t")
to:
            ligne.strip().split("\t")
            print('length: {}, ligne: {}',format(len(ligne), ligne))
            t,h,Text,Tint,lux = ligne
So you can better see what's in ligne, and if it split correctly
Reply
#5
i try to change the line as you said but it give me en error, i should have change some values ?
Error:
Traceback (most recent call last): File "C:\Users\samue\Documents\Documents\prepa\PT\tipe\expérimentation\releves\moyenne_val_triée.py", line 19, in <module> print('length: {}, ligne: {}',format(len(ligne),ligne)) ValueError: Invalid format specifier
the code
with open("data.txt","r") as f:
    for i in f:
        for _ in range(10):
            ligne=f.readline()
            print(ligne)
            ligne.strip().split("\t")
            print('length: {}, ligne: {}',format(len(ligne),ligne))
            t,h,Text,Tint,lux = ligne
Reply
#6
line#7, it should be .format, not ,format
Reply
#7
ok i change it so it show that:
length: 30, ligne: 0.0	59.77	8.77	9.375	3586.64
Error:
Traceback (most recent call last): File "C:\Users\samue\Documents\Documents\prepa\PT\tipe\expérimentation\releves\moyenne_val_triée.py", line 20, in <module> t,h,Text,Tint,lux = ligne ValueError: too many values to unpack (expected 5)
Reply
#8
There's nothing wrong with the way you are reading data (with for loop, not the second read with readlines) , or
populating the values.
The problem is with range!
you are trying to read 10 lines
you are also trying to read two records at a time
but data runs out before that, thus the error.
The following will work:
with open("data.txt","r") as f:
    for ligne in f:
        print(ligne)
        t, h, Text, Tint, lux = ligne.strip().split("\t")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  split the line fixed length tokens bb8 5 5,655 Nov-25-2017, 06:18 PM
Last Post: heiner55
  How to access each line in for loop and split string SriRajesh 2 3,160 Aug-14-2017, 06:05 PM
Last Post: tetrmnot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020