Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import csv
  2.  
  3. data = []
  4.  
  5. def transform_row(row):
  6. # id = new count
  7. age = line[2]
  8. gender = line[3]
  9. url = line[4]
  10.  
  11. return [
  12. #new count
  13. age,
  14. gender,
  15. url
  16. ]
  17.  
  18. # read csv file line by line
  19. with open('data_sample.csv', 'r') as f:
  20. reader = csv.reader(f)
  21.  
  22. """ bad try at ignoring the line with value -1
  23. for value in reader:
  24. if value == '-1':
  25. pass
  26. else:
  27. continue
  28. """
  29.  
  30. # loop through each line in csv and transform
  31. for line in reader:
  32. data.append(transform_row(line))
  33.  
  34. # write a new csv file
  35. with open('data_test.csv', 'w', newline='') as f:
  36. # define new csv writer
  37. writer = csv.writer(f, delimiter=',')
  38.  
  39. # write a header row to our output.csv file
  40. writer.writerow([
  41. #'id', - new line count as id
  42. 'age',
  43. 'gender',
  44. 'url'
  45. ])
  46.  
  47. # write our data to the file
  48. writer.writerows(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement