How to cut a line in python? - string

2331,0,13:30:08,25.35,22.05,23.8,23.9,23.5,23.7,5455,350,23.65,132,23.6,268,23.55,235,23.5,625,23.45,459,23.7,83,23.75,360,23.8,291,23.85,186,23.9,331,0,1,25,1000,733580089,name,,,
I got a line like this and how could I cut it? I only need the first 9 variable like this:
2331,0,13:30:08,25.35,22.05,23.8,23.9,23.5,23.7,5455
the original data i save as txt.file, and could I rewrite the original one and save?

Use either csv or just to straight file io with string split function
For example:
import csv
with open('some.txt', 'rb') as f:
reader = csv.reader(f)
for row in reader:
print row[:9]
or if everything is on a single line and you don't want to use a csv interface
with open('some.txt', 'r') as f:
line = f.read()
print line.split(str=",")[:9]

If you have a file called "content.txt".
f = open("content.txt","r")
contentFile = f.read();
output = contentFile.split(",")[:9]
output = ",".join(output)
f.close()
f = open("content.txt","wb")
f.write(output)

If all your values are stored in an Array, you can slice like this:
arrayB = arrayA[:9]
To get your values into an array you could split your String at every ","
arrayA = inputString.split(str=",")

Related

Random and unique from txt file

I want to get random but unqiues lines/words from a txt file in Python but It doesnt work for me
this is my code :
f=open("Order#.txt", "r")
aaawdad = f.read()
words = aaawdad.split()
Repeat = len(words)
driver = webdriver.Chrome(options=option)
df = pd.read_csv('Order#.txt', sep='\t')
uniques = df[df.columns[0]].unique()
for i in range(Repeat):
Mainlink = 'https://footlocker.narvar.com/footlocker/tracking/startrack?order_number=' + uniques
driver.get(Mainlink)
The text file looks like this :
Order#1
Order#2
Order#3
…
You didn't attach the file.
But I think you should put the lines of the text file to the list and then random the index.

The task is to convert strings to floats, but my files are still strings. Why?

Here I'm trying to convert few numbers inside a list read from a file into float format, but my output comes still as a string format. Where is the problem?
table = []
fileName = input("Enter the name of the file: ")
readFile = open(fileName)
lines = readFile.readlines()
for line in lines:
line = line.split()
for item in line:
item = float(item)
table.append(item)
print(table)
Here is a screenshot of my code :
You should append the item that is a float(stored in the variable Item) and not the string version(stored in the variable line) inside the loop so each item is added as the loop iterates through the items.I also use the split() function to add every three numbers into another nested list
Here is the fixed code:
table = []
readFile = open(filename)
lines = readFile.readlines()
for i in lines:
for line in i.split():
items = float(lines)
table = [[items]]
print(table)
OR:
readFile = open(filename)
lines = readFile.readlines()
table=[([items] for line in i.split) for i in lines]
print(table)
Output:
[[2.0,7.0,6.0],[9.0,5.0,1.0],[4.0,3.0,8.0]]

How to remove more than one symbol from csv

I'm trying to replace my old.csv data that looks like this: 6004387,6219127,'12524449',10340
Into new.csv that should look like this: 6004387|6219127|12524449|10340
What I get now is "['6004387'| '6219127'| ""'12524449'""| '10340']"
How can I remove more than one symbol?
import csv
import string
input_file = open('old.csv', 'r')
output_file = open('new.csv', 'w')
data = csv.reader(input_file)
writer = csv.writer(output_file)
specials = ','
for row in data:
row = str(row)
new_row = str.replace(row,specials,'|')
writer.writerow(new_row.split(','))
input_file.close()
output_file.close()
If you want to remove quote characters from input file, specify quotechar="'" in csv.reader. Also, for | delimiter in output file, specify delimiter='|' in csv.writer:
import csv
input_file = open('old.csv', 'r')
output_file = open('new.csv', 'w')
data = csv.reader(input_file, quotechar="'")
writer = csv.writer(output_file, delimiter='|')
for row in data:
writer.writerow(row)
input_file.close()
output_file.close()
Creates new.csv:
6004387|6219127|12524449|10340
You can use the | as the delimeter and then recurively remove unwanted characters afterwards with a function such as str.strip("\"' ") which removes all characters from the string which match what's in the brackets.

