Reputation: 3
my name is Rhein and I have just started to learn Python, and I'm having a lot of fun :D. I just finished a course on YouTube and I am currently working on a project of mine. Currently, I am trying to separate the columns into their own strings from a crime-data csv.
with open('C:/Users/aferdous/python-works/data-set/crime-data/crime_data-windows-1000.csv') as crime_data:
for crime in crime_data:
id = crime_data.readline(8) #<- prints the first x char of each line
print(id)
case_number = crime_data.readline(8) #<- prints the first x char of each line
print(case_number)
date = crime_data.readline(22) #<- prints the first x char of each line
print(date)
block = crime_data.readline(25) #<- prints the first x char of each line
print(block)
This was easy for the first two columns, since they all have the same amount of character lengths. But for 'block', the words in the columns have different lengths, so I do not know how to extract the right amount of characters from each word in each line. And there is a 1000 lines total. - Thanks
Upvotes: 0
Views: 130
Reputation: 305
I assumen that your csv format is "value1, value2, value3" if that the case you can user a python function called split. Examples:
...
columns = crime_data.split(",")
print(columns[0]) #print column 1
print(columns[2]) #print column 2
...
But for read csv in python there a lot better options you can search in google a examples I found:
Upvotes: 1