Python-CSV format cleaning and conversion, CSV format column conversion, CSV format data cleaning [three methods of data reading] [strip, replace, split, join function]

1) CSV format cleaning and conversion

description

The attachment is a CSV format file, and the extracted data is converted into the following format:

( 1) Sort in reverse order by line;

( 2) Each row of data is arranged in reverse order;

( 3) a semicolon (;) instead of a comma (,) divided data, no empty cells. ‬‮‬‪‬‭‬

The data will be output after conversion according to the above requirements.  ‪‬‬‪‬‪‬‪‬‮‬‪‬‭‬

Input and output examples

The following is a sample format, not the final result.

 

Input

Output

Example 1

(The following is in the file)

1,2,3

4,5,6

7,8,9

9;8;7

6;5;4

3;2;1

fo = open ("data.csv") 
lt = fo.readlines () # an element in the list lt 
lt = lt [::-1] # reverse order 
for item in lt: 
    item = item.strip (' \ n ') 
    #Remove the newline at the end of each line 
    item = item [::-1] #Reverse the order of each line element item = item.replace ("", "") #Remove the space of each line element 
    item = item .split (',') #According to split each line of elements to form a list 
    print (';'. join (item)) 
#Split the elements in the list item; split to form a long string fo.close () #Note 
: Use the strip () method to remove the carriage return at the end of each line, and use replace () to remove the spaces on both sides of each line element.

 

2) CSV format column transformation

description

The attachment is a CSV file, please arrange each row in reverse order after column output, without changing the format of each element (such as the layout of surrounding spaces, etc.). ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‬‪‬‮‬‪‬‭‬

Input and output examples

This is a format example, not the correct result.

 

Input

Output

Example 1

(The following is the content of the file)

1,2,3,4

a,b,c,d

4,3,2,1

d,c,b,a

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬

How to read attachments in the  attachment    program?

Attachment 1: data.csv


#My answer fo = open ("data.csv") 
lt = fo.readlines () #Return list lt 
for item in lt: 
    item = item.strip ('\ n') 
    item = item.split (',' ) 
    item = item [::-1] 
print (','. join (item)) 
fo.close () #Reference 

answer 
f = open ("data.csv") 
for line in f: #Directly on the text file f Iterate through 
    line = line.strip ("\ n") 
    ls = line.split (",") 
    ls = ls [::-1] 
    print (",". Join (ls)) 
f.close ()

 

3) CSV format data cleaning

description

The attachment is a CSV file, there are spaces before and after each data, please clean it, the requirements are as follows: ‬‪‬‪‬‪‬‬‪‬‭‬

( 1) Remove the space before and after each data, that is, the data is only separated by a comma (,);

( 2) Printout after cleaning. ‪‬‪‬‪‬‪‬‪‬‮‬‬‮‬‪‬‭‬

Input and output examples

Here is a format example, not the correct result.

 

Input

Output

Example 1

(In the file)

1, 2, 3, 4, 5

'a', 'b' , 'c' , 'd','e'

1,2,3,4,5

'a','b','c','d','e'

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬

How to read attachments in the  attachment    program?

Attachment 1: data.csv


#My answer fo = open ("data.csv") 
lt = fo.readlines () 
for item in lt: #Line by   line processing, suitable for large files (a dozen G levels) 
    item = item.strip ('\ n ') 
    item = item.replace ("", "") 
    item = item.split (', ') 
    print (', '. join (item)) 
#Reference 

 answer    f = open ("data.csv") 
s = f.read () #Read all content 
s = s.replace ("", "") #Replace all spaces at once, suitable for small files 
print (s) 
f.close ()

  

 

 

Guess you like

Origin www.cnblogs.com/Anjoras-bk/p/12701941.html