Python Forum
Converting .txt to .csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting .txt to .csv file
#18
A .csv file is a text file.

Something has to tell the csv reader what belongs in what column.

Below, the separator is a comma.

The comma was put there when I saved a Libre Office spreadsheet as .csv

Here is a peek:

Quote:Photo,phone,number,name,attendance,attn%,Final Score,points%,points,hwW16,cwW16,Week 16,hwW15,cwW15,Week 15,hwW14,cwW14,Week 14,hwW13,cwW13,Week 13,hwW12,cwW12,Week 12,hwW11,cwW11,Week 11,hwW10,cwW10,Week 10,hwW9,cwW9,Week 9
,,,,max Attendance,,,Max Score,425,,,,50,25,,50,25,,50,25,,50,25,,50,25,,0,25,,0,25,
,,,23名,8,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Photo,phone,学号,姓名,出勤,出勤率,期末分数,得分率,得分,HW,CW,Week 16,HW,CW,Week 15,HW,CW,Week 14,HW,CW,Week 13,HW,CW,Week 12,HW,CW,Week 11,HW,CW,Week 10,HW,CW,Week 9
,,1921030146,聂林,0,0,0,0,0,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,,1921090147,郜阳杰,0,0,0,0,0,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,,1922190144,汪毓韬,0,0,0,0,0,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,,1923010312,张长乐,7,100,100,100,425,,,1,50,25,1,50,25,1,50,25,1,50,25,1,50,25,1,0,25,1,0,25,1

When I open this .csv file with Libre Office, LO looks for the separator. Can be tab, comma, dunno what.

This reads the above file and also can save the data I want, better than csv.reader() (I think):

import pandas as pd

# Read the file

file1 = '/home/pedro/winter2020/20PY/dumpFiles/attendanceWinter2020_20PYlatestPyMade_test1.csv'

dg = pd.read_csv(file1, usecols=['number', 'name', 'Final Score'])

# write the file

file2 = '/home/pedro/winter2020/20PY/dumpFiles/panda_bear.csv'

dg.to_csv(file2, encoding='utf8', index=False, sep=',')
Reply


Messages In This Thread
Converting .txt to .csv file - by SunWers - Jan-01-2021, 06:29 AM
RE: Converting .txt to .csv file - by palladium - Jan-01-2021, 06:52 AM
RE: Converting .txt to .csv file - by SunWers - Jan-01-2021, 07:31 AM
RE: Converting .txt to .csv file - by palladium - Jan-01-2021, 07:53 AM
RE: Converting .txt to .csv file - by SunWers - Jan-01-2021, 08:04 AM
RE: Converting .txt to .csv file - by buran - Jan-01-2021, 07:27 AM
RE: Converting .txt to .csv file - by SunWers - Jan-01-2021, 07:39 AM
RE: Converting .txt to .csv file - by buran - Jan-01-2021, 07:41 AM
RE: Converting .txt to .csv file - by SunWers - Jan-01-2021, 07:48 AM
RE: Converting .txt to .csv file - by palladium - Jan-01-2021, 07:56 AM
RE: Converting .txt to .csv file - by buran - Jan-01-2021, 07:54 AM
RE: Converting .txt to .csv file - by SunWers - Jan-01-2021, 07:56 AM
RE: Converting .txt to .csv file - by buran - Jan-01-2021, 08:39 AM
RE: Converting .txt to .csv file - by SunWers - Jan-02-2021, 01:22 AM
RE: Converting .txt to .csv file - by stevenroger - Jan-01-2021, 09:55 AM
RE: Converting .txt to .csv file - by buran - Jan-01-2021, 03:04 PM
RE: Converting .txt to .csv file - by buran - Jan-02-2021, 03:24 AM
RE: Converting .txt to .csv file - by Pedroski55 - Jan-02-2021, 05:03 AM
RE: Converting .txt to .csv file - by caleb_cruze - Jan-04-2021, 01:10 PM
RE: Converting .txt to .csv file - by buran - Jan-04-2021, 01:12 PM
RE: Converting .txt to .csv file - by pruthvi666 - Jan-19-2024, 10:36 AM
RE: Converting .txt to .csv file - by Larz60+ - Jan-20-2024, 10:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a json file to a dataframe with rows and columns eyavuz21 13 5,016 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  [split] Help- converting file with pyton script eltomassito 6 3,382 Jul-02-2021, 05:29 PM
Last Post: snippsat
  Help- converting file with pyton script grinleon 3 2,563 Sep-23-2020, 11:48 AM
Last Post: grinleon

Forum Jump:

User Panel Messages

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