Skip lines with strange characters when I read a file

I am trying to read some data files '.txt' and some of them contain strange random characters and even extra columns in random rows, like in the following example, where the second row is an example of a right row:
CTD 10/07/30 05:17:14.41 CTD 24.7813, 0.15752, 1.168, 0.7954, 1497.¸ 23.4848, 0.63042, 1.047, 3.5468, 1496.542
CTD 10/07/30 05:17:14.47 CTD 23.4846, 0.62156, 1.063, 3.4935, 1496.482
I read the description of np.loadtxt and I have not found a solution for my problem. Is there a systematic way to skip rows like these?
The code that I use to read the files is:
#Function to read a datafile
def Read(filename):
#Change delimiters for spaces
s = open(filename).read().replace(':',' ')
s = s.replace(',',' ')
s = s.replace('/',' ')
#Take the columns that we need
data=np.loadtxt(StringIO(s),usecols=(4,5,6,8,9,10,11,12))
return data
This works without using csv like the other answer and just reads line by line checking if it is ascii
data = []
def isascii(s):
return len(s) == len(s.encode())
with open("test.txt", "r") as fil:
for line in fil:
res = map(isascii, line)
if all(res):
data.append(line)
print(data)
You could use the csv module to read the file one line at a time and apply your desired filter.
import csv
def isascii(s):
len(s) == len(s.encode())
with open('file.csv') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
if len(row)==expected_length and all((isascii(x) for x in row)):
'write row onto numpy array'
I got the ascii check from this thread
How to check if a string in Python is in ASCII?

Convert and concatenate data from two columns of a csv file

I have a csv file which contains data in two columns, as follows:
40500 38921
43782 32768
55136 49651
63451 60669
50550 36700
61651 34321
and so on...
I want to convert each data into it's hex equivalent, then concatenate them, and write them into a column in another csv file.
For example: hex(40500) = 9E34, and hex(38921) = 9809.
So, in output csv file, element A1 would be 9E349809
So, i am expecting column A in output csv file to be:
9E349809
AB068000
D760C1F3
F7DBECFD
C5768F5C
F0D38611
I referred a sample code which concatenates two columns, but am struggling with the converting them to hex and then concatenating them. Following is the code:-
import csv
inputFile = 'input.csv'
outputFile = 'output.csv'
with open(inputFile) as f:
reader = csv.reader(f)
with open(outputFile, 'w') as g:
writer = csv.writer(g)
for row in reader:
new_row = [''.join([row[0], row[1]])] + row[2:]
writer.writerow(new_row)
How can i convert data in each column to its hex equivalent, then concatenate them and write them in another file?
You could do this in 4 steps:
Read the lines from the input csv file
Use formatting options to get the hex values of each number
Perform string concatenation to get your result
Write to new csv file.
Sample Code:
with open (outputFile, 'w') as outfile:
with open (inputFile,'r') as infile:
for line in infile: # Iterate through each line
left, right = int(line.split()[0]), int(line.split()[1]) # split left and right blocks
newstr = '{:x}'.format(left)+'{:x}'.format(right) # create new string using hex values excluding '0x'
outfile.write(newstr) # write to output file
print ('Conversion completed')
print ('Closing outputfile')
Sample Output:
In[44] line = '40500 38921'
Out[50]: '9e349809'
ParvBanks solution is good (clear and functionnal), I would simplify it a little like that:
with open (inputFile,'r') as infile, open (outputFile, 'w+') as outfile:
for line in infile:
outfile.write("".join(["{:x}".format(int(v)) for v in line.split()]))

Resources