Python Forum
list from a data file problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list from a data file problem
#1
hello. i need advice for an simple problem.

with open("clusters.csv", "r") as data_file:
    all_words = []
    for line in data_file.readlines():
        for word in line.split("'. "):
            all_words.append(word.lower())
unique_words = set(all_words)
 
xlen = len(unique_words)
#print xlen
xlist = list(unique_words)
for i in range(xlen): 
        print xlist[i]
# i get the result:
(173, 151, 68) ; 1
(255, 230, 191) ; 1
(103, 88, 31) ; 1
(14, 15, 0) ; 1
(48, 45, 19) ; 1

# i want the result to bee
(173, 151, 68) 
(255, 230, 191) 
(103, 88, 31) 
(14, 15, 0)  
(48, 45, 19)  
to use it as a variabel value

i am greatful for your help
Br Marre
Reply
#2
The split statement seems incorrect. Could you post a few lines of the data file? Also why do you still run Python 2 instead of Python 3?
Reply
#3
I have 4 old python2 programs.I have try to use python3,but it allways complain obout some modules,and print error etc. thats why.
But i want to send the csv file to you ,but i dont understand how.Cn you help me
Br Marre
Reply
#4
i have struggle with the problem and get my solusion:
with open("clusters.csv", "r") as data_file:
all_words = []
for line in data_file.readlines():
for word in line.split("'. "):
all_words.append(word.lower())
unique_words = set(all_words)
i = 0
xlen = len(unique_words)
xlist = list(unique_words)
while i <= (xlen):

x = xlist[i].find(";"); # längd
x1 = xlist[i].find(",") # 1 comma plats
x2 = x1 + xlist[i].find(",") # 2 comma plats
x3 = xlist[i].find(")") # ) plats
ww = (xlist[i][0:x]) # clusterdata
rr = int(ww[1:x1]) # röd
gg = int(ww[x1+2:x2+1]) # grön
bb = int(ww[x2+3:x3]) # blue
korr_value = (rr, gg, bb, 255)
pixel_value = (rr, gg, bb, 255)

print korr_value
filename = ( str(rr)+"_"+str(gg)+"_"+str(bb) )
suffix = ".png"
thanks to all that hav toughtabout it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xml file creation from an XML file template and data from an excel file naji_python 1 125 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  Regex text file to store data in list TheSithSiggi 1 147 Dec-03-2020, 04:46 PM
Last Post: bowlofred
  saving data from text file to CSV file in python having delimiter as space K11 1 304 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 395 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Process Data from one csv file and write to another CSV file specific column ajin9581 1 438 Jun-17-2020, 06:00 PM
Last Post: buran
  Problem reading data from file player1681 2 478 May-22-2020, 08:57 AM
Last Post: player1681
  problem coverting string data file to dictionary AKNL 22 1,548 Mar-10-2020, 01:27 PM
Last Post: AKNL
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 722 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  extracting data from a list in a file clarablanes 2 907 Jan-20-2019, 12:17 PM
Last Post: clarablanes
  Problem with serial data. How do I ignor incorrect data? donmerch 1 1,588 Jul-11-2017, 01:55 AM
Last Post: donmerch

Forum Jump:

User Panel Messages

